#!/bin/sh

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

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

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

# Make a new event.

cat > "event_cmd.tmp" <<EOF
new
period 0 edit 20141126 16 00 00 Europe/Oslo 20141126 17 00 00 Europe/Oslo
attendance accept
attendee mailto:harvey.horse@example.com
attendee mailto:resource-room-confroom@example.com
summary Meeting at 4pm
finish
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 a free/busy period for the sender at the sender.

list_script "out1f.tmp" "$SENDER" "freebusy"
test_freebusy '20141126T150000Z' '20141126T160000Z' "out1f.tmp"

# Cancel the event.

cat > "event_cancel_cmd.tmp" <<EOF
finish
cancel-publish event_cancel.tmp
EOF

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

# Present the cancellation at the sender.

outgoing_script "event_cancel.tmp"

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

list_script "out2f.tmp" "$SENDER" "freebusy"
test_freebusy_missing '20141126T150000Z' '20141126T160000Z' "out2f.tmp"
have_occurrences "$UID1" 0 "out2f.tmp" && success || failed "out2f.tmp"

# Make a recurring event.

cat > "event_recurring_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:resource-room-confroom@example.com
summary Recurring event
finish
publish event_recurring_self.tmp
UID event_recurring_uid.tmp
EOF

text_client "$SENDER" "event_recurring_cmd.tmp"

# Obtain the event UID.

read UID2 < "event_recurring_uid.tmp"

# Register the event at the sender.

outgoing_script "event_recurring_self.tmp"

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

list_script "out3f.tmp" "$SENDER" "freebusy"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out3f.tmp"
have_occurrences "$UID2" 3 "out3f.tmp" && success || failed "out3f.tmp"

# Cancel a recurrence.

cat > "event_cancel_recurring_cmd.tmp" <<EOF
period 1 cancel
finish
publish event_cancel_recurring.tmp
EOF

text_client_specific "$SENDER" "event_cancel_recurring_cmd.tmp" "$UID2"

# Present the cancellation at the sender.

outgoing_script "event_cancel_recurring.tmp"

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

list_script "out4f.tmp" "$SENDER" "freebusy"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out4f.tmp"
test_freebusy_missing '20141114T090000Z' '20141114T100000Z' "out4f.tmp"
have_occurrences "$UID2" 2 "out4f.tmp" && success || failed "out4f.tmp"

# Reschedule an instance.

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

text_client_specific "$SENDER" "event_reschedule_cmd.tmp" "$UID2"

# Present the rescheduling at the sender.

outgoing_script "event_reschedule_self.tmp"

# Expect a rescheduled free/busy period for the sender at the sender.

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

# Cancel the event.

cat > "event_cancel_recurring_all_cmd.tmp" <<EOF
finish
cancel-publish event_cancel_recurring_all.tmp
EOF

text_client_specific "$SENDER" "event_cancel_recurring_all_cmd.tmp" "$UID2"

# Present the cancellation at the sender.

outgoing_script "event_cancel_recurring_all.tmp"

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

list_script "out6f.tmp" "$SENDER" "freebusy"
test_freebusy_missing '20141011T080000Z' '20141011T090000Z' "out6f.tmp"
have_occurrences "$UID2" 0 "out6f.tmp" && success || failed "out6f.tmp"

# Make a recurring all-day event.

cat > "event_recurring_day_cmd.tmp" <<EOF
new
period 0 edit 20141010 - 20141010 -
rrule
freq monthly 1
selection weekday FR 2 end
count 3
end
attendance accept
attendee mailto:resource-room-confroom@example.com
summary Recurring event
finish
publish event_recurring_day_self.tmp
UID event_recurring_day_uid.tmp
EOF

text_client "$SENDER" "event_recurring_day_cmd.tmp"

# Obtain the event UID.

read UID3 < "event_recurring_day_uid.tmp"

# Register the event at the sender.

outgoing_script "event_recurring_day_self.tmp"

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

list_script "out7f.tmp" "$SENDER" "freebusy"
test_freebusy '20141211T230000Z' '20141212T230000Z' "out7f.tmp"
have_occurrences "$UID3" 3 "out7f.tmp" && success || failed "out7f.tmp"

# Cancel the event.

cat > "event_cancel_recurring_day_cmd.tmp" <<EOF
finish
cancel-publish event_cancel_recurring_day.tmp
EOF

text_client_specific "$SENDER" "event_cancel_recurring_day_cmd.tmp" "$UID3"

# Present the cancellation at the sender.

outgoing_script "event_cancel_recurring_day.tmp"

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

list_script "out8f.tmp" "$SENDER" "freebusy"
test_freebusy_missing '20141211T230000Z' '20141212T230000Z' "out8f.tmp"
have_occurrences "$UID2" 0 "out8f.tmp" && success || failed "out8f.tmp"

# Switch time zones.

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

# Register the recurring event at the sender.

outgoing_script "event_recurring_self.tmp"

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

list_script "out9f.tmp" "$SENDER" "freebusy"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out9f.tmp"
have_occurrences "$UID2" 3 "out9f.tmp" && success || failed "out9f.tmp"

# Present the cancellation of the recurring event at the sender.

outgoing_script "event_cancel_recurring_all.tmp"

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

list_script "out10f.tmp" "$SENDER" "freebusy"
test_freebusy_missing '20141011T080000Z' '20141011T090000Z' "out10f.tmp"
have_occurrences "$UID2" 0 "out10f.tmp" && success || failed "out10f.tmp"

# Register the recurring all-day event at the sender.

outgoing_script "event_recurring_day_self.tmp"

# Expect free/busy periods for the sender at the sender.
# With the changed time zone, the periods will be earlier than before.

list_script "out11f.tmp" "$SENDER" "freebusy"
test_freebusy '20141211T220000Z' '20141212T220000Z' "out11f.tmp"
have_occurrences "$UID3" 3 "out11f.tmp" && success || failed "out11f.tmp"

# Present the cancellation at the sender.

outgoing_script "event_cancel_recurring_day.tmp"

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

list_script "out12f.tmp" "$SENDER" "freebusy"
test_freebusy_missing '20141211T220000Z' '20141212T220000Z' "out12f.tmp"
have_occurrences "$UID2" 0 "out12f.tmp" && success || failed "out12f.tmp"
