#!/bin/sh

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

ACCEPT_SCRIPT="$THIS_DIR/test_handle.py"
ACCEPT_ARGS="accept $STORE_TYPE $STORE $JOURNAL $PREFS"

COUNTER_SCRIPT="$THIS_DIR/test_handle.py"
COUNTER_ARGS="counter $STORE_TYPE $STORE $JOURNAL $PREFS"

DECLINE_SCRIPT="$THIS_DIR/test_handle.py"
DECLINE_ARGS="decline $STORE_TYPE $STORE $JOURNAL $PREFS"

FREEBUSY_SCRIPT="$BASE_DIR/tools/make_freebusy.py"
FREEBUSY_ARGS="-s -n"
FREEBUSY_QUOTA_ARGS="-s -q"

LIST_SCRIPT="$THIS_DIR/list_table.py"
LIST_ARGS="$STORE_TYPE $STORE $JOURNAL"

OUTGOING_SCRIPT="$BASE_DIR/imip_person_outgoing.py"

PERSON_SCRIPT="$BASE_DIR/imip_person.py"

SET_DELEGATES="$BASE_DIR/tools/set_delegates.py"
SET_DELEGATES_ARGS="-T $STORE_TYPE -j $JOURNAL" 

SET_QUOTA_GROUPS="$BASE_DIR/tools/set_quota_groups.py"
SET_QUOTA_GROUPS_ARGS="-T $STORE_TYPE -j $JOURNAL" 

SET_QUOTA_LIMITS="$BASE_DIR/tools/set_quota_limits.py"
SET_QUOTA_LIMITS_ARGS="-T $STORE_TYPE -j $JOURNAL" 

TEXT_CLIENT="$BASE_DIR/imip_text_client.py"

TAB=`printf '\t'`

PYTHONPATH="$BASE_DIR"
export PYTHONPATH



# Helper functions.

failed() {
    # [ <filename> ]
    echo -n "Failed"
    if [ "$1" ]; then
        echo ": $1"
    else
        echo
    fi
}

success() {
    echo "Success"
}

# Output testing.

have_occurrences() {
    # <string> <number> [ <filename> ]
    if [ "$3" ]; then
        [ `grep "$1" "$3" | wc -l` = "$2" ]
    else
        [ `grep "$1" | wc -l` = "$2" ]
    fi
}

is_absent() {
    # <string> [ <filename> ]
    if [ "$2" ]; then
        ! grep -q "$1" "$2"
    else
        ! grep -q "$1"
    fi
}

is_present() {
    # <string> [ <filename> ]
    if [ "$2" ]; then
        grep -q "$1" "$2"
    else
        grep -q "$1"
    fi
}

# Specific operations.

freebusy_request() {
    # <filename> <scope> [ resource ]
    if [ "$2" ]; then
        SCOPE="-$2"
    else
        SCOPE=
    fi
    if [ "$3" = 'resource' ]; then
        resource_script "$TEMPLATES/fb-request$SCOPE.txt" "$1"
    else
        person_script "$TEMPLATES/fb-request-person$SCOPE.txt" "$1"
    fi
}

list_script() {
    # <filename> <arg>...
    FILENAME=$1
    shift 1
    "$LIST_SCRIPT" $LIST_ARGS $* > "$FILENAME"
}

outgoing_script() {
    # [ <filename> ]
    if [ "$1" ]; then
        "$OUTGOING_SCRIPT" < "$1" $ARGS 2>> $ERROR
    else
        "$OUTGOING_SCRIPT" $ARGS 2>> $ERROR
    fi
}

person_script() {
    # <filename> <output filename> [ <raw output filename> ]
    OUTFILE="$3"
      "$PERSON_SCRIPT" $ARGS < "$1" 2>> $ERROR \
    | tee $OUTFILE \
    | "$SHOWMAIL" \
    > "$2"
}

resource_script() {
    # <filename> <output filename> [ <raw output filename> ]
    OUTFILE="$3"
      "$RESOURCE_SCRIPT" $ARGS < "$1" 2>> $ERROR \
    | tee $OUTFILE \
    | "$SHOWMAIL" \
    > "$2"
}

showmail() {
    # <filename> [ <messages to skip> ]
    "$SHOWMAIL" $2 < "$1" 2>> $ERROR
}

test_freebusy() {
    # <start> <end> <filename>
    is_present "^$1${TAB}$2" "$3" && success || failed "$3"
}

test_freebusy_missing() {
    # <start> <end> <filename>
    is_absent "^$1${TAB}$2" "$3" && success || failed "$3"
}

text_client() {
    # <user> <command filename> [ <event filename> ]
    LOG=`basename "$2" .tmp`_log.tmp
    if [ "$3" ]; then
        "$TEXT_CLIENT" $ARGS -u "$1" -f "$3" --echo < "$2" > "$LOG"
    else
        "$TEXT_CLIENT" $ARGS -u "$1" --echo < "$2" > "$LOG"
    fi
}

text_client_specific() {
    # <user> <command filename> <uid> [ <recurrence-id> ]
    LOG=`basename "$2" .tmp`_log.tmp
    if [ "$4" ]; then
        "$TEXT_CLIENT" $ARGS -u "$1" --uid "$3" --recurrence-id "$4" --echo < "$2" > "$LOG"
    else
        "$TEXT_CLIENT" $ARGS -u "$1" --uid "$3" --echo < "$2" > "$LOG"
    fi
}

test_initial_freebusy() {
    # [ resource ]
    freebusy_request "init_fb.tmp" 'all' "$1"

    # Expect a reply with no periods.

       is_present 'METHOD:REPLY' "init_fb.tmp" \
    && is_absent '^FREEBUSY' "init_fb.tmp" \
    && success || failed "init_fb.tmp"
}

test_initial_freebusy_limited() {
    # [ resource ]
    freebusy_request "init_fb_limited.tmp" '' "$1"

    # Expect a reply with no periods.

       is_present 'METHOD:REPLY' "init_fb_limited.tmp" \
    && is_absent '^FREEBUSY' "init_fb_limited.tmp" \
    && success || failed "init_fb_limited.tmp"
}



# Test environment initialisation.

if [ "$STORE_TYPE" = "file" ]; then
    rm -rf "$STORE"
    rm -rf "$JOURNAL"
elif [ "$STORE_TYPE" = "postgresql" ]; then
    dropdb "$DBNAME"
    createdb "$DBNAME"
    psql -q -f "$BASE_DIR/conf/postgresql/schema.sql" "$DBNAME" 2>> $DBERROR
fi

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