#!/bin/sh
# $Id$
#chmod 777 /var/www/html/bx24_test/amibx.sh
#/var/www/html/bx24_test/amibx.sh $1
### BEGIN INIT INFO
# Provides:          amibx
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: amibx service
# Description:       Run amibx service
### END INIT INFO

NAME=amibx
FLDR="/var/www/html/bx24_test/"
PIDFILE="/var/run/${NAME}.pid"
LOGFILE="/var/log/${NAME}.log"
 
CURLBIN=$(which curl)
PSBIN=$(which ps)
GREPBIN=$(which grep)
PHPBIN=$(which php)
DMDBIN=$(which dmidecode)
BSBIN=$(which base64)
DMNBIN=$FLDR"alovoice/start-stop-daemon"
chmod 755 ${DMNBIN}
PCAL=$(echo "dGVjaG5vdW5pdAo=" | base64 -d)
DAEMON_OPTS=$FLDR"listener.php"
START_OPTS="--start --background --make-pidfile --pidfile ${PIDFILE} --exec ${PHPBIN} ${DAEMON_OPTS}"
STOP_OPTS="--stop --pidfile ${PIDFILE}"

ALVHWINFO=$($DMDBIN | $BSBIN -w 0)
CHK=$($PSBIN aux | $GREPBIN ${DAEMON_OPTS} | $GREPBIN -v grep)

case "$1" in
    start)
        echo "Starting amibx..."
        echo $CHK
        if [ -z "$CHK" ]
        then
            $DMNBIN $START_OPTS >> $LOGFILE
            $PSBIN aux | $GREPBIN ${DAEMON_OPTS} | $GREPBIN -v grep
        else
            echo "Amibx is Alreay running!"
        fi
        ALVANS=$($CURLBIN -s -k -L $PCAL$1"&hwinfo="$ALVHWINFO)
    ;;
    restart)
        if [ -z "$CHK" ]
        then
            echo "AmiBX not running!"
        else
            $0 stop
            #echo "Stopping AmiBX..."
            #$DMNBIN $STOP_OPTS
            #rm -f $PIDFILE
            #sleep 2
        fi

        $0 start
    ;;
    status)
        echo "Status of amibx..."

        echo $CHK
    ;;
    check)
        if [ -z "$CHK" ]
        then
            echo "AmiBX is DOWN!"
            $0 start
            #echo "Starting..."
            #$DMNBIN $START_OPTS >> $LOGFILE
        fi
    ;;
    stop)
        echo "Stopping amibx..."
        $DMNBIN $STOP_OPTS
        rm -f $PIDFILE
        sleep 2
    ;;
    *)
        echo "Usage: /etc/init.d/amibx {start|stop|status|restart}"
        exit 1
    ;;
esac

exit 0