#!/bin/sh

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

USER="mailto:resource-room-sauna@example.com"
SENDER="mailto:paul.boddie@example.com"
RIVALSENDER="mailto:vincent.vole@example.com"
FBFILE="$STORE/$USER/freebusy"
FBOFFERFILE="$STORE/$USER/freebusy-offers"
FBSENDERFILE="$STORE/$SENDER/freebusy"
FBSENDEROTHERFILE="$STORE/$SENDER/freebusy-other/$USER"
FBSENDERREQUESTS="$STORE/$SENDER/requests"
FBRIVALSENDERFILE="$STORE/$RIVALSENDER/freebusy"

mkdir -p "$PREFS/$USER"
echo 'Europe/Oslo' > "$PREFS/$USER/TZID"
echo 'share' > "$PREFS/$USER/freebusy_sharing"
echo 'schedule_corrected_in_freebusy' > "$PREFS/$USER/scheduling_function"
echo '10,12,14,16,18:0,15,30,45' > "$PREFS/$USER/permitted_times"
echo 'PT60S' > "$PREFS/$USER/freebusy_offers"

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/fb-request-sauna-all.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out0.tmp

   grep -q 'METHOD:REPLY' out0.tmp \
&& ! grep -q '^FREEBUSY' out0.tmp \
&& echo "Success" \
|| echo "Failed"

# Attempt to schedule an event.

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-sauna-bad.txt" 2>> $ERROR

   grep -q "^20141126T151000Z${TAB}20141126T154500Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-sauna-bad.txt" 2>> $ERROR \
| tee out1r.tmp \
| "$SHOWMAIL" \
> out1.tmp

   grep -q 'METHOD:COUNTER' out1.tmp \
&& grep -q 'DTSTART;TZID=Europe/Oslo.*:20141126T161500' out1.tmp \
&& echo "Success" \
|| echo "Failed"

   ! [ -e "$FBFILE" ] \
|| ! grep -q "^20141126T151500Z${TAB}20141126T154500Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   grep -q "^20141126T151500Z${TAB}20141126T154500Z" "$FBOFFERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the response to the organiser.

  "$PERSON_SCRIPT" $ARGS < out1r.tmp 2>> $ERROR \
| tee out2r.tmp \
| "$SHOWMAIL" \
> out2.tmp

   ( ! [ -e "$FBSENDEROTHERFILE" ] \
  || ! grep -q "^20141126T151000Z${TAB}20141126T154500Z" "$FBSENDEROTHERFILE") \
&& echo "Success" \
|| echo "Failed"

   grep -q 'DTSTART;TZID=Europe/Oslo.*:20141126T161000' "$STORE/$SENDER/objects/event13@example.com" \
&& echo "Success" \
|| echo "Failed"

   grep -q 'DTSTART;TZID=Europe/Oslo.*:20141126T161500' "$STORE/$SENDER/counters/objects/event13@example.com/$USER" \
&& echo "Success" \
|| echo "Failed"

   grep -q 'event13@example.com' "$FBSENDERREQUESTS" \
&& echo "Success" \
|| echo "Failed"

# Attempt to schedule an event in the proposed period.

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-sauna-rival.txt" 2>> $ERROR

   ! grep -q "^20141126T151000Z${TAB}20141126T154500Z" "$FBRIVALSENDERFILE" \
&& grep -q "^20141126T153000Z${TAB}20141126T154500Z" "$FBRIVALSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-sauna-rival.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out3.tmp

   grep -q 'METHOD:REPLY' out3.tmp \
&& grep -q 'ATTENDEE.*;PARTSTAT=DECLINED' out3.tmp \
&& echo "Success" \
|| echo "Failed"

# Present the response to the organiser.

  "$PERSON_SCRIPT" $ARGS < out3.tmp 2>> $ERROR \
| tee out4r.tmp \
| "$SHOWMAIL" \
> out4.tmp

   ! grep -q "^20141126T151000Z${TAB}20141126T154500Z" "$FBRIVALSENDERFILE" \
&& grep -q "^20141126T153000Z${TAB}20141126T154500Z" "$FBRIVALSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

   grep -q 'ATTENDEE.*;PARTSTAT=DECLINED' "$STORE/$RIVALSENDER/objects/event18@example.com" \
&& echo "Success" \
|| echo "Failed"

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

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/fb-request-sauna-all.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out5.tmp

   grep -q 'METHOD:REPLY' out5.tmp \
&& ! grep -q 'FREEBUSY;FBTYPE=BUSY:20141126T151000Z/20141126T154500Z' out5.tmp \
&& ! grep -q 'FREEBUSY;FBTYPE=BUSY:20141126T151500Z/20141126T154500Z' out5.tmp \
&& ! grep -q 'FREEBUSY;FBTYPE=BUSY:20141126T153000Z/20141126T154500Z' out5.tmp \
&& echo "Success" \
|| echo "Failed"

# Attempt to schedule again in response to the counter-proposal.

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-sauna-good.txt" 2>> $ERROR

   ! grep -q "^20141126T151000Z${TAB}20141126T154500Z" "$FBSENDERFILE" \
&& ! grep -q "^20141126T151500Z${TAB}20141126T154500Z" "$FBSENDERFILE" \
&& ! grep -q "^20141126T153000Z${TAB}20141126T154500Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

   [ ! -e "$STORE/$SENDER/counters/objects/event13@example.com/$USER" ] \
&& echo "Success" \
|| echo "Failed"

   ! grep -q 'event13@example.com' "$FBSENDERREQUESTS" \
&& echo "Success" \
|| echo "Failed"

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-sauna-good.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out6.tmp

   grep -q 'METHOD:REPLY' out6.tmp \
&& grep -q 'ATTENDEE.*;PARTSTAT=ACCEPTED' out6.tmp \
&& echo "Success" \
|| echo "Failed"

   grep -q "^20141126T150000Z${TAB}20141126T154500Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   ! grep -q "^20141126T150000Z${TAB}20141126T154500Z" "$FBOFFERFILE" \
&& ! grep -q "^20141126T151500Z${TAB}20141126T154500Z" "$FBOFFERFILE" \
&& ! grep -q "^20141126T153000Z${TAB}20141126T154500Z" "$FBOFFERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the response to the organiser.

  "$PERSON_SCRIPT" $ARGS < out6.tmp 2>> $ERROR \
| tee out7r.tmp \
| "$SHOWMAIL" \
> out7.tmp

   grep -q "^20141126T150000Z${TAB}20141126T154500Z" "$FBSENDERFILE" \
&& ! grep -q "^20141126T151000Z${TAB}20141126T154500Z" "$FBSENDERFILE" \
&& ! grep -q "^20141126T151500Z${TAB}20141126T154500Z" "$FBSENDERFILE" \
&& ! grep -q "^20141126T153000Z${TAB}20141126T154500Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

   [ ! -e "$STORE/$SENDER/counters/objects/event13@example.com/$USER" ] \
&& echo "Success" \
|| echo "Failed"

   ! grep -q 'event13@example.com' "$FBSENDERREQUESTS" \
&& echo "Success" \
|| echo "Failed"

# Check the free/busy state of the resource again.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/fb-request-sauna-all.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out6.tmp

   grep -q 'METHOD:REPLY' out6.tmp \
&& grep -q 'FREEBUSY;FBTYPE=BUSY:20141126T150000Z/20141126T154500Z' out6.tmp \
&& ! grep -q 'FREEBUSY;FBTYPE=BUSY:20141126T151000Z/20141126T154500Z' out6.tmp \
&& ! grep -q 'FREEBUSY;FBTYPE=BUSY:20141126T151500Z/20141126T154500Z' out6.tmp \
&& ! grep -q 'FREEBUSY;FBTYPE=BUSY:20141126T153000Z/20141126T154500Z' out6.tmp \
&& echo "Success" \
|| echo "Failed"
