#!/bin/sh

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

USER="mailto:vincent.vole@example.com"
SENDER="mailto:paul.boddie@example.com"

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-rdate.txt" 2>> $ERROR

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "freebusy" \
>  out1f.tmp

   grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out1f.tmp" \
&& echo "Success" \
|| echo "Failed"

# There should be an event created by the sender.

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "object" "event26@example.com" \
>  out1O.tmp

   grep -q 'event26@example.com' "out1O.tmp" \
&& echo "Success" \
|| echo "Failed"

# Test registration in the incoming handler for the recipient.

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

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out2f.tmp

   ! grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out2f.tmp" \
&& echo "Success" \
|| echo "Failed"

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy_other" "$SENDER" \
>  out2o.tmp

   grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out2o.tmp" \
&& echo "Success" \
|| echo "Failed"

# There should be an event created by the sender.

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "object" "event26@example.com" \
>  out2O.tmp

   grep -q 'event26@example.com' "out2O.tmp" \
&& echo "Success" \
|| echo "Failed"

# Test acceptance and registration in the outgoing handler.

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out3f.tmp

   grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out3f.tmp" \
&& echo "Success" \
|| echo "Failed"

# Test registration in the incoming handler.

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

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out4f.tmp

   [ `grep "event26@example.com" "out4f.tmp" | wc -l` = '2' ] \
&& grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out4f.tmp" \
&& echo "Success" \
|| echo "Failed"

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy_other" "$SENDER" \
>  out4o.tmp

   [ `grep "event26@example.com" "out4o.tmp" | wc -l` = '2' ] \
&& grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out4o.tmp" \
&& echo "Success" \
|| echo "Failed"

# Test recurrence declining in the outgoing handler.

  "$DECLINE_SCRIPT" $DECLINE_ARGS "$USER" "20141011T100000" "20141011T110000" "event26@example.com" "20141011T100000" 2>> $ERROR \
| tee out5.tmp \
| "$OUTGOING_SCRIPT" $ARGS 2>> $ERROR

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out5s.tmp

   [ `grep "event26@example.com" "out5s.tmp" | wc -l` = '1' ] \
&& ! grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out5s.tmp" \
&& echo "Success" \
|| echo "Failed"

# There should be a recurrence created by the user.

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "object" "event26@example.com" \
>  out5O.tmp

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "recurrence" "event26@example.com" "20141011T100000" \
>  out5R.tmp

   grep -q 'event26@example.com' "out5O.tmp" \
&& grep -q 'event26@example.com' "out5R.tmp" \
&& echo "Success" \
|| echo "Failed"

# Test declining in the incoming handler.

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

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "freebusy" \
>  out6f.tmp

   [ `grep "event26@example.com" "out6f.tmp" | wc -l` = '2' ] \
&& grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out6f.tmp" \
&& echo "Success" \
|| echo "Failed"

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "freebusy_other" "$USER" \
>  out6o.tmp

   [ `grep "event26@example.com" "out6o.tmp" | wc -l` = '1' ] \
&& ! grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out6o.tmp" \
&& echo "Success" \
|| echo "Failed"

# There should be a recurrence created by the user.

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "object" "event26@example.com" \
>  out6O.tmp

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "recurrence" "event26@example.com" "20141011T100000" \
>  out6R.tmp

   grep -q 'event26@example.com' "out6O.tmp" \
&& grep -q 'event26@example.com' "out6R.tmp" \
&& echo "Success" \
|| echo "Failed"

# Test recurrence acceptance in the outgoing handler.

  "$ACCEPT_SCRIPT" $ACCEPT_ARGS "$USER" "20141011T100000" "20141011T110000" "event26@example.com" "20141011T100000" 2>> $ERROR \
| tee out7.tmp \
| "$OUTGOING_SCRIPT" $ARGS 2>> $ERROR

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "freebusy" \
>  out7s.tmp

   [ `grep "event26@example.com" "out7s.tmp" | wc -l` = '2' ] \
&& grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out7s.tmp" \
&& echo "Success" \
|| echo "Failed"

# There should still be a recurrence created by the user.

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "object" "event26@example.com" \
>  out7O.tmp

   "$LIST_SCRIPT" $LIST_ARGS "$USER" "recurrence" "event26@example.com" "20141011T100000" \
>  out7R.tmp

   grep -q 'event26@example.com' "out7O.tmp" \
&& grep -q 'event26@example.com' "out7R.tmp" \
&& echo "Success" \
|| echo "Failed"

# Test acceptance in the incoming handler.

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

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "freebusy" \
>  out8f.tmp

   [ `grep "event26@example.com" "out8f.tmp" | wc -l` = '2' ] \
&& grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out8f.tmp" \
&& echo "Success" \
|| echo "Failed"

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "freebusy_other" "$USER" \
>  out8o.tmp

   [ `grep "event26@example.com" "out8o.tmp" | wc -l` = '2' ] \
&& grep -q "^20141011T080000Z${TAB}20141011T090000Z" "out8o.tmp" \
&& echo "Success" \
|| echo "Failed"

# There should be a recurrence created by the user.

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "object" "event26@example.com" \
>  out8O.tmp

   "$LIST_SCRIPT" $LIST_ARGS "$SENDER" "recurrence" "event26@example.com" "20141011T100000" \
>  out8R.tmp

   grep -q 'event26@example.com' "out8O.tmp" \
&& grep -q 'event26@example.com' "out8R.tmp" \
&& echo "Success" \
|| echo "Failed"
