#!/bin/sh

. "`dirname \"$0\"`/common.sh"

USER="mailto:vincent.vole@example.com"
SENDER="mailto:paul.boddie@example.com"

mkdir -p "$PREFS/$USER"
echo 'Europe/Oslo' > "$PREFS/$USER/TZID"
echo 'share' > "$PREFS/$USER/freebusy_sharing"

mkdir -p "$PREFS/$SENDER"
echo 'Europe/Oslo' > "$PREFS/$SENDER/TZID"

# Test the recipient's initial free/busy state.

test_initial_freebusy
test_initial_freebusy_limited

# Make a new event.

cat > "event_cmd.tmp" <<EOF
new
period 0 edit 20141010 10 00 00 Europe/Oslo 20141010 11 00 00 Europe/Oslo
rrule
freq monthly 1
selection weekday FR 2 end
count 3
end
attendance accept
attendee mailto:vincent.vole@example.com
summary Recurring event
finish
update event.tmp
publish event_self.tmp
UID event_uid.tmp
EOF

text_client "$SENDER" "event_cmd.tmp"

# Obtain the event UID.

read UID1 < "event_uid.tmp"

# Register the event at the sender.

outgoing_script "event_self.tmp"

# Expect free/busy periods for the sender.

list_script "out1f.tmp" "$SENDER" "freebusy"
test_freebusy '20141212T090000Z' '20141212T100000Z' "out1f.tmp"

# Present the request to the recipient.

person_script "event.tmp" "out2.tmp"

# Expect no reply because the recipient must handle the request.

is_absent 'METHOD:REPLY' "out2.tmp" && success || failed "out2.tmp"

# Expect no free/busy periods for the recipient.

list_script "out2f.tmp" "$USER" "freebusy"
test_freebusy_missing '20141212T090000Z' '20141212T100000Z' "out2f.tmp"

# Expect free/busy periods for the sender at the recipient.

list_script "out2fo.tmp" "$USER" "freebusy_other" "$SENDER"
test_freebusy '20141212T090000Z' '20141212T100000Z' "out2fo.tmp"

# Accept the invitation.

cat > "event_accept_cmd.tmp" <<EOF
attendance accept
finish
update event_accept.tmp
EOF

text_client_specific "$USER" "event_accept_cmd.tmp" "$UID1"

# Register the acceptance at the recipient.

outgoing_script "event_accept.tmp"

# Expect free/busy periods at the recipient.

list_script "out2f2.tmp" "$USER" "freebusy"
test_freebusy '20141212T090000Z' '20141212T100000Z' "out2f2.tmp"

# Cancel a recurrence of the event.

cat > "event_cancel_cmd.tmp" <<EOF
period 1 cancel
finish
update event_cancel.tmp
EOF

text_client_specific "$SENDER" "event_cancel_cmd.tmp" "$UID1"

# Register the cancellation at the sender.

outgoing_script "event_cancel.tmp"

# Expect only two periods at the sender.

list_script "out3s.tmp" "$SENDER" "freebusy"
test_freebusy_missing '20141114T090000Z' '20141114T100000Z' "out3s.tmp"
have_occurrences "$UID1" 2 "out3s.tmp" && success || failed "out3s.tmp"

# Register the cancellation at the recipient.

person_script "event_cancel.tmp" "out4.tmp"

# Expect no reply from the recipient.

is_absent 'METHOD:REPLY' "out4.tmp" && success || failed "out4.tmp"

# Expect only two periods at the recipient.

list_script "out4f.tmp" "$USER" "freebusy"
test_freebusy_missing '20141114T090000Z' '20141114T100000Z' "out4f.tmp"
have_occurrences "$UID1" 2 "out4f.tmp" && success || failed "out4f.tmp"

# Expect only two periods for the sender at the recipient.

list_script "out4fo.tmp" "$USER" "freebusy_other" "$SENDER"
test_freebusy_missing '20141114T090000Z' '20141114T100000Z' "out4fo.tmp"
have_occurrences "$UID1" 2 "out4fo.tmp" && success || failed "out4fo.tmp"

# Reschedule the first period

cat > "event_reschedule_cmd.tmp" <<EOF
period 0 edit 20141011 10 00 00 Europe/Oslo 20141011 11 00 00 Europe/Oslo
finish
update event_reschedule.tmp
EOF

text_client_specific "$SENDER" "event_reschedule_cmd.tmp" "$UID1"

# Register the rescheduling at the sender.

outgoing_script "event_reschedule.tmp"

# Expect a change in the first period at the sender.

list_script "out4s.tmp" "$SENDER" "freebusy"
test_freebusy_missing '20141010T080000Z' '20141010T090000Z' "out4s.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out4s.tmp"

# Register the rescheduling at the recipient.

person_script "event_reschedule.tmp" "out5.tmp"

# Expect no reply from the recipient.

is_absent 'METHOD:REPLY' "out5.tmp" && success || failed "out5.tmp"

# Expect no change in the first period at the recipient.

list_script "out5f.tmp" "$USER" "freebusy"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out5f.tmp"
test_freebusy_missing '20141011T080000Z' '20141011T090000Z' "out5f.tmp"

# Expect a change in the first period for the sender at the recipient.

list_script "out5fo.tmp" "$USER" "freebusy_other" "$SENDER"
test_freebusy_missing '20141010T080000Z' '20141010T090000Z' "out5fo.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out5fo.tmp"

# Accept the rescheduling.

cat > "event_accept2_cmd.tmp" <<EOF
attendance accept
finish
update event_accept2.tmp
EOF

text_client_specific "$USER" "event_accept2_cmd.tmp" "$UID1" '20141010T080000Z'

# Register the acceptance at the recipient.

outgoing_script "event_accept2.tmp"

# Expect a change in the first period at the recipient.

list_script "out6f.tmp" "$USER" "freebusy"
test_freebusy_missing '20141010T080000Z' '20141010T090000Z' "out6f.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out6f.tmp"

# Prepare a complete cancellation.

cat > "event_cancel_all_cmd.tmp" <<EOF
cancel
finish
cancel event_cancel_all.tmp
EOF

text_client_specific "$SENDER" "event_cancel_all_cmd.tmp" "$UID1"

# Register the cancellation at the sender.

outgoing_script "event_cancel_all.tmp"

# Expect no periods at the sender.

list_script "out7s.tmp" "$SENDER" "freebusy"
have_occurrences "$UID1" 0 "out7s.tmp" && success || failed "out7s.tmp"

# Register the cancellation at the recipient.

person_script "event_cancel_all.tmp" "out7.tmp"

# Expect no reply from the recipient.

is_absent 'METHOD:REPLY' "out7.tmp" && success || failed "out7.tmp"

# Expect no periods at the recipient.

list_script "out7f.tmp" "$USER" "freebusy"
have_occurrences "$UID1" 0 "out7f.tmp" && success || failed "out7f.tmp"

# Expect no periods for the sender at the recipient.

list_script "out7fo.tmp" "$USER" "freebusy_other" "$SENDER"
have_occurrences "$UID1" 0 "out7fo.tmp" && success || failed "out7fo.tmp"

# Switch time zones.

echo 'Europe/Mariehamn' > "$PREFS/$USER/TZID"

# Register the event at the sender.

outgoing_script "event_self.tmp"

# Expect free/busy periods for the sender.

list_script "out8f.tmp" "$SENDER" "freebusy"
test_freebusy '20141212T090000Z' '20141212T100000Z' "out8f.tmp"

# Present the request to the recipient.

person_script "event.tmp" "out9.tmp"

# Expect no reply because the recipient must handle the request.

is_absent 'METHOD:REPLY' "out9.tmp" && success || failed "out9.tmp"

# Expect no free/busy periods for the recipient.

list_script "out9f.tmp" "$USER" "freebusy"
test_freebusy_missing '20141212T090000Z' '20141212T100000Z' "out9f.tmp"

# Expect free/busy periods for the sender at the recipient.

list_script "out9fo.tmp" "$USER" "freebusy_other" "$SENDER"
test_freebusy '20141212T090000Z' '20141212T100000Z' "out9fo.tmp"

text_client_specific "$USER" "event_accept_cmd.tmp" "$UID1"

# Register the acceptance at the recipient.

outgoing_script "event_accept.tmp"

# Expect free/busy periods at the recipient.

list_script "out9f2.tmp" "$USER" "freebusy"
test_freebusy '20141212T090000Z' '20141212T100000Z' "out9f2.tmp"

# Cancel the event.

outgoing_script "event_cancel_all.tmp"

# Expect no periods at the sender.

list_script "out10s.tmp" "$SENDER" "freebusy"
have_occurrences "$UID1" 0 "out10s.tmp" && success || failed "out10s.tmp"

# Register the cancellation at the recipient.

person_script "event_cancel_all.tmp" "out10.tmp"

# Expect no reply from the recipient.

is_absent 'METHOD:REPLY' "out10.tmp" && success || failed "out10.tmp"

# Expect no periods at the recipient.

list_script "out10f.tmp" "$USER" "freebusy"
have_occurrences "$UID1" 0 "out10f.tmp" && success || failed "out10f.tmp"

# Expect no periods for the sender at the recipient.

list_script "out10fo.tmp" "$USER" "freebusy_other" "$SENDER"
have_occurrences "$UID1" 0 "out10fo.tmp" && success || failed "out10fo.tmp"

# Test floating dates. Here, the outgoing interpretation is different because
# the sender is in a different time zone.

cat > "event_floating_cmd.tmp" <<EOF
new
period 0 edit 20141010 - 20141010 -
rrule
freq monthly 1
selection weekday FR 2 end
count 3
end
attendee mailto:vincent.vole@example.com
summary Recurring event
list
finish
update event_floating.tmp
publish event_floating_self.tmp
UID event_floating_uid.tmp
EOF

text_client "$SENDER" "event_floating_cmd.tmp"

# Obtain the event UID.

read UID2 < "event_floating_uid.tmp"

# Register the event at the sender.

outgoing_script "event_floating_self.tmp"

# Expect free/busy periods for the sender.

list_script "out11s.tmp" "$SENDER" "freebusy"
test_freebusy '20141211T230000Z' '20141212T230000Z' "out11s.tmp"

# Present the request to the recipient.

person_script "event_floating.tmp" "out11.tmp"

# Expect no reply because the recipient must handle the request.

is_absent 'METHOD:REPLY' "out11.tmp" && success || failed "out11.tmp"

# Expect no free/busy periods for the recipient (in either time zone).

list_script "out11f.tmp" "$USER" "freebusy"
test_freebusy_missing '20141211T220000Z' '20141212T220000Z' "out11f.tmp"
test_freebusy_missing '20141211T230000Z' '20141212T230000Z' "out11f.tmp"

# Expect no free/busy periods for the sender at the recipient.
# The organiser is not attending.

list_script "out11fo.tmp" "$USER" "freebusy_other" "$SENDER"
test_freebusy_missing '20141211T220000Z' '20141212T220000Z' "out11fo.tmp"
test_freebusy_missing '20141211T230000Z' '20141212T230000Z' "out11fo.tmp"

# Accept the invitation.

text_client_specific "$USER" "event_accept_cmd.tmp" "$UID2"

# Register the acceptance at the recipient.

outgoing_script "event_accept.tmp"

# Expect free/busy periods at the recipient (in the appropriate time zone).

list_script "out12f.tmp" "$USER" "freebusy"
test_freebusy '20141211T220000Z' '20141212T220000Z' "out12f.tmp"
test_freebusy_missing '20141211T230000Z' '20141212T230000Z' "out12f.tmp"
