#!/bin/sh

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

USER1="mailto:vincent.vole@example.com"
USER2="mailto:harvey.horse@example.com"
SENDER="mailto:paul.boddie@example.com"

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

mkdir -p "$PREFS/$USER2"
echo 'Europe/Oslo' > "$PREFS/$USER2/TZID"
echo 'share' > "$PREFS/$USER2/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
period new 20141011 10 00 00 Europe/Oslo 20141011 11 00 00 Europe/Oslo
attendance accept
attendee mailto:vincent.vole@example.com
attendee mailto:harvey.horse@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 at the sender.

list_script "out1f.tmp" "$SENDER" "freebusy"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out1f.tmp"

# There should be an event created by the sender.

list_script "out1O.tmp" "$SENDER" "object" "$UID1"
is_present "$UID1" "out1O.tmp" && success || failed "out1O.tmp"

# Present the event to the recipients.

person_script "event.tmp" "out2.tmp"

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

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

# Expect no free/busy periods for the recipients.

list_script "out2f1.tmp" "$USER1" "freebusy"
test_freebusy_missing '20141011T080000Z' '20141011T090000Z' "out2f1.tmp"

list_script "out2f2.tmp" "$USER2" "freebusy"
test_freebusy_missing '20141011T080000Z' '20141011T090000Z' "out2f2.tmp"

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

list_script "out2fo1.tmp" "$USER1" "freebusy_other" "$SENDER"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out2fo1.tmp"

list_script "out2fo2.tmp" "$USER2" "freebusy_other" "$SENDER"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out2fo2.tmp"

# There should be an event created by the sender.

list_script "out2O1.tmp" "$USER1" "object" "$UID1"
is_present "$UID1" "out2O1.tmp" && success || failed "out2O1.tmp"

list_script "out2O2.tmp" "$USER1" "object" "$UID1"
is_present "$UID1" "out2O2.tmp" && success || failed "out2O2.tmp"

# Accept the invitation.

cat > "event_accept1_cmd.tmp" <<EOF
attendance accept
finish
update event_accept1.tmp
EOF

  sed 's/event_accept1.tmp/event_accept2.tmp/' "event_accept1_cmd.tmp" \
> "event_accept2_cmd.tmp"

text_client_specific "$USER1" "event_accept1_cmd.tmp" "$UID1"
text_client_specific "$USER2" "event_accept2_cmd.tmp" "$UID1"

# Register the acceptance at the recipients.

outgoing_script "event_accept1.tmp"
outgoing_script "event_accept2.tmp"

# Expect free/busy periods at the recipients.

list_script "out3f1.tmp" "$USER1" "freebusy"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out3f1.tmp"

list_script "out3f2.tmp" "$USER2" "freebusy"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out3f2.tmp"

# Test registration in the incoming handler.

person_script "event_accept1.tmp" "out4i1.tmp"
list_script "out4f1.tmp" "$SENDER" "freebusy_other" "$USER1"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out4f1.tmp"
list_script "out4f2v1.tmp" "$SENDER" "freebusy_other" "$USER2"
test_freebusy_missing '20141011T080000Z' '20141011T090000Z' "out4f2v1.tmp"

person_script "event_accept2.tmp" "out4i2.tmp"
list_script "out4f2v2.tmp" "$SENDER" "freebusy_other" "$USER1"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out4f2v2.tmp"

# Decline the invitation to the recurrence for the first user.

cat > "event_decline1_cmd.tmp" <<EOF
attendance decline
finish
update event_decline1.tmp
EOF

text_client_specific "$USER1" "event_decline1_cmd.tmp" "$UID1" '20141011T080000Z'

# Register the rejection at the user.

outgoing_script "event_decline1.tmp"

# Expect only one free/busy period at the user.

list_script "out5s.tmp" "$USER1" "freebusy"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out5s.tmp"
test_freebusy_missing '20141011T080000Z' '20141011T090000Z' "out5s.tmp"

# There should be a recurrence created by the user.

list_script "out5O.tmp" "$USER1" "object" "$UID1"
is_present "$UID1" "out5O.tmp" && success || failed "out5O.tmp"

list_script "out5R.tmp" "$USER1" "recurrence" "$UID1" '20141011T080000Z'
is_present "$UID1" "out5R.tmp" && success || failed "out5R.tmp"

# Register the rejection at the sender.

person_script "event_decline1.tmp" "out6.tmp"

# Expect two free/busy periods for the sender.

list_script "out6f.tmp" "$SENDER" "freebusy"
have_occurrences "$UID1" 2 "out6f.tmp" && success || failed "out6f.tmp"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out6f.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out6f.tmp"

# Expect one free/busy period for the first user at the sender.

list_script "out6o1.tmp" "$SENDER" "freebusy_other" "$USER1"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out6o1.tmp"
test_freebusy_missing '20141011T080000Z' '20141011T090000Z' "out6o1.tmp"

# Expect two free/busy periods for the second user at the sender.

list_script "out6o2.tmp" "$SENDER" "freebusy_other" "$USER2"
have_occurrences "$UID1" 2 "out6o2.tmp" && success || failed "out6o2.tmp"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out6o2.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out6o2.tmp"

# There should be a recurrence created by the user.

list_script "out6O.tmp" "$SENDER" "object" "$UID1"
is_present "$UID1" "out6O.tmp" && success || failed "out6O.tmp"

list_script "out6R.tmp" "$SENDER" "recurrence" "$UID1" '20141011T080000Z'
is_present "$UID1" "out6R.tmp" && success || failed "out6R.tmp"

# This should cause the organiser to tell the second user about the recurrence.

is_present 'METHOD:REQUEST' "out6.tmp" && success || failed "out6.tmp"

# Present the update to the second user.

person_script "out6.tmp" "out62.tmp"

# There should be a recurrence created by the first user at the second user.

list_script "out6O2.tmp" "$USER2" "object" "$UID1"
is_present "$UID1" "out6O2.tmp" && success || failed "out6O2.tmp"

list_script "out6R2.tmp" "$USER2" "recurrence" "$UID1" '20141011T080000Z'
is_present "$UID1" "out6R2.tmp" && success || failed "out6R2.tmp"

# The second user's schedule should remain unchanged.
# NOTE: The nature of the periods might need to change, with the recurrence
# NOTE: taking over the affected period.

list_script "out6f2.tmp" "$USER2" "freebusy"
have_occurrences "$UID1" 2 "out6f2.tmp" && success || failed "out6f2.tmp"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out6f2.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out6f2.tmp"

# Accept the invitation for the recurrence at the first user.

  sed 's/event_accept1.tmp/event_accept1r.tmp/' "event_accept1_cmd.tmp" \
> "event_accept1r_cmd.tmp"

text_client_specific "$USER1" "event_accept1r_cmd.tmp" "$UID1" '20141011T080000Z'

# Register acceptance in the outgoing handler.

outgoing_script "event_accept1r.tmp"

# Expect both periods in the user's free/busy collection.

list_script "out7f.tmp" "$USER1" "freebusy"
have_occurrences "$UID1" 2 "out7f.tmp" && success || failed "out7f.tmp"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out7f.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out7f.tmp"

# There should still be a recurrence created by the user.

list_script "out7O.tmp" "$USER1" "object" "$UID1"
is_present "$UID1" "out7O.tmp" && success || failed "out7O.tmp"

list_script "out7R.tmp" "$USER1" "recurrence" "$UID1" '20141011T080000Z'
is_present "$UID1" "out7R.tmp" && success || failed "out7R.tmp"

# Register the acceptance at the sender.

person_script "event_accept1r.tmp" "out8.tmp" 

# Expect both periods in the sender's free/busy collection.

list_script "out8f.tmp" "$SENDER" "freebusy"
have_occurrences "$UID1" 2 "out8f.tmp" && success || failed "out8f.tmp"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out8f.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out8f.tmp"

# Expect both periods for the first user at the sender.

list_script "out8o1.tmp" "$SENDER" "freebusy_other" "$USER1"
have_occurrences "$UID1" 2 "out8o1.tmp" && success || failed "out8o1.tmp"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out8o1.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out8o1.tmp"

# The second user should not have been affected.

list_script "out8o2.tmp" "$SENDER" "freebusy_other" "$USER2"
have_occurrences "$UID1" 2 "out8o2.tmp" && success || failed "out8o2.tmp"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out8o2.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out8o2.tmp"

# There should be a recurrence created by the user.

list_script "out8O.tmp" "$SENDER" "object" "$UID1"
is_present "$UID1" "out8O.tmp" && success || failed "out8O.tmp"

list_script "out8R.tmp" "$SENDER" "recurrence" "$UID1" '20141011T080000Z'
is_present "$UID1" "out8R.tmp" && success || failed "out8R.tmp"

# Test recurrence declining in the outgoing handler.
# Now the second user declines the parent event.

  sed 's/event_decline1.tmp/event_decline2.tmp/' "event_decline1_cmd.tmp" \
> "event_decline2_cmd.tmp"

text_client_specific "$USER2" "event_decline2_cmd.tmp" "$UID1"

# Register acceptance in the outgoing handler.

outgoing_script "event_decline2.tmp"

# Expect only the recurrence period for the second user.

list_script "out9s.tmp" "$USER2" "freebusy"
have_occurrences "$UID1" 1 "out9s.tmp" && success || failed "out9s.tmp"
test_freebusy_missing '20141010T080000Z' '20141010T090000Z' "out9s.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out9s.tmp"

# Test declining in the incoming handler.

person_script "event_decline2.tmp" "out10.tmp"

# Expect periods for the organiser as before.

list_script "out10f.tmp" "$SENDER" "freebusy"
have_occurrences "$UID1" 2 "out10f.tmp" && success || failed "out10f.tmp"
test_freebusy '20141010T080000Z' '20141010T090000Z' "out10f.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out10f.tmp"

# Expect only the recurrence period for the second user at the organiser.

list_script "out10o2.tmp" "$SENDER" "freebusy_other" "$USER2"
have_occurrences "$UID1" 1 "out10o2.tmp" && success || failed "out10o2.tmp"
test_freebusy_missing '20141010T080000Z' '20141010T090000Z' "out10o2.tmp"
test_freebusy '20141011T080000Z' '20141011T090000Z' "out10o2.tmp"
