#!/bin/sh

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

USER="mailto:resource-car-porsche911@example.com"
SENDER="mailto:paul.boddie@example.com"
FBFILE="$STORE/$USER/freebusy"
FBSENDERFILE="$STORE/$SENDER/freebusy"
QUOTA="$USER"
JOURNALFILE="$JOURNAL/$QUOTA/journal/$SENDER"

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

# Employ a user-specific quota (no argument with the functions above).

mkdir -p "$JOURNAL/$QUOTA"
echo '* PT10H' > "$JOURNAL/$QUOTA/limits"

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

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

# Present the request to the resource.

  "$RESOURCE_SCRIPT" $ARGS < "$TEMPLATES/event-request-car-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 "$FBFILE" ] \
|| ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE" \
  && ! grep -q "^20141206T150000Z${TAB}20141206T160000Z" "$FBFILE" ) \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is not confirmed).

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

# Modify the event and attempt to schedule it again.

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

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

# Present the request to the resource.

  sed 's/FREQ=DAILY/FREQ=DAILY;COUNT=11/;' "$TEMPLATES/event-request-car-recurring.txt" \
| "$RESOURCE_SCRIPT" $ARGS 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 "$FBFILE" ] \
|| ( ! grep -q "^20141126T150000Z${TAB}20141126T160000Z" "$FBFILE" \
  && ! grep -q "^20141205T150000Z${TAB}20141205T160000Z" "$FBFILE" \
  && ! grep -q "^20141206T150000Z${TAB}20141206T160000Z" "$FBFILE" ) \
&& echo "Success" \
|| echo "Failed"

# Check the quota (event is confirmed).

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

# Modify the event and attempt to schedule it again.

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

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

# Present the request to the resource.

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

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

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

# Check the quota (event is confirmed).

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