#!/bin/sh
##########################################################################
#
#   AGS Sentinel Server 6 control script for UNIX.
#   (c) 2003-2008 Advanced Global Systems, Ltd.
#
##########################################################################
#
# Use this script to start, stop and control Sentinel Server manually 
# from UNIX command line and in "init" scripts. 
#
# The usage is:  sentinel [start|stop|restart|status|exstatus]
#
# Initial values of configurable parameters in this file are set 
# by the installer on UNIX. If you have since then moved Sentinel 
# to another directory or machine, or if you copied Sentinel from 
# Windows, you may have to set SENTINEL_HOME and JAVA parameters 
# below manually. 

#
#    AGS_HOME
#
#    AGS home directory - the directory which contains "sentinel" folder.
#    /opt/AGS by default.     
#
AGS_HOME=$HOME/AGS


#
#    JAVA
#
#    Java interpreter executable (e.g. /usr/bin/java). 
#    The initial value is set by installer based on automatically 
#    discovered or manually chosen VM. 
#    This must be JRE version 1.8.0 or 1.7.0    
#
JAVA=java


# SENTINEL_PORT
#     Port on which service listens
#
SENTINEL_PORT=10556

#===============================================================
# DO NOT EDIT BELOW THIS LINE
#===============================================================
SERVICE="AGS Sentinel Server"
SENTINEL_HOME=$AGS_HOME/sentinel

set_paths() 
{
   CONFIGFILE=sentinel.cfg
   PIDFILE=$SENTINEL_HOME/sentinel.pid
   LOGFILE=$SENTINEL_HOME/logs/sentinel.log
   CPATH="$SENTINEL_HOME:$SENTINEL_HOME/lib/scm.jar:$SENTINEL_HOME/lib/ifxjdbc.jar:$SENTINEL_HOME/lib/derby.jar:$SENTINEL_HOME/lib/hsqldb.jar:$SENTINEL_HOME/lib/cryptix.jar:$SENTINEL_HOME/lib/mail.jar:$SENTINEL_HOME/lib/activation.jar:$SENTINEL_HOME/lib/jta.jar"
   if [ "z$CLASSPATH" != "z" ]; then 
       CPATH=$CPATH:$CLASSPATH
   fi
   CLASSPATH=$CPATH
   export CLASSPATH
}

#
# Validates environment, including JVM presense, sanity and version. 
# If something is not acceptable, writes error message into stderr and
# exits script with non-zero exit code. 
#
validate_env() {
    
    script_java=${JAVA}

    case $JAVA in
    @*) 
        echo "WARNING: JAVA variable in script '$0' is not set. Trying 'java'" >&2
        JAVA=java
        ;;
    esac

    case $AGS_HOME in
    @*)
        echo "ERROR:" >&2
        echo "  AGS_HOME variable in script '$0' is not set !" >&2
        echo "  please edit the script and set this variable." >&2
        exit 1
        ;;
    esac

    SENTINEL_HOME=$AGS_HOME/sentinel
    export SENTINEL_HOME
    script_home=$SENTINEL_HOME
    
    jv=`$JAVA -version 2>&1`
    if [ $? != 0 ]; then
       echo "ERROR: " >&2
       echo "  JAVA variable in script '$0' ('${script_java}') does not point to a valid JVM" >&2
       echo "  please edit the script and set this variable." >&2
       exit 1
    fi
    
    version=`echo $jv | sed '1,1s/^java *version *"*\([_0-9.][_0-9.]*\).*/\1/g;q'`
    case $version in
    1.[1-3]*)
        echo "ERROR:" >&2
        echo "  $SERVICE requires JRE version 1.4 or newer." >&2
        echo "  Your JRE ($JAVA) is $version" >&2
        echo "  Please change JAVA parameter in the script '$0'" >&2
        echo "  or contact your operating system vendor for a newer version" >&2 
        echo "  of Java Runtime Environment (JRE)." >&2
        exit 1
        ;;
    *)
        ;;
    esac
    
    set_paths
    
}


t=`echo -n "test\c"`
case $t in
"-n test")
    echo="echo"
    eol="\c"    
    ;;
*) 
    echo="echo -n"
    eol=""
    ;;
esac

is_running() {
    export CLASSPATH
    $JAVA com.agsltd.sentinel.SentinelProbe -port $SENTINEL_PORT -q -ping >/dev/null 2>&1
    return $?
}

is_stopped() {
    if is_running >/dev/null 2>&1 ; then 
       return 1
    else
       return 0
    fi
}


stop_service() {
    
    $echo "stopping ${SERVICE}... $eol"
    if is_stopped; then
        echo "$SERVICE is not running"
        return 0
    fi
    pid=
    if [ -f ${PIDFILE} ]; then 
        pid=`cat ${PIDFILE}`
    fi
    export CLASSPATH
    $JAVA launcher -root "$AGS_HOME" -port $SENTINEL_PORT -shutdown
    if [ -f ${PIDFILE} ]; then 
        rm -f ${PIDFILE}
    fi
    echo "done"
    
}

start_service() {
    
    $echo "starting ${SERVICE}... $eol"
    if is_running; then
        echo "$SERVICE is already running: "
        echo "-------------------------------------"
        show_ex_status
        return 1
    fi

    if [ -f $SENTINEL_HOME/config/$CONFIGFILE ]; then
       memory=`awk -F= '/^[ \t]*MEMORY[ \t]*=[ \t]*[0-9]/{print $2;}' $SENTINEL_HOME/config/$CONFIGFILE | sed 's/^[ \t]*\([0-9][0-9]*\).*$/\1/g'`
       if [ $? = 0 ]; then
          if [ "z${memory}" != "z" ]; then
             jopt="-Xmx${memory}m"
          fi
       fi
    fi
    
    rm -f ${PIDFILE}
    
    export CLASSPATH
    echo $JAVA $jopt -classpath $CLASSPATH launcher -root "$AGS_HOME" -port $SENTINEL_PORT >"$LOGFILE"
    $JAVA $jopt -classpath $CLASSPATH launcher -root "$AGS_HOME" -port $SENTINEL_PORT >>"$LOGFILE" 2>&1 &
    pid=$! 
    javarc=$?
    if [ $javarc -ne 0 ]; then
	echo
        echo "$0:"
        echo "Could not start $SERVICE."
        echo "Please examine log file $LOGFILE."
        exit $javarc
    fi
    $echo "${pid}${eol}" > ${PIDFILE} 
    sleep 5
    kill -0 $pid >/dev/null 2>&1
    rc=$? 
    if [ $rc -ne 0 ]; then
	echo
	echo "$0:"
	echo "$SERVICE has started, but then stopped."
	echo "This most likely indicates problem with configuration or environment."
        echo "Please examine log file $LOGFILE."
	exit $rc
    fi
    echo "done (pid=${pid})."

}


show_status() {
    if is_stopped; then
        echo "$SERVICE is not running"
        return 0
    fi
    export CLASSPATH
    $JAVA com.agsltd.sentinel.SentinelProbe -port $SENTINEL_PORT
    if [ -f $PIDFILE ]; then
        pid=`cat $PIDFILE`
        echo "Server PID: $pid"
    fi
    return 0
}

show_ex_status() {
    if is_stopped; then
        echo "$SERVICE is not running"
        return 0
    fi
    export CLASSPATH
    $JAVA com.agsltd.sentinel.SentinelProbe -port $SENTINEL_PORT -config
    return 0
}

x_which() {
    return 0
}

case $1 in

'start')
    validate_env
    start_service
    ;;

'stop')
    validate_env
    stop_service
    ;;

'restart')
    validate_env
    stop_service
    start_service
    ;;
    
'status')
    validate_env
    show_status
    ;;

'ping')
    validate_env
    is_running
    ;;
    
'exstatus')
    validate_env
    show_ex_status
    ;;

*)
    echo "usage:"
    echo "  $0 {start|stop|restart|status|exstatus}"
    ;;
esac
