#!/bin/sh

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

USER1="mailto:resource-car-porsche911@example.com"
USER2="mailto:resource-car-fiat500@example.com"
SENDER1="mailto:paul.boddie@example.com"
SENDER2="mailto:vincent.vole@example.com"
SENDERADDRESS1="paul.boddie@example.com"
SENDERADDRESS2="vincent.vole@example.com"
FBFILE1="$STORE/$USER1/freebusy"
FBFILE2="$STORE/$USER2/freebusy"
FBSENDERFILE1="$STORE/$SENDER1/freebusy"
FBSENDERFILE2="$STORE/$SENDER2/freebusy"
QUOTA=cars
JOURNALFILE1="$JOURNAL/$QUOTA/journal/$SENDER1"
JOURNALFILE2="$JOURNAL/$QUOTA/journal/$SENDER2"

mkdir -p "$PREFS/$USER1"
echo 'Europe/Oslo' > "$PREFS/$USER1/TZID"
echo 'share' > "$PREFS/$USER1/freebusy_sharing"
cat > "$PREFS/$USER1/scheduling_function" <<EOF
schedule_in_freebusy
check_quota $QUOTA
EOF
cat > "$PREFS/$USER1/confirmation_function" <<EOF
add_to_quota $QUOTA
EOF
cat > "$PREFS/$USER1/retraction_function" <<EOF
remove_from_quota $QUOTA
EOF

mkdir -p "$PREFS/$USER2"
echo 'Europe/Oslo' > "$PREFS/$USER2/TZID"
echo 'share' > "$PREFS/$USER2/freebusy_sharing"
cat > "$PREFS/$USER2/scheduling_function" <<EOF
schedule_in_freebusy
check_quota $QUOTA
EOF
cat > "$PREFS/$USER2/confirmation_function" <<EOF
add_to_quota $QUOTA
EOF
cat > "$PREFS/$USER2/retraction_function" <<EOF
remove_from_quota $QUOTA
EOF

mkdir -p "$JOURNAL/$QUOTA"
cat > "$JOURNAL/$QUOTA/limits" <<EOF
mailto:vincent.vole@example.com PT10H
* PT5H
EOF

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/fb-request-car-all.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out0.tmp

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

# Attempt to schedule an event.

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

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

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-cars-recurring.txt" 2>> $ERROR \
| tee out1r.tmp \
| "$SHOWMAIL" \
> out1.tmp

   grep -q 'METHOD:REPLY' out1.tmp \
&& grep -q 'ATTENDEE.*;PARTSTAT=DECLINED' out1.tmp \
&& echo "Success" \
|| echo "Failed"

   ( ! [ -e "$FBFILE1" ] \
  || ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE1" \
    && ! grep -q "^20141206T150000Z${TAB}20141206T160000Z" "$FBFILE1" )) \
&& ( ! [ -e "$FBFILE2" ] \
  || ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE2" \
    && ! grep -q "^20141206T150000Z${TAB}20141206T160000Z" "$FBFILE2" )) \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is not confirmed).

   ! [ -e "$JOURNALFILE1" ] \
|| ! grep -q "event25@example.com" "$JOURNALFILE1" \
&& echo "Success" \
|| echo "Failed"

# Modify the event and attempt to schedule it again.

  sed 's/FREQ=DAILY/FREQ=DAILY;COUNT=5/;' "$TEMPLATES/event-request-cars-recurring.txt" \
| "$OUTGOING_SCRIPT" $ARGS 2>> $ERROR

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

# Present the request to the resource.

  sed 's/FREQ=DAILY/FREQ=DAILY;COUNT=5/;' "$TEMPLATES/event-request-cars-recurring.txt" \
| "$RESOURCE_SCRIPT" $ARGS 2>> $ERROR \
| tee out2r.tmp \
| "$SHOWMAIL" \
> out2.tmp

# Since the email module used by showmail.py cannot stop after reading a single
# message, the second message is obtained.

  grep -n '^From ' out2r.tmp \
| tail -n 1 \
| cut -d ':' -f 1 \
| xargs -I{} tail -n +'{}' out2r.tmp \
| "$SHOWMAIL" \
>> out2.tmp

   grep -q 'METHOD:REPLY' out2.tmp \
&& grep -q 'ATTENDEE.*;PARTSTAT=ACCEPTED' out2.tmp \
&& grep -q 'ATTENDEE.*;PARTSTAT=DECLINED' out2.tmp \
&& echo "Success" \
|| echo "Failed"

   (( ! [ -e "$FBFILE1" ] \
   || ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE1" \
     && ! grep -q "^20141130T150000Z${TAB}20141130T160000Z" "$FBFILE1" )) \
     && [ -e "$FBFILE2" ] \
     && grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE2" \
     && grep -q "^20141130T150000Z${TAB}20141130T160000Z" "$FBFILE2" ) \
|| (( ! [ -e "$FBFILE2" ] \
   || ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE2" \
     && ! grep -q "^20141130T150000Z${TAB}20141130T160000Z" "$FBFILE2" )) \
     && [ -e "$FBFILE1" ] \
     && grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE1" \
     && grep -q "^20141130T150000Z${TAB}20141130T160000Z" "$FBFILE1" ) \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is confirmed for one resource).

   ! [ -e "$JOURNALFILE1" ] \
|| grep -q "event25@example.com" "$JOURNALFILE1" \
&& echo "Success" \
|| echo "Failed"

# Cancel the event.

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

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

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-cancel-cars-recurring.txt" 2>> $ERROR \
| tee out3r.tmp \
| "$SHOWMAIL" \
> out3.tmp

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

   ( ! [ -e "$FBFILE1" ] \
  || ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE1" \
    && ! grep -q "^20141206T150000Z${TAB}20141206T160000Z" "$FBFILE1" )) \
&& ( ! [ -e "$FBFILE2" ] \
  || ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE2" \
    && ! grep -q "^20141206T150000Z${TAB}20141206T160000Z" "$FBFILE2" )) \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is retracted).

   ! [ -e "$JOURNALFILE1" ] \
|| [ `grep "event25@example.com" "$JOURNALFILE1" | wc -l` = '2' ] \
&& echo "Success" \
|| echo "Failed"

# Modify the event and attempt to schedule it again.

  sed 's/FREQ=DAILY/FREQ=DAILY;COUNT=5/;' "$TEMPLATES/event-request-cars-recurring.txt" \
| sed "s/$SENDERADDRESS1/$SENDERADDRESS2/;" \
| "$OUTGOING_SCRIPT" $ARGS 2>> $ERROR

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

# Present the request to the resource.

  sed 's/FREQ=DAILY/FREQ=DAILY;COUNT=5/;' "$TEMPLATES/event-request-cars-recurring.txt" \
| sed "s/$SENDERADDRESS1/$SENDERADDRESS2/;" \
| "$RESOURCE_SCRIPT" $ARGS 2>> $ERROR \
| tee out4r.tmp \
| "$SHOWMAIL" \
> out4.tmp

# Since the email module used by showmail.py cannot stop after reading a single
# message, the second message is obtained.

  grep -n '^From ' out4r.tmp \
| tail -n 1 \
| cut -d ':' -f 1 \
| xargs -I{} tail -n +'{}' out4r.tmp \
| "$SHOWMAIL" \
>> out4.tmp

   grep -q 'METHOD:REPLY' out4.tmp \
&& [ `grep 'ATTENDEE.*;PARTSTAT=ACCEPTED' out4.tmp | wc -l` = '2' ] \
&& echo "Success" \
|| echo "Failed"

   [ -e "$FBFILE1" ] \
&& grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE1" \
&& grep -q "^20141130T150000Z${TAB}20141130T160000Z" "$FBFILE1" \
&& [ -e "$FBFILE2" ] \
&& grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE2" \
&& grep -q "^20141130T150000Z${TAB}20141130T160000Z" "$FBFILE2" \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is confirmed for both resources).

   [ -e "$JOURNALFILE2" ] \
&& [ `grep "event25@example.com" "$JOURNALFILE2" | wc -l` = '1' ] \
&& echo "Success" \
|| echo "Failed"
