#!/bin/bash
# chkconfig: 2345 65 37

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

#Start return codes:
#0 - ok
#1 - error

#Stop return codes:
#0 - ok
#1 - error

#Status return codes:
#0 - running
#1 - stopped
#2 - error
#3 - error

#Restart return codes:
#0 - running
#1 - error during stop
#2 - error during start

#General return codes:
#4 - error

daemon="pod-facade"
pidfile="/var/run/pod-facade.pid"

echo `date +'%F %T.%N'` $daemon $1 >>/tmp/thirdlane_daemons.log

function start {
        status start_stop
        rc=$?
        if [ "$rc" -eq "0" ]
        then
                echo "$daemon is already running"
                return 0
        fi
        if [ "$rc" -ne "1" ]
        then
                return 1
        fi

        echo -n $"Starting $daemon: "

        /usr/local/sbin/tcfctl start facade > /dev/null 2>&1

        sleep 2

        pstatus=`pgrep tcf::facade | wc -l`
        if [ "$pstatus" -eq "1" ]
        then
                echo "[OK]"
                echo $pid > $pidfile
                rc=0
        else
                echo "failed. Make sure $daemon is not running and execute this command with parameter \"stop\" or \"restart\""
                rc=1
        fi
        return $rc
}

function stop {
        pid=`ps -aefw | grep "tcf::facade" | grep -v "grep" | awk '{print $2}'`
        if [ "$pid" != "" ]
        then
                /usr/local/sbin/tcfctl stop facade > /dev/null 2>&1
                sleep 3
        fi
        rm -rf $pidfile
        status start_stop
        rc=$?
        if [ "$rc" -eq "1" ]
        then
                return 0
        else
                return 1
        fi
}

function status {
        if [ -f $pidfile ]
        then
                pid=`cat $pidfile`
                if [ "$pid" == "" ]
                then
                        output="$daemon is stopped"
                        rc=1
                fi
                pstatus=`pgrep tcf::facade | wc -l`
                case $pstatus in
                        0)
                                output="$daemon is dead but pid file exists"
                                rc=1
                                ;;
                        1)
                                output="$daemon is running"
                                rc=0
                                ;;
                        *)
                                output="General error. Make sure $daemon is not running and execute this command with parameter \"stop\" or \"restart\""
                                rc=2
                                ;;
                esac
        else
                output="$daemon is stopped"
                rc=1
        fi

        if [ "$1" != "start_stop" ]
        then
                echo $output
        fi

        return $rc
}

function restart {
        stop
        if [ "$?" -ne "0" ]
        then
                return 1
        fi
        start
        if [ "$?" -ne "0" ]
        then
                return 2
        fi
}


case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        status)
                status
                ;;
        restart)
                restart
                ;;
        *)
                echo $"Usage: $0 {start|stop|status|restart}"
                exit 4
esac


exit $?