#!/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/$USER/TZID"
echo 'always' > "$PREFS/$SENDER/event_refreshing"

# 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"

# Test the nature of the acceptance message.

   showmail "event_accept.tmp" | is_present 'METHOD:REPLY' \
&& success || failed "event_accept.tmp"

# Expect free/busy periods at the recipient.

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

# Present the result to the sender.

person_script "event_accept.tmp" "out4.tmp"

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

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

# Attempt to add an occurrence to the event.

cat > "event_cmd2.tmp" <<EOF
period new 20150109 10 00 00 Europe/Oslo 20150109 11 00 00 Europe/Oslo
finish
update event_add.tmp
EOF

text_client_specific "$SENDER" "event_cmd2.tmp" "$UID1"

# Register the new occurrence at the sender.

outgoing_script "event_add.tmp"

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

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

# Present the request to the recipient.

person_script "event_add.tmp" "out5.tmp"

# Expect a response demanding a refreshed event.

is_present 'METHOD:REFRESH' "out5.tmp" && success || failed "out5.tmp"

# Expect no new free/busy period for the event at the recipient.

list_script "out5f.tmp" "$USER" "freebusy"
test_freebusy '20141212T090000Z' '20141212T100000Z' "out5f.tmp"
test_freebusy_missing '20150109T090000Z' '20150109T100000Z' "out5f.tmp"

list_script "out5fo.tmp" "$USER" "freebusy_other" "$SENDER"
test_freebusy '20141212T090000Z' '20141212T100000Z' "out5fo.tmp"
test_freebusy_missing '20150109T090000Z' '20150109T100000Z' "out5fo.tmp"

# Present the refresh message to the organiser.

person_script "out5.tmp" "out6.tmp" "out6r.tmp"

# Expect a new event request from the organiser.

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

# Present the request to the recipient.

person_script "out6r.tmp" "out7.tmp"

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

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

# Expect no new free/busy period for the event at the recipient.

list_script "out7f.tmp" "$USER" "freebusy"
test_freebusy '20141212T090000Z' '20141212T100000Z' "out7f.tmp"
test_freebusy_missing '20150109T090000Z' '20150109T100000Z' "out7f.tmp"

# Expect the new free/busy period for the sender at the recipient.

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

# Test acceptance and registration in the outgoing handler.

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

# Accept the invitation.

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

text_client_specific "$USER" "event_accept_cmd2.tmp" "$UID1" '20150109T090000Z'

# Register the acceptance at the recipient.

outgoing_script "event_accept2.tmp"

# Test the nature of the acceptance message.

   showmail "event_accept2.tmp" | is_present 'METHOD:REPLY' \
&& success || failed "event_accept2.tmp"

# Expect the new free/busy period at the recipient.

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