#!/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"
FBOTHERFILE="$STORE/$USER/freebusy-other/$SENDER"
FBSENDERFILE="$STORE/$SENDER/freebusy"
FBSENDEROTHERFILE="$STORE/$SENDER/freebusy-other/$USER"
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"

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"

mkdir -p "$PREFS/$SENDER"
echo 'Europe/Oslo' > "$PREFS/$USER/TZID"
echo 'always' > "$PREFS/$SENDER/event_refreshing"

# Test event request registration in the outgoing handler.

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-person-recurring.txt" 2>> $ERROR

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the request to the recipient.

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

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

   ! [ -e "$FBFILE" ] \
|| ! grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"

# Test acceptance and registration in the outgoing handler.

  "$ACCEPT_SCRIPT" $ACCEPT_ARGS "$USER" "event8@example.com" 2>> $ERROR \
| tee out3.tmp \
| "$OUTGOING_SCRIPT" $ARGS 2>> $ERROR

   "$SHOWMAIL" < out3.tmp | grep -q 'METHOD:REPLY' \
&& echo "Success" \
|| echo "Failed"

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the result to the recipient.

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

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBSENDEROTHERFILE" \
&& echo "Success" \
|| echo "Failed"

# Attempt to add an occurrence to the event.

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-add-person-recurring.txt" 2>> $ERROR

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBSENDERFILE" \
&& grep -q "^20150109T090000Z${TAB}20150109T100000Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the request to the recipient.

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/event-add-person-recurring.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out5.tmp

   grep -q 'METHOD:REFRESH' out5.tmp \
&& echo "Success" \
|| echo "Failed"

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBFILE" \
&& ! grep -q "^20150109T090000Z${TAB}20150109T100000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBOTHERFILE" \
&& grep -q "^20150109T090000Z${TAB}20150109T100000Z" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the refresh message to the organiser.

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

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

# Present the request to the recipient.

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

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

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBFILE" \
&& ! grep -q "^20150109T090000Z${TAB}20150109T100000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBOTHERFILE" \
&& grep -q "^20150109T090000Z${TAB}20150109T100000Z" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"

# Test acceptance and registration in the outgoing handler.

  "$ACCEPT_SCRIPT" $ACCEPT_ARGS "$USER" "event8@example.com" 2>> $ERROR \
| tee out8.tmp \
| "$OUTGOING_SCRIPT" $ARGS 2>> $ERROR

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

  "$ACCEPT_SCRIPT" $ACCEPT_ARGS "$USER" "event8@example.com" "20150109T090000Z" 2>> $ERROR \
| tee out9.tmp \
| "$OUTGOING_SCRIPT" $ARGS 2>> $ERROR

   "$SHOWMAIL" < out9.tmp | grep -q 'METHOD:REPLY' \
&& echo "Success" \
|| echo "Failed"

   grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBFILE" \
&& grep -q "^20150109T090000Z${TAB}20150109T100000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"
