#!/bin/sh

THIS_DIR=`dirname $0`

TEMPLATES="$THIS_DIR/templates"
PERSON_SCRIPT="$THIS_DIR/../imip_person.py"
SHOWMAIL="$THIS_DIR/../tools/showmail.py"
STORE=/tmp/store
STATIC=/tmp/static
PREFS=/tmp/prefs
ARGS="-S $STORE -P $STATIC -p $PREFS -d"
USER="mailto:paul.boddie@example.com"
SENDER="mailto:resource-room-confroom@example.com"
FBFILE="$STORE/$USER/freebusy"
FBOTHERFILE="$STORE/$USER/freebusy-other/$SENDER"
TAB=`printf '\t'`

PYTHONPATH="$THIS_DIR/.."
export PYTHONPATH

ERROR=err.tmp

rm -rf $STORE
rm -rf $STATIC
rm -rf $PREFS
rm -f $ERROR
rm -f out*.tmp

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

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/fb-publish.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out0.tmp

   grep -q "^20140401T070000Z${TAB}20140401T080000Z" "$FBOTHERFILE" \
&& grep -q "^20140401T080000Z${TAB}20140401T100000Z" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"

  "$PERSON_SCRIPT" $ARGS < "$TEMPLATES/fb-publish-again.txt" 2>> $ERROR \
| "$SHOWMAIL" \
> out0.tmp

   grep -q "^20140401T070000Z${TAB}20140401T080000Z" "$FBOTHERFILE" \
&& ! grep -q "^20140401T080000Z${TAB}20140401T100000Z" "$FBOTHERFILE" \
&& grep -q "^20140401T083000Z${TAB}20140401T100000Z" "$FBOTHERFILE" \
&& echo "Success" \
|| echo "Failed"
