#!/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"
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"

# Test free/busy responses.

  "$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"

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

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

# Publish an event, testing 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"

# Test registration in the incoming handler for 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"

   ! 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

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

# Test recurrence cancellation in the outgoing handler.

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

   [ `grep "event8@example.com" "$FBSENDERFILE" | wc -l` = '2' ] \
&& ! grep -q "^20141114T090000Z${TAB}20141114T100000Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

# Test cancellation in the incoming handler.

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

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

   [ `grep "event8@example.com" "$FBFILE" | wc -l` = '2' ] \
&& ! grep -q "^20141114T090000Z${TAB}20141114T100000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   [ `grep "event8@example.com" "$FBOTHERFILE" | wc -l` = '2' ] \
&& ! grep -q "^20141114T090000Z${TAB}20141114T100000Z" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"

# Test rescheduling in the outgoing handler.

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

   grep -q "^20141011T080000Z${TAB}20141011T090000Z" "$FBSENDERFILE" \
&& ! grep -q "^20141010T080000Z${TAB}20141010T090000Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

# Test rescheduling in the incoming handler.

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

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

   ! grep -q "^20141011T080000Z${TAB}20141011T090000Z" "$FBFILE" \
&& grep -q "^20141010T080000Z${TAB}20141010T090000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

   grep -q "^20141011T080000Z${TAB}20141011T090000Z" "$FBOTHERFILE" \
&& ! grep -q "^20141010T080000Z${TAB}20141010T090000Z" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"

# Test rescheduling acceptance in the outgoing handler.

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

   grep -q "^20141011T080000Z${TAB}20141011T090000Z" "$FBFILE" \
&& ! grep -q "^20141010T080000Z${TAB}20141010T090000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

# Test complete cancellation in the outgoing handler.

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

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

# Test complete cancellation in the incoming handler.

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

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

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

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

# Switch time zones.

echo 'Europe/Mariehamn' > "$PREFS/$USER/TZID"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

# Test floating dates. Here, the outgoing interpretation is different because
# the sender is in a different time zone.

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

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

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

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

   ! grep -q "^20141211T220000Z${TAB}20141212T220000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

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

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

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