#!/bin/bash
#
# storageAPI - this script starts and stops the storageAPI daemon
#
# chkconfig:   - 85 15 
# description:  storageAPI provides a REST API for storage operations
# processname: python
# pidfile:     /var/run/storageAPI.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Source Onapp vars
if [ -e /etc/onapp.conf ]; then
    . /etc/onapp.conf
fi

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

APIutil="/usr/pythoncontroller/storageAPI.py"
prog=$(basename $APIutil)

lockfile=/var/lock/subsys/cputils

start() {
    [ -x $APIutil ] || exit 5
    echo -n $"Starting $prog: "
    ARGS=""
    if [ ! -z $HOST ]; then ARGS="-s $HOST"; fi
    daemon $APIutil $ARGS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    if [ $retval -eq 0 ]; then
        if [ "$CONSOLETYPE" != "serial" ]; then
           echo -en "\\033[16G"
        fi
        while rh_status_q
        do
            sleep 1
            echo -n $"."
        done
        rm -f $lockfile
    fi
    echo
    return $retval
}

restart() {
    stop
    start
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
	    ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart}"
        exit 2
esac
