#!/bin/sh

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

USER1="mailto:resource-car-porsche911@example.com"
USER2="mailto:resource-car-fiat500@example.com"
SENDER="mailto:paul.boddie@example.com"
FBFILE1="$STORE/$USER1/freebusy"
FBFILE2="$STORE/$USER2/freebusy"
FBSENDERFILE="$STORE/$SENDER/freebusy"
QUOTA=cars
JOURNALFILE="$JOURNAL/$QUOTA/journal/$SENDER"

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"
echo '* PT1H' > "$JOURNAL/$QUOTA/limits"

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/fb-request-car.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-car.txt" 2>> $ERROR

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

# Present the request to the resource.

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

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

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

# Check the quota (event is confirmed).

   [ -e "$JOURNALFILE" ] \
&& grep -q "event21@example.com" "$JOURNALFILE" \
&& echo "Success" \
|| echo "Failed"

# Attempt to schedule another event.

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-car-conflict.txt" 2>> $ERROR

   grep -q "^20141126T153000Z${TAB}20141126T163000Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-car-conflict.txt" 2>> $ERROR \
| tee out2r.tmp \
| "$SHOWMAIL" \
> out2.tmp

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

   ! [ -e "$FBFILE2" ] \
|| ! grep -q "^20141126T153000Z${TAB}20141126T163000Z" "$FBFILE2" \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is not confirmed).

   [ -e "$JOURNALFILE" ] \
&& grep -q "event21@example.com" "$JOURNALFILE" \
&& ! grep -q "event22@example.com" "$JOURNALFILE" \
&& echo "Success" \
|| echo "Failed"

# Increase the quota.

echo '* PT2H' > "$JOURNAL/$QUOTA/limits"

# Attempt to schedule the event again.

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-car-conflict.txt" 2>> $ERROR

   grep -q "^20141126T153000Z${TAB}20141126T163000Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-car-conflict.txt" 2>> $ERROR \
| tee out3r.tmp \
| "$SHOWMAIL" \
> out3.tmp

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

   [ -e "$FBFILE2" ] \
&& grep -q "^20141126T153000Z${TAB}20141126T163000Z" "$FBFILE2" \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is confirmed).

   [ -e "$JOURNALFILE" ] \
&& grep -q "event21@example.com" "$JOURNALFILE" \
&& grep -q "event22@example.com" "$JOURNALFILE" \
&& echo "Success" \
|| echo "Failed"

# Cancel the first event.

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

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

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-cancel-car.txt" 2>> $ERROR \
| tee out4r.tmp \
| "$SHOWMAIL" \
> out4.tmp

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

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

# Check the quota (event is retracted).

   [ -e "$JOURNALFILE" ] \
&& [ `grep "event21@example.com" "$JOURNALFILE" | wc -l` = '2' ] \
&& grep -q "event22@example.com" "$JOURNALFILE" \
&& echo "Success" \
|| echo "Failed"

# Add collective scheduling tests.

cat > "$PREFS/$USER1/scheduling_function" <<EOF
schedule_in_freebusy
schedule_across_quota $QUOTA
check_quota $QUOTA
EOF
cat > "$PREFS/$USER1/confirmation_function" <<EOF
add_to_quota_freebusy $QUOTA
add_to_quota $QUOTA
EOF
cat > "$PREFS/$USER1/retraction_function" <<EOF
remove_from_quota_freebusy $QUOTA
remove_from_quota $QUOTA
EOF

cat > "$PREFS/$USER2/scheduling_function" <<EOF
schedule_in_freebusy
schedule_across_quota $QUOTA
check_quota $QUOTA
EOF
cat > "$PREFS/$USER2/confirmation_function" <<EOF
add_to_quota_freebusy $QUOTA
add_to_quota $QUOTA
EOF
cat > "$PREFS/$USER2/retraction_function" <<EOF
remove_from_quota_freebusy $QUOTA
remove_from_quota $QUOTA
EOF

# Remind the resource about the second event.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-car-conflict.txt" 2>> $ERROR \
| tee out5r.tmp \
| "$SHOWMAIL" \
> out5.tmp

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

   [ -e "$FBFILE2" ] \
&& grep -q "^20141126T153000Z${TAB}20141126T163000Z" "$FBFILE2" \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is still confirmed).

   [ -e "$JOURNALFILE" ] \
&& [ `grep "event21@example.com" "$JOURNALFILE" | wc -l` = '2' ] \
&& [ `grep "event22@example.com" "$JOURNALFILE" | wc -l` = '1' ] \
&& echo "Success" \
|| echo "Failed"

# Attempt to schedule the first event.

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

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

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-car.txt" 2>> $ERROR \
| tee out6r.tmp \
| "$SHOWMAIL" \
> out6.tmp

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

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

# Check the quota (event is still retracted and not newly confirmed).

   [ -e "$JOURNALFILE" ] \
&& [ `grep "event21@example.com" "$JOURNALFILE" | wc -l` = '2' ] \
&& [ `grep "event22@example.com" "$JOURNALFILE" | wc -l` = '1' ] \
&& echo "Success" \
|| echo "Failed"

# Attempt to schedule the first event moved earlier.

"$OUTGOING_SCRIPT" $ARGS < "$TEMPLATES/event-request-car-moved.txt" 2>> $ERROR

   ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBSENDERFILE" \
&& grep -q "^20141126T143000Z${TAB}20141126T153000Z" "$FBSENDERFILE" \
&& echo "Success" \
|| echo "Failed"

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-car-moved.txt" 2>> $ERROR \
| tee out7r.tmp \
| "$SHOWMAIL" \
> out7.tmp

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

   grep -q "^20141126T143000Z${TAB}20141126T153000Z" "$FBFILE1" \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is newly confirmed).

   [ -e "$JOURNALFILE" ] \
&& [ `grep "event21@example.com" "$JOURNALFILE" | wc -l` = '3' ] \
&& [ `grep "event22@example.com" "$JOURNALFILE" | wc -l` = '1' ] \
&& echo "Success" \
|| echo "Failed"
