#!/bin/sh

. "`dirname \"$0\"`/common.sh"

USER="mailto:paul.boddie@example.com"
FBFILE="$STORE/$USER/freebusy"

mkdir -p "$PREFS/$USER"
echo 'Europe/Oslo' > "$PREFS/$USER/TZID"
echo 'share' > "$PREFS/$USER/freebusy_sharing"

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request.txt" 2>> $ERROR
cp "$FBFILE" out1.tmp

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

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-cancel.txt" 2>> $ERROR
echo "Cancel..."
cp "$FBFILE" out2.tmp

   ! grep -q '^2' "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-recurring.txt" 2>> $ERROR
cp "$FBFILE" out3.tmp

   [ `cat "$FBFILE" | wc -l` = '3' ] \
&& grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-cancel-recurring-instance.txt" 2>> $ERROR
cp "$FBFILE" out4.tmp

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

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-recurring-reschedule-instance.txt" 2>> $ERROR
cp "$FBFILE" out5.tmp

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

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-cancel-recurring.txt" 2>> $ERROR
cp "$FBFILE" out6.tmp

   ! grep -q '^2' "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-recurring-day.txt" 2>> $ERROR
cp "$FBFILE" out7.tmp

   [ `cat "$FBFILE" | wc -l` = '3' ] \
&& grep -q "^20141211T230000Z${TAB}20141212T230000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-cancel-recurring-day.txt" 2>> $ERROR
cp "$FBFILE" out8.tmp

   ! grep -q '^2' "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

# Switch time zones.

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

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-recurring.txt" 2>> $ERROR
cp "$FBFILE" out9.tmp

   [ `cat "$FBFILE" | wc -l` = '3' ] \
&& grep -q "^20141212T090000Z${TAB}20141212T100000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-cancel-recurring.txt" 2>> $ERROR
cp "$FBFILE" out10.tmp

   ! grep -q '^2' "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-recurring-day.txt" 2>> $ERROR
cp "$FBFILE" out11.tmp

   [ `cat "$FBFILE" | wc -l` = '3' ] \
&& grep -q "^20141211T230000Z${TAB}20141212T230000Z" "$FBFILE" \
&& echo "Success" \
|| echo "Failed"

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-cancel-recurring-day.txt" 2>> $ERROR
cp "$FBFILE" out12.tmp

   ! grep -q '^2' "$FBFILE" \
&& echo "Success" \
|| echo "Failed"
