#!/bin/bash

# Start and stop the SkyForm AIP service
# Run by hand or install in /etc/init.d
# and make a symbolic link from rc3.d to it.
#
# The following is for the Linux chkconfig utlility
# chkconfig: 35 99 01
# description: AIP

### BEGIN INIT INFO
# Provides: AIP
# Required-Start: $local_fs $remote_fs $network $syslog 
# Required-Stop: $local_fs $remote_fs $syslog $network
# Default-Start:  3 4 5
# Default-Stop: 0 1 6
# Short-Description: aip
# Description: AIP is a resource manager for AI applications
### END INIT INFO

# This is the root of the AIP installation
# change it if installed somewhere else.
export AIP_TOP=/cubetop
export CB_ENVDIR=$AIP_TOP/etc
export LANG=en_US.UTF-8

if [ -f $CB_ENVDIR/limits.sh ]; then
    . $CB_ENVDIR/limits.sh
fi

# kill the old daemons first.
#
kill_daemons()
{
    killall -q cbls cbexe cbjm cbsched cbcrond
    killall -q -9 cbjm
}

check_ret_code()
{
   if [ -z "$PID" ]; then
       err=1
   fi
}

do_exit()
{
   if [ "$err" == "1" ]; then
       exit 1
   fi
   exit 0
}
# Restart is just like start since in both
# cases previously running daemons are 
# killed.
opcode="$1"
if [ "$opcode" = "restart" ]
then 
   opcode="start"
   echo "Stopping SkyForm AIP service..."
fi

# Process the requested operation.
case "$opcode" in
  'stop')
        echo "Stopping SkyForm AIP service..."
        kill_daemons
        exit 0
        ;;

  'start')
        export CB_ENVDIR
        export CB_SERVERDIR=$AIP_TOP/sbin
        if [ -d $CB_ENVDIR ]
        then
            kill_daemons

            # Get the location of the SkyForm AIP daemons

            if [ -f $CB_ENVDIR/cb.conf ]; then
               . $CB_ENVDIR/cb.conf
            fi
            echo "Starting SkyForm AIP service..."

	    ID=`id -u`
            if [ "$ID" != 0 ]; then
            # single user mode use this
            # to run test clusters
               $CB_SERVERDIR/cbls -1
               $CB_SERVERDIR/cbexe -1
               $CB_SERVERDIR/cbjm -1
               echo "AIP daemons started in single user mode"
            else
               # multi user mode
               $CB_SERVERDIR/cbls
               $CB_SERVERDIR/cbexe
               $CB_SERVERDIR/cbjm
               echo "AIP daemons started"
            fi
            exit 0
        fi
        echo "$CB_ENVDIR: no such file or directory"
        exit 1
        ;;
  'status')
        err=0
        PID=`pidof cbls`
        check_ret_code
        echo "AIP daemon cbls: <$PID>"
        PID=`pidof cbexe`
        check_ret_code
        echo "AIP daemon cbexe: <$PID>"
        PID=`pidof cbjm`
        check_ret_code
        echo "AIP daemon cbjm: <$PID>"
        check_ret_code
        PID=`pidof cbsched`
        if [ _"$PID" != '_' ]; then
            echo "AIP daemon cbsched: <$PID>"
        fi
        PID=`pidof cbcrond`
        if [ _"$PID" != '_' ]; then
            echo "AIP daemon cbcrond: <$PID>"
        fi
        do_exit
        ;;
  *)
        echo "Script for starting up and shutting down SkyForm AIP"
        echo "Usage: $0 { start | stop | status | restart }"
        exit 0
        ;;
esac

