#!/bin/sh
# ________________________________________________________________________
#
#  Copyright (C) 2014 Andrew Fullford
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
# ________________________________________________________________________
#

P=$(basename $0 .sh)

PYTHON=python

PYTHON_VERSION="$($PYTHON --version 2>&1)"
if [ "$?" -ne 0 ]; then
    echo $PYTHON_VERSION >&2
    exit 1
fi

usage()
{
    cat >&2 <<EOT
Usage: ./$P [-v] [-l] [-C] [-A] [-x] [-w addr] [-c certfile] role [...]

       Run the example configuration in the specified role.
       This must be run from within the example directory, ie:

            cd ..../taskforce/examples
        ./$P

       Log output will be to stderr.

       Flags:
    -h  This message.

    -v  Enable verbose logging in the example run.

    -l  Log to syslog

    -c  Override examples certificate file

    -C  Check the config and exit

    -A  Allow HTTP operations that can change the
        task state.

    -x  Cause simulated ntpd to exit randomly to
        demonstrate the 'timeset' restart.

    -w  Listen for HTTP on addr

EOT
    exit "${1:-99}"
}

TFARGS=
LOGDEST='--log-stderr'
while [ "$#" -gt 0 ]; do
    case "$1" in
    -v*)
        TFARGS="$TFARGS --verbose"
        ;;
    -l*)
        LOGDEST=''
        ;;
    -C*)
        TFARGS="$TFARGS --check-config"
        ;;
    -A*)
        TFARGS="$TFARGS --allow-control"
        ;;
    -c*)
        shift
        TFARGS="$TFARGS --certfile $1"
        ;;
    -w*)
        shift
        TFARGS="$TFARGS --http $1"
        ;;
    -x*)    MINSLEEP=20; export MINSLEEP
        SLEEPRANGE=20; export SLEEPRANGE
        ;;
    -h)
        usage 0
        ;;
    --)
        shift
        break
        ;;
    -*)
        echo "$P: Unknown flag '$1'" >&2
        usage
        ;;
    *)
        break
        ;;
    esac
    shift
done

if [ "$#" -eq 0 ]; then
    echo "$P: At least one role needed" >&2
    usage
fi

TFBASE="$(cd .. && echo $PWD)"
EXAMPLES_BASE="$TFBASE/examples"; export EXAMPLES_BASE
EXBIN="$EXAMPLES_BASE/bin"
EXCONF="$EXAMPLES_BASE/example.conf"
EXROLES="$EXAMPLES_BASE/example.roles"
if [ ! -x "$EXBIN/procsim" ]; then
    echo "$P: Script must be run from the 'taskforce/examples' directory" >&2
    exit 2
fi
if [ ! -r "$EXCONF" ]; then
    echo "$P: Example config '$EXCONF' is missing" >&2
    exit 2
fi
TFBIN="$TFBASE/bin"
if [ ! -x "$TFBIN/taskforce" ]; then
    echo "$P: 'taskforce' application not found in '$TFBIN'" >&2
    exit 2
fi
TFPKG="$TFBASE/taskforce"
if [ ! -f "$TFPKG/__init__.py" ]; then
    echo "$P: 'taskforce' package not found in '$TFPKG'" >&2
    exit 2
fi
if [ ! -d "$EXAMPLES_BASE/var/run" ]; then
    mkdir -p "$EXAMPLES_BASE/var/run" || exit 3
fi
PATH=$EXBIN:$PATH; export PATH
PYTHONPATH=$TFBASE:$PYTHONPATH; export PYTHONPATH

> $EXROLES || exit 3
while [ "$#" -gt 0 ]; do
    echo $1 >> $EXROLES
    shift
done

exec $PYTHON $TFBIN/taskforce $LOGDEST $TFARGS --config-file $EXCONF --roles-file $EXROLES
