#!/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_accept.py"
ACCEPT_ARGS="$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"
