#!/bin/sh

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

SENDER="mailto:paul.boddie@example.com"
ROOM="mailto:resource-room-confroom@example.com"
CAR="mailto:resource-car-porsche911@example.com"

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

mkdir -p "$PREFS/$CAR"
echo 'Europe/Mariehamn' > "$PREFS/$CAR/TZID"
echo 'share' > "$PREFS/$CAR/freebusy_sharing"

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

test_initial_freebusy 'resource'
test_initial_freebusy_limited 'resource'

# 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:resource-room-confroom@example.com
attendee mailto:resource-car-porsche911@example.com
summary Test drive
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"

# Present the event to the recipient.

resource_script "event.tmp" "out2.tmp" "out2r.tmp"

# Expect replies because each recipient handles the request.

# Inspect the room's response.

is_present 'METHOD:REPLY' "out2.tmp" && success || failed "out2.tmp"
is_present 'ATTENDEE;PARTSTAT=ACCEPTED' "out2.tmp" && success || failed "out2.tmp"

# Inspect the car's response.

showmail "out2r.tmp" 1 | is_present 'METHOD:REPLY' "out2.tmp" && success || failed "out2.tmp"
showmail "out2r.tmp" 1 | is_present 'ATTENDEE;PARTSTAT=ACCEPTED' "out2.tmp" && success || failed "out2.tmp"

# Expect free/busy periods for the recipients.

list_script "out2f1.tmp" "$ROOM" "freebusy"
test_freebusy '20141126T150000Z' '20141126T160000Z' "out2f1.tmp"
list_script "out2f2.tmp" "$CAR" "freebusy"
test_freebusy '20141126T150000Z' '20141126T160000Z' "out2f2.tmp"

# Present a new free/busy request to the resources.
# NOTE: We might open the event and use that for the request.

cat > "fb_cmd.tmp" <<EOF
new
period 0 edit 20141126 16 00 00 Europe/Oslo 20141126 19 00 00 Europe/Oslo
attendee mailto:resource-room-confroom@example.com
attendee mailto:resource-car-porsche911@example.com
finish
freebusy fb.tmp
EOF

text_client "$SENDER" "fb_cmd.tmp"

# Present the request to the recipients.

resource_script "fb.tmp" "out4.tmp" "out4r.tmp"

# Expect replies because each recipient handles the request.

# Inspect the room's response.

is_present 'METHOD:REPLY' "out4.tmp" && success || failed "out4.tmp"
is_present 'FREEBUSY;FBTYPE=BUSY:20141126T150000Z/20141126T160000Z' "out4.tmp" && success || failed "out4.tmp"

# Inspect the car's response.

showmail "out4r.tmp" 1 | is_present 'METHOD:REPLY' "out4.tmp" && success || failed "out4.tmp"
showmail "out4r.tmp" 1 | is_present 'FREEBUSY;FBTYPE=BUSY:20141126T150000Z/20141126T160000Z' "out4.tmp" && success || failed "out4.tmp"

# Cancel the attendance of the conference room.

cat > "event_cancel_cmd.tmp" <<EOF
attendee mailto:resource-room-confroom@example.com remove
finish
cancel event_cancel.tmp
EOF

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

# Present the request to the recipient.

resource_script "event_cancel.tmp" "out5.tmp"

# Present a new free/busy request to each resource.

cat > "fb_room_cmd.tmp" <<EOF
new
period 0 edit 20141126 16 00 00 Europe/Oslo 20141126 19 00 00 Europe/Oslo
attendee mailto:resource-room-confroom@example.com
finish
freebusy fb_room.tmp
EOF

text_client "$SENDER" "fb_room_cmd.tmp"

# Present the request to the recipients.

resource_script "fb_room.tmp" "out6.tmp"

# Expect a reply but with no free/busy periods.

is_present 'METHOD:REPLY' "out6.tmp" && success || failed "out6.tmp"
is_absent 'FREEBUSY;FBTYPE=BUSY:20141126T150000Z/20141126T160000Z' "out6.tmp" && success || failed "out6.tmp"

# Present a new free/busy request to each resource.

cat > "fb_car_cmd.tmp" <<EOF
new
period 0 edit 20141126 16 00 00 Europe/Oslo 20141126 19 00 00 Europe/Oslo
attendee mailto:resource-car-porsche911@example.com
finish
freebusy fb_car.tmp
EOF

text_client "$SENDER" "fb_car_cmd.tmp"

# Present the request to the recipients.

resource_script "fb_car.tmp" "out7.tmp"

# Expect a reply with free/busy periods.

is_present 'METHOD:REPLY' "out7.tmp" && success || failed "out7.tmp"
is_present 'FREEBUSY;FBTYPE=BUSY:20141126T150000Z/20141126T160000Z' "out7.tmp" && success || failed "out7.tmp"
