#!/bin/sh
# Copyright(c) 2022, Magustek Co. Ltd.
# rtnnctl    Startup script for openPlant Server
# chkconfig: - 90 10
# description: openPlant Server
# processname: openplant
#
### BEGIN INIT INFO
# Provides: magustek
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: openPlant Server
### END INIT INFO
#
# for Solaris:
# ln -s /home/sis/openplant/rtnnctl /etc/rc3.d/S90rtnn
# ln -s /home/sis/openplant/rtnnctl /etc/rc0.d/K01rtnn
#
# for Redhat Linux:
# ln -s /home/sis/openplant/rtnnctl /etc/init.d/openplant
# chkconfig --add openplant
# chkconfig openplant on
#
#

PATH="/usr/sbin:/usr/bin:/sbin:/bin:/usr/ucb:/usr/ccs/bin"; export PATH
LD_LIBRARY_PATH="/usr/lib:/usr/local/lib:/usr/sfw/lib"; export LD_LIBRARY_PATH

# resolve links - $0 may be a softlink
PRG="$0"
while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

PRGDIR=`dirname "$PRG"`
PRGDIR=`cd $PRGDIR;pwd`
LD_LIBRARY_PATH=$PRGDIR/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH

OPHome="$PRGDIR"
OPDaemon="rtnn"
OPCmd=$OPHome/$OPDaemon
pid_file=$OPHome/data/`hostname`.pid

rval=0

#
# killproc by name like pkill   
#
killproc() {            # kill the named process(es)
        pid=`ps -ef | grep -w $1 | awk '{if ($0!~/grep/) print $2}'`
        [ "$pid" != "" ] && kill $pid
}

case `echo "testing\c"`,`echo -n testing` in
    *c*,-n*) echo_n=   echo_c=     ;;
    *c*,*)   echo_n=-n echo_c=     ;;
    *)       echo_n=   echo_c='\c' ;;
esac


# for HP-UX patch
OS_TYPE=`uname`
if [ "$OS_TYPE" = "HP-UX" ]; then
    alias ps="UNIX95= /usr/bin/ps"
fi


# Start/stop processes

op_start() {
    RETVAL=0
    # test if pid_file exists and has a size 
    if test -s $pid_file
    then
        pid=`cat $pid_file`
        output=`ps -p $pid -o args | grep $OPDaemon`
        if [ -n "$output" ]
        then
            echo "$OPDaemon is running with pid $pid"
            return 0;
        fi
    fi

    if [ -x $OPCmd ]; then
        if [ "$OS_TYPE" = "HP-UX" ]; then
            ${OPCmd} -d
        else
            $OPCmd &
        fi
        RETVAL=1
    else
        echo "$OPCmd is not executable"
    fi
    return $RETVAL;
}


op_stop() {
    if test -s $pid_file
    then
        pid=`cat $pid_file`
        output=`ps -p $pid -o args | grep $OPDaemon`
        if [ -n "$output" ]
        then
            kill $pid
            [ $? -ne 0 ] && return 0;
            
            # wait 30s for OPDaemon to exit
            echo "Wait for openplant to exit "
            sleep 1
            while [ -s $pid_file -a "$flags" != "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ]
            do
                echo $echo_n ".$echo_c"
                flags=a$flags
                sleep 1
            done
            if [ -s $pid_file ]; then 
                echo "gave up waiting!"
            else
                echo "done"
            fi
        else
            echo "No $OPDaemon is running with pid $pid. Looked for $pid_file."
        fi
    fi
}

op_status() {
    # test if pid_file exists and has a size 
    if test -s $pid_file
    then
        pid=`cat $pid_file`
        output=`ps -p $pid -o args | grep $OPDaemon`
        if [ -n "$output" ]
        then
            echo "$OPDaemon is running with pid $pid"
            return 1;
        fi
    fi
    return 0;
}


op_install() {
    if test -s /etc/init.d/openplant; then
        echo openplant is already installed
    else 
        SELF=$PRGDIR/`basename $0`
        ln -s $SELF /etc/init.d/openplant

        # add service
        if which chkconfig >/dev/null 2>&1; then
            chkconfig --add openplant
            chkconfig openplant on
        elif which update-rc.d >/dev/null 2>&1; then
            update-rc.d openplant defaults
        else
            ln -s /etc/init.d/openplant /etc/rc3.d/S90openplant
            ln -s /etc/init.d/openplant /etc/rc0.d/K10openplant
        fi
        if [ $? -eq 0 ]; then
            echo install ok
        fi 
    fi
}

case "$1" in
'start_msg')
    echo "Starting openPlant Server"
    ;;
'stop_msg')
    echo "Stopping openPlant Server"
    ;;
'start')
    op_start
    ;;
'stop')
    op_stop
    ;;
'status')
    op_status
    ;;
'install')
    op_install
    ;;
*)
   echo "Usage: $0 { start | stop | status | install }"
   rval=1
   ;;
esac

exit $rval
