#! /bin/sh
# chkconfig: 235 98 98
# description: Starts and stops the Cassandra Server

### BEGIN INIT INFO
# Provides:             Cassandra_Server
# Required-Start:       
# Required-Stop:        
# Default-Start:        2 3 5
# Default-Stop:         0 1 6
# Short-Description:    Cassandra_Server
### END INIT INFO

# To run as a service run:
# chkconfig --add cassandra

PRODUCT_DESC="Cassandra_Server"
INSTDIR="/opt/cassandra"
USER="root"
CONFIG_FILE="$INSTDIR/config/cassandra"
PIDFILE="$INSTDIR/bin/cassandra.pid"
STARTUP_LOG="$INSTDIR/logs/startup.log"
JAVA_HOME="/opt/Axway-7.5.2/apigateway/platform/jre"

# Read configuration file if it is present
[ -r "$CONFIG_FILE" ] && . "$CONFIG_FILE"

DAEMON="$INSTDIR/bin/cassandra"
SCRIPTNAME=/etc/init.d/cassandra

if [ -f /etc/rc.d/init.d/functions ]
then
    . /etc/rc.d/init.d/functions
    HAVE_FUNS=true
else
    success() {
        echo -n " success"
    }

    failure() {
        echo -n " fail"
    }

fi

# Exit with failure if the package is not installed
if [ ! -x "$DAEMON" ]; then
    echo -n "$PRODUCT_DESC not found "
    failure; echo
    echo "Looking for:"
    echo "    $DAEMON"
    echo "Please ensure that the path above is correct."
    echo "You may need to modify the value of INSTDIR in:"
    echo "    $0    or"
    echo "    $CONFIG_FILE"
    exit 1
fi

mkdir -p $INSTDIR/logs

do_status() {
    [ -f $PIDFILE ] && pgrep -f $(basename $0) | grep -q $(cat $PIDFILE)
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        echo "$PRODUCT_DESC service is running with pid $(cat $PIDFILE)"
    else
        echo -n "$PRODUCT_DESC is not running"
        RETVAL=1
        pgrep -f CassandraDaemon > /dev/null
        if [ $? -eq 0 ]; then
            RETVAL=2
            echo " with service PID read from $PIDFILE."
            echo -en "Cassandra running with PID\n$(pgrep -f CassandraDaemon)"
        fi
        echo ""
    fi
    return $RETVAL
}
        
do_start() {
    echo -n "Starting the $PRODUCT_DESC service"    
    if [ x"$USER" = x"" -o x"$USER" = x"`id -nu`" ]; then
        "$DAEMON" -p $PIDFILE > $STARTUP_LOG 2>&1
    else
        su $USER -c "\"$DAEMON\" -p \"$PIDFILE\" " > $STARTUP_LOG 2>&1
    fi
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    success; echo
        echo "Cassandra server started with process id: $(cat $PIDFILE)"
	else
	    failure; echo
        echo "Please see $STARTUP_LOG for more details"
	fi
	return $RETVAL
}

do_stop() {
    do_status || return 1
    echo -n "Stopping the $PRODUCT_DESC service"
    if [ -f $PIDFILE ]; then    	
    	kill $(cat $PIDFILE)
    fi
    i=0
    #while pgrep -f $(basename $0) | $CASSPID > /dev/null; do    
    while [ -f $PIDFILE ]; do
        echo -n "." && sleep 1
        i=$[$i+1]
        if [ $i -eq 60 ]; then
            failure; echo
            return 1
        fi
    done
    success; echo
    return 0
}

case "$1" in
    start)
        # check it is not already running
        [ -f $PIDFILE ] && pgrep -f $(basename $0) | grep -q $(cat $PIDFILE)
        if [ $? -eq 0 ]; then
            echo "$PRODUCT_DESC is already running with pid $(cat $PIDFILE)"
            exit 1
        fi
        do_start
        exit $?
        ;;
    stop)
        do_stop
        exit $?
        ;;
    status)
        do_status
        exit $?
        ;;
    restart)
        do_stop
        sleep 5
        do_start
        exit $?
        ;;    
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
        exit 3
        ;;
esac
