#!/bin/sh
#
# chkconfig: 345 91 04
# description: Starts and stops hp Insight Diagnostics
#
# init.d/hpdiags
#
# System startup script for
#
### BEGIN INIT INFO
# Provides: hpdiags
# Required-Start: hpasm
# Required-Stop:  hpasm
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Start hpdiags
### END INIT INFO

DIAGS_DIR="/usr/share/diagnostics"
DIAGS_RUN="hpdiags"
DIAGS_BIN="hpdiags-bin"
DIAGS_DRIVER="hpdvmkd"

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
  function rc_status()
  {
        return 0
  }
  function rc_exit()
  {
        return 0
  }

elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
  function rc_status()
  {
        return 0
  }
  function rc_exit()
  {
        return 0
  }

elif [ -f /etc/rc.status ] ; then
  . /etc/rc.status
  rc_reset
  function echo_success()
  {
        return 0
  }
  function echo_failure()
  {
        return 0
  }

fi


pid=0
function get_diags_pid()
{
        pid=`ps -fu root | grep $DIAGS_BIN | grep -v grep | grep \\\-w | awk '($3==1) {print $2}'`
}


RETVAL=0
start() {
        echo -n $"Starting hp Insight Diagnostics: "

        get_diags_pid
        if test -n "$pid"
        then
          echo -n "already running"
          echo_success
        else
        {
                pushd $DIAGS_DIR > /dev/null

                ./$DIAGS_RUN -w > /dev/null

                RETVAL=$?
                popd > /dev/null
                rc_status -v

                get_diags_pid
                if test -n "$pid"
                then
                echo_success
                else
                echo_failure
                RETVAL=1
                fi
        }
        fi

        echo
        return $RETVAL
}

stop() {
        echo -n $"Shutting down hp Insight Diagnostics: "

        get_diags_pid
        if test -n "$pid"
        then
                pushd $DIAGS_DIR > /dev/null

                ./$DIAGS_RUN -k > /dev/null

                RETVAL=$?
                sleep 2
                #/sbin/rmmod $DIAGS_DRIVER 1> /dev/null 2> /dev/null
                popd > /dev/null

                echo_success

        else
                echo -n "not running"
                echo_success
        fi

        rc_status -v
        echo
        return $RETVAL
}

restart() {
        stop
        start
        rc_status
}

status() {
        get_diags_pid
        if test -n "$pid"
        then
                echo "hp Insight Diagnostics running"
        else
                echo "hp Insight Diagnostics not running"
        fi
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  status)
        status
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

exit $?
rc_exit

