#!/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"
echo 'add' > "$PREFS/$USER/add_method_response"

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

# 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 a request from an attendee for the event details to be refreshed.

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

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

# Present the result to the recipient.

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

   ! grep -q 'METHOD:REPLY' out3.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 out4.tmp \
| "$OUTGOING_SCRIPT" $ARGS 2>> $ERROR

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

# Test a request from a non-attendee for the event details to be refreshed.

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

   ! grep -q 'METHOD:REQUEST' out5.tmp \
&& 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 another request from an attendee for the event details to be refreshed.

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

   grep -q 'METHOD:REQUEST' out6.tmp \
&& grep -q 'RECURRENCE-ID' out6.tmp \
&& [ `grep 'BEGIN:VEVENT' out6.tmp | wc -l` = '2' ] \
&& echo "Success" \
|| echo "Failed"

# Process the resulting message.

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

   [ -e "$STORE/$USER/objects/event8@example.com" ] \
&& [ -e "$STORE/$USER/recurrences/event8@example.com/20141010T080000Z" ] \
&& echo "Success" \
|| echo "Failed"

# Cancel a recurrence. Both the original and rescheduled recurrences should be
# absent from the free/busy collection.

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

   [ -e "$STORE/$SENDER/objects/event8@example.com" ] \
&& ! [ -e "$STORE/$SENDER/recurrences/event8@example.com/20141010T080000Z" ] \
&& [ -e "$STORE/$SENDER/cancellations/recurrences/event8@example.com/20141010T080000Z" ] \
&& echo "Success" \
|| echo "Failed"

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

# Test another request from an attendee for the event details to be refreshed.
# The additional recurrence should now be absent.

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

   grep -q 'METHOD:REQUEST' out7.tmp \
&& grep -q 'METHOD:CANCEL' out7.tmp \
&& [ `grep 'BEGIN:VEVENT' out7.tmp | wc -l` = '2' ] \
&& echo "Success" \
|| echo "Failed"

# Process the resulting message.

  "$PERSON_SCRIPT" $ARGS < out7r.tmp 2>> $ERROR \
| "$SHOWMAIL" \
> out7a.tmp

   [ -e "$STORE/$USER/objects/event8@example.com" ] \
&& ! [ -e "$STORE/$USER/recurrences/event8@example.com/20141010T080000Z" ] \
&& [ -e "$STORE/$USER/cancellations/recurrences/event8@example.com/20141010T080000Z" ] \
&& echo "Success" \
|| echo "Failed"

# Re-add the cancelled instance.

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

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

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

   [ -e "$STORE/$USER/objects/event8@example.com" ] \
&& [ -e "$STORE/$USER/recurrences/event8@example.com/20141010T080000Z" ] \
&& ! [ -e "$STORE/$USER/cancellations/recurrences/event8@example.com/20141010T080000Z" ] \
&& echo "Success" \
|| echo "Failed"

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

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

# Test acceptance and registration in the outgoing handler.

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

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