#!/bin/sh

THIS_DIR=`dirname $0`

TEMPLATES="$THIS_DIR/templates"
PERSON_SCRIPT="$THIS_DIR/../imip_person.py"
RESOURCE_SCRIPT="$THIS_DIR/../imip_resource.py"
SHOWMAIL="$THIS_DIR/../tools/showmail.py"
STORE=/tmp/store
STATIC=/tmp/static
PREFS=/tmp/prefs
ARGS="-S $STORE -P $STATIC -p $PREFS -d"
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"
TAB=`printf '\t'`

OUTGOING_SCRIPT="$THIS_DIR/../imip_person_outgoing.py"

ERROR=err.tmp

rm -r $STORE
rm -r $STATIC
rm -r $PREFS
rm $ERROR
rm out*.tmp

mkdir -p "$PREFS/$USER"
echo 'Europe/Oslo' > "$PREFS/$USER/TZID"
echo 'share' > "$PREFS/$USER/freebusy_sharing"
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 \
| "$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 < out1.tmp 2>> $ERROR \
| tee out2r.tmp \
| "$SHOWMAIL" \
> out2.tmp

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

   grep -q 'ATTENDEE.*;PARTSTAT=DECLINED' "$STORE/$SENDER/objects/event13@example.com" \
&& 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" \
&& echo "Success" \
|| echo "Failed"

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

# Decline the counter-proposal.

  sed 's/COUNTER/DECLINECOUNTER/' < out1.tmp \
| sed 's/^From: calendar/To: resource-room-sauna/' \
| sed 's/^To: paul.boddie/From: paul.boddie/' \
> out3.tmp

"$OUTGOING_SCRIPT" $ARGS < out3.tmp 2>> $ERROR

  "$RESOURCE_SCRIPT" $ARGS < out3.tmp 2>> $ERROR \
| "$SHOWMAIL" \
> out4.tmp

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

   ! grep -q "^20141126T151500Z${TAB}20141126T154500Z" "$FBOFFERFILE" \
&& 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" \
> out5.tmp

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

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

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

# Present the response to the organiser.

  "$PERSON_SCRIPT" $ARGS < out5.tmp 2>> $ERROR \
| "$SHOWMAIL" \
> out6.tmp

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

   grep -q 'ATTENDEE.*;PARTSTAT=ACCEPTED' "$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" \
> out7.tmp

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