#!/bin/bash
# manage the credential server
# $Id: fl-credential-ctl 24514 2005-08-25 15:54:36Z bdelbosc $

if [ -n "$FL_HOME" ]; then
    FL_SRC=$FL_HOME/funkload
    echo "### Using src in $FL_SRC"
else
    FL_SRC=`python -c "import funkload; print funkload.__path__[0]"` || exit -1
fi

start() {
    echo "### fl-credential-ctl: Starting credential server."
    (python $FL_SRC/credentiald.py $conf_path  >> $LOGFILE 2>&1 || echo "### fl-credential-ctl: ERROR server not started use fl-credential-ctl CONF_PATH log for more info") &
    # let the server starts
    sleep 1
}

startd() {
    echo "### fl-credential-ctl: Starting credential server in debug mode:"
    python $FL_SRC/credentiald.py $conf_path
}


stop() {
    echo "### fl-credential-ctl: Stopping credential server."
    python $FL_SRC/credentialctl.py $conf_path stop
}

test_server() {
    echo "### fl-credential-ctl: Testing credential server:"
    python $FL_SRC/credentialctl.py $conf_path test
}

status() {
    echo "### fl-credential-ctl: Checking credential server status:"
    python $FL_SRC/credentialctl.py $conf_path status
}

reload() {
    echo "### fl-credential-ctl: Checking credential server reload:"
    python $FL_SRC/credentialctl.py $conf_path reload
}

usage() {
    echo "Usage: $0 CONF_PATH {start|startd|stop|restart|status|log}"
}

LOGFILE=credentiald.log

if [ ! -f $1 ]; then
    usage
    exit 1
fi

conf_path=$1
shift

case "$1" in
    start)
        start;;
    startd)
        startd;;
    stop)
        stop;;
    status)
        status;;
    reload|force-reload)
        reload
        status;;
    restart)
        stop
        sleep 5
        start
        status
        ;;
    test|check)
        test_server
        ;;
    log)
        echo "### fl-credential-ctl: tail -f $LOGFILE"""
        tail -f $LOGFILE
        ;;
    *)
        usage
        exit 1
        ;;
esac

