#!/bin/sh

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

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

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

person_script "$TEMPLATES/fb-request-person-all.txt" "out0.tmp"

   is_present 'METHOD:REPLY' "out0.tmp" \
&& is_absent '^FREEBUSY' "out0.tmp" \
&& success || failed "out0.tmp"

# 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:vincent.vole@example.com
summary Meeting at 4pm
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.tmp"

# Expect free/busy periods at the sender.

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

# Present the request to the recipient.

person_script "event.tmp" "out1.tmp"

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

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

# Expect no free/busy periods for the recipient.

list_script "out1f.tmp" "$USER" "freebusy"
test_freebusy_missing '20141126T150000Z' '20141126T160000Z' "out1f.tmp"

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

list_script "out1fo.tmp" "$USER" "freebusy_other" "$SENDER"
test_freebusy '20141126T150000Z' '20141126T160000Z' "out1fo.tmp"

# Counter the invitation.

cat > "event_counter_cmd.tmp" <<EOF
period 0 edit 20141126 17 00 00 Europe/Oslo 20141126 18 00 00 Europe/Oslo
finish
update event_counter.tmp
EOF

text_client_specific "$USER" "event_counter_cmd.tmp" "$UID1"

# Expect a counter-proposal from the client.

   showmail "event_counter.tmp" | is_present 'METHOD:COUNTER' \
&& success || failed "event_counter.tmp"

# Note that the invitation has only been prepared, not processed.

list_script "out2f.tmp" "$USER" "freebusy"
test_freebusy_missing '20141126T150000Z' '20141126T160000Z' "out2f.tmp"
test_freebusy_missing '20141126T160000Z' '20141126T170000Z' "out2f.tmp"

list_script "out2fo.tmp" "$USER" "freebusy_offers"
test_freebusy_missing '20141126T160000Z' '20141126T170000Z' "out2fo.tmp"

# Send the counter-proposal.

outgoing_script "event_counter.tmp"

# Expect no free/busy periods for the recipient.

list_script "out2f2.tmp" "$USER" "freebusy"
test_freebusy_missing '20141126T150000Z' '20141126T160000Z' "out2f2.tmp"
test_freebusy_missing '20141126T160000Z' '20141126T170000Z' "out2f2.tmp"

# Expect a free/busy offer period.

list_script "out2o2.tmp" "$USER" "freebusy_offers"
test_freebusy '20141126T160000Z' '20141126T170000Z' "out2o2.tmp"

# Present the response to the organiser.

person_script "event_counter.tmp" "out3.tmp"

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

list_script "out3f.tmp" "$SENDER" "freebusy_other" "$USER"
test_freebusy_missing '20141126T150000Z' '20141126T160000Z' "out3f.tmp"
test_freebusy_missing '20141126T160000Z' '20141126T170000Z' "out3f.tmp"

# Expect the original object at the sender.

list_script "out3O.tmp" "$SENDER" "object" "$UID1"
   is_present 'DTSTART;TZID=Europe/Oslo.*:20141126T160000' "out3O.tmp" \
&& success || failed "out3O.tmp"

# Expect the countered object at the sender.

list_script "out3C.tmp" "$SENDER" "countered_object" "$UID1" "$USER"
   is_present 'DTSTART;TZID=Europe/Oslo.*:20141126T170000' "out3C.tmp" \
&& success || failed "out3C.tmp"

# Expect a request at the sender.

list_script "out3R.tmp" "$SENDER" "requests"
is_present "$UID1" "out3R.tmp" && success || failed "out3R.tmp"

# Check the free/busy state of the recipient so far.

person_script "$TEMPLATES/fb-request-person-all.txt" "out4.tmp"

   is_present 'METHOD:REPLY' "out4.tmp" \
&& is_absent '^FREEBUSY' "out4.tmp" \
&& success || failed "out4.tmp"

# Decline the counter-proposal.
# NOTE: Support this in the client.

  showmail "event_counter.tmp" \
| sed 's/COUNTER/DECLINECOUNTER/' \
| sed "s/^From: $USERADDRESS/To: $USERADDRESS/" \
| sed "s/^To: $SENDERADDRESS/From: $SENDERADDRESS/" \
> "event_declinecounter.tmp"

outgoing_script "event_declinecounter.tmp"

# The countered object should be removed at the sender.

list_script "out5C.tmp" "$SENDER" "countered_object" "$UID1" "$USER"
is_absent "$UID1" "out5C.tmp" && success || failed "out5C.tmp"

# The request should be removed at the sender.

list_script "out5R.tmp" "$SENDER" "requests"
is_absent "$UID1" "out5R.tmp" && success || failed "out5R.tmp"

# Present the decline message to the recipient.

person_script "event_declinecounter.tmp" "out6.tmp"

# Expect no free/busy periods at the recipient.

list_script "out6f.tmp" "$USER" "freebusy"
test_freebusy_missing '20141126T150000Z' '20141126T160000Z' "out6f.tmp"
test_freebusy_missing '20141126T160000Z' '20141126T170000Z' "out6f.tmp"

# Expect no free/busy offers at the recipient.

list_script "out6o.tmp" "$USER" "freebusy_offers"
test_freebusy_missing '20141126T150000Z' '20141126T160000Z' "out6o.tmp"
test_freebusy_missing '20141126T160000Z' '20141126T170000Z' "out6o.tmp"
