#!/bin/sh

THIS_DIR=`dirname $0`

TEMPLATES="$THIS_DIR/templates"
PERSON_SCRIPT="$THIS_DIR/../imip_person.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:vincent.vole@example.com"
SENDER="mailto:paul.boddie@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"
TAB=`printf '\t'`

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

PYTHONPATH="$THIS_DIR/.."
export PYTHONPATH

ACCEPT_SCRIPT="$THIS_DIR/test_handle.py"
ACCEPT_ARGS="accept $STORE"

COUNTER_SCRIPT="$THIS_DIR/test_handle.py"
COUNTER_ARGS="counter $STORE"

DECLINE_SCRIPT="$THIS_DIR/test_handle.py"
DECLINE_ARGS="decline $STORE"

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 'PT60S' > "$PREFS/$USER/freebusy_offers"

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/fb-request-person-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-person.txt" 2>> $ERROR

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

# Present the request to the recipient.

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/event-request-person.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out1.tmp

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

   ! [ -e "$FBFILE" ] \
|| ! grep -q "event6@example.com" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   ! [ -e "$FBOTHERFILE" ] \
|| ! grep -q "event6@example.com" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"

# Counter the invitation.

  "$COUNTER_SCRIPT" $COUNTER_ARGS "$USER" 20141126T170000 20141126T180000 "event6@example.com" 2>> $ERROR \
| tee out2r.tmp \
| "$SHOWMAIL" \
> out2.tmp

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

   ! [ -e "$FBFILE" ] \
|| ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE" \
  && ! grep -q "^20141126T160000Z${TAB}20141126T170000Z" "$FBFILE" ) \
&& echo "Success" \
|| echo "Failed"

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

   ! [ -e "$FBOFFERFILE" ] \
|| ! grep -q "^20141126T160000Z${TAB}20141126T170000Z" "$FBOFFERFILE" \
&& echo "Success" \
|| echo "Failed"

# Send the counter-proposal.

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

   ! [ -e "$FBFILE" ] \
|| ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE" \
  && ! grep -q "^20141126T160000Z${TAB}20141126T170000Z" "$FBFILE" ) \
&& echo "Success" \
|| echo "Failed"

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

# Present the response to the organiser.

  "$PERSON_SCRIPT" $ARGS < out2r.tmp 2>> $ERROR \
| tee out3r.tmp \
| "$SHOWMAIL" \
> out3.tmp

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

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

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

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

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

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/fb-request-person-all.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out4.tmp

   grep -q 'METHOD:REPLY' out4.tmp \
&& ! grep -q 'FREEBUSY;FBTYPE=BUSY:20141126T150000Z/20141126T160000Z' out4.tmp \
&& ! grep -q 'FREEBUSY;FBTYPE=BUSY:20141126T160000Z/20141126T170000Z' out4.tmp \
&& echo "Success" \
|| echo "Failed"

# Decline the counter-proposal.

  sed 's/COUNTER/DECLINECOUNTER/' < out2.tmp \
| sed 's/^From: calendar/To: vincent.vole/' \
| sed 's/^To: paul.boddie/From: paul.boddie/' \
> out5.tmp

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

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

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

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

   ! [ -e "$FBFILE" ] \
|| ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE" \
  && ! grep -q "^20141126T160000Z${TAB}20141126T170000Z" "$FBFILE" ) \
&& echo "Success" \
|| echo "Failed"

   ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBOFFERFILE" \
&& ! grep -q "^20141126T160000Z${TAB}20141126T170000Z" "$FBOFFERFILE" \
&& echo "Success" \
|| echo "Failed"
