#!/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
ARGS="-S $STORE -P $STATIC -d"
USER="mailto:vincent.vole@example.com"
SENDER="mailto:paul.boddie@example.com"
FBFILE="$STORE/$USER/freebusy"
FBOTHERFILE="$STORE/$USER/freebusy-other/$SENDER"
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"

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

rm -r $STORE
rm -r $STATIC

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

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

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/fb-request-person.txt" 2> /dev/null \
| "$SHOWMAIL" \
> out1.tmp

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

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/event-request-person.txt" 2> /dev/null \
| "$SHOWMAIL" \
> out2.tmp

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

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

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

  "$ACCEPT_SCRIPT" $ACCEPT_ARGS "$USER" "event6@example.com" \
| tee out3.tmp \
| "$OUTGOING_SCRIPT" $ARGS

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

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/event-request-person-conflict.txt" 2> /dev/null \
| "$SHOWMAIL" \
> out4.tmp

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

   ! grep -q "event7@example.com" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   grep -q "event7@example.com" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"

  "$DECLINE_SCRIPT" $DECLINE_ARGS "$USER" "event7@example.com" \
| tee out5.tmp \
| "$OUTGOING_SCRIPT" $ARGS

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

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/event-cancel-person.txt" 2> /dev/null \
| "$SHOWMAIL" \
> out6.tmp

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

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

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

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/event-cancel-person-all.txt" 2> /dev/null \
| "$SHOWMAIL" \
> out7.tmp

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

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

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

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/event-request-person-spoofed.txt" 2> /dev/null \
| "$SHOWMAIL" \
> out8.tmp

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

   ! grep -q "spoof2@example.com" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   ! grep -q "spoof2@example.com" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"
