#!/bin/sh
#
### BEGIN INIT INFO
# Provides: @PACKAGE_PREFIX@owcimomd
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6 
# Description: @PACKAGE_PREFIX@owcimomd
#       Start/Stop the OpenWBEM CIMOM Daemon
### END INIT INFO
#
#
# chkconfig: 2345 36 64
# description: ADIC CIMOM Daemon
# processname: owcimomd

NAME=owcimomd
DAEMONPATH=/home/embedded/library/SMI-SService
DAEMON=${DAEMONPATH}/bin/$NAME
OPTIONS=
DESCRIPTIVE="ADIC CIMOM Daemon"

SLPDNAME=slpd
SLPDDAEMONPATH=/home/embedded/library/SMI-SService
SLPDDAEMON=${SLPDDAEMONPATH}/bin/$SLPDNAME
SLPDLOGFILE=/dev/null
SLPDOPTIONS=" -l ${SLPDLOGFILE} -c ${SLPDDAEMONPATH}/etc/openwbem/slp.conf -r ${SLPDDAEMONPATH}/etc/openwbem/slp.reg -p /var/run/slpd.pid"
SLPDDESCRIPTIVE="ADIC slpd Daemon"

REPOSITORYDIR=${DAEMONPATH}/repository
REPOSITORYBACKUP=${DAEMONPATH}/repository.bk

SSLINSTALLDIR=/home/embedded/library/SSLService
export LD_LIBRARY_PATH=${DAEMONPATH}/lib:${SSLINSTALLDIR}/lib:${DAEMONPATH}/lib/cmpiproviders:${DAEMONPATH}/../lib
export SUPPORTOPENSLP=yes
ret=0

# Set the current working directory
cd /


#################### START ##########################
slpd_start() {
 if [ ! -f $SLPDDAEMON ]; then
     echo "ERROR: $SLPDDAEMON not found, exiting"
     exit 1
  fi
    
  if [ ! -f ${SLPDDAEMONPATH}/etc/openwbem/slp.conf ]; then
     echo "ERROR: ${SLPDDAEMONPATH}/etc/openwbem/slp.conf not found, exiting"
     exit 1
  fi

  if [ ! -f ${SLPDLOGFILE} ]; then
     touch ${SLPDLOGFILE};
  fi
  #
  # Verify no currently running snmpd
  #
  ps -e | grep -v "grep $SLPDNAME"| grep $SLPDNAME 1>/dev/null 2>&1;
  if [ $? = 0 ]; then
     echo "$SLPDDESCRIPTIVE is already running"
     return
  else
     rm -f /var/run/slpd.pid;
  fi
  # Start daemon.
  echo -n "Starting the $SLPDDESCRIPTIVE"
  $SLPDDAEMON $SLPDOPTIONS >/dev/null 
  ret=$?
  echo "."
  renice 19 `cat /var/run/slpd.pid` > /dev/null
}

smis_start() {
 #Check SSL enabled or not and changee our conf accordingly
 isSSLenabled=`grep ssmis /etc/extaccess.conf | awk -F= '{print $2}'`;

 if [ ${isSSLenabled} -ne 0 ]; then
     sed -i s/".*http_server.https_port = -1"/"http_server.https_port = 5989"/g $DAEMONPATH/etc/openwbem/openwbem.conf;
     sed -i s/".*http_server.http_port = 5988"/"http_server.http_port = -1"/g $DAEMONPATH/etc/openwbem/openwbem.conf;
 else
     sed -i s/".*http_server.http_port = -1"/"http_server.http_port = 5988"/g $DAEMONPATH/etc/openwbem/openwbem.conf;
 fi

 # Make sure the logging directory exists
 local log_dir=`dirname $(grep '^log.main.location' /home/embedded/library/SMI-SService/etc/openwbem/openwbem.conf | awk '{print $3}')`
 if [ -n "$log_dir" ]; then
   [ ! -d "$log_dir" ] && mkdir -p "$log_dir"
 fi
     
 #switch the repository to good one on every reboot
 if [ ! -f ${REPOSITORYBACKUP} ]; then
    tar Pczf ${REPOSITORYBACKUP} ${REPOSITORYDIR}
 fi
 if [ ! -f $DAEMON ]; then
     echo "ERROR: $DAEMON not found, exiting"
     exit 1
  fi
 
  if [ ! -f ${DAEMONPATH}/etc/openwbem/openwbem.conf ]; then
     echo "ERROR: ${DAEMONPATH}/etc/openwbem/openwbem.conf not found, exiting"
     exit 1
  fi
  #
  # Verify no currently running snmpd
  #
  ps -e | grep -v "grep $NAME"|grep $NAME 1>/dev/null 2>&1;
  if [ $? = 0 ]; then
     echo "$DESCRIPTIVE is already running"
     return
  else
     rm -f /var/run/owcimomd.pid;
  fi
  # Start daemon.
  echo -n "Starting the $DESCRIPTIVE"
  $DAEMON $OPTIONS >/dev/null 
  ret=$?
  echo "."
  renice 19 `cat /var/run/owcimomd.pid` > /dev/null
}


#################### STOP ##########################
slpd_stop() {  
   ps -e | grep -v "grep $SLPDNAME"| grep $SLPDNAME 1>/dev/null 2>&1;
   if [ $? = 0 ]; then
      # Stop daemons.
      echo -n "Shutting down $SLPDDESCRIPTIVE"

      kill -TERM `cat /var/run/slpd.pid`;
      count=0
      while ps -e|grep -v grep|grep $SLPDNAME >/dev/null; do
         usleep 200
         count=$(expr $count + 1)
         if [ $count -gt 20 ]; then
            killall -9 $SLPDNAME >/dev/null
            break;
         fi
      done

      echo " Terminated"
      ret=0
   else
      # echo "WARNING: $SLPDDESCRIPTIVE already stopped!"
      return
   fi
}

smis_stop() {  
   ps -e | grep -v "grep $NAME"|grep $NAME 1>/dev/null 2>&1;
   if [ $? = 0 ]; then
      # Stop daemons.
      echo -n "Shutting down $DESCRIPTIVE"

      kill -TERM `cat /var/run/owcimomd.pid`;
      count=0                                                                                                                 
      while ps -e|grep -v grep|grep $NAME >/dev/null; do                                                                  
         usleep 200000 
         count=$(expr $count + 1)
         if [ $count -gt 20 ]; then
            killall -9 $NAME >/dev/null
            break;
         fi
      done                     

      echo " Terminated"
      ret=0
   else
      # echo "WARNING: $DESCRIPTIVE already stopped!"
      return
   fi
}

#################### RESTART ##########################
slpd_restart() {
  slpd_stop
  sleep 1
  slpd_start
}
smis_restart() {
  smis_stop
  sleep 1
  smis_start
}

#################### STATUS ##########################
slpd_status() {
  ps -e | grep -v "grep $SLPDNAME"| grep $SLPDNAME 1>/dev/null 2>&1;
  if [ $? = 0 ]; then
      echo "$SLPDDESCRIPTIVE is running."
      ret=0
  else
      echo "$SLPDDESCRIPTIVE is not running."
      ret=3
  fi
}
smis_status() {
  ps -e | grep -v "grep $NAME"|grep $NAME 1>/dev/null 2>&1;
  if [ $? = 0 ]; then
      echo "$DESCRIPTIVE is running."
      ret=0
  else
      echo "$DESCRIPTIVE is not running."
      ret=3
  fi
}

# See how we were called.
case "$1" in
 start)
if [ "${SUPPORTOPENSLP}" = "yes" ]; then
    slpd_start
fi
    smis_start
    ;;

 stop)
if [ "${SUPPORTOPENSLP}" = "yes" ]; then
    slpd_stop
fi
    smis_stop
    ;;

 restart)
if [ "${SUPPORTOPENSLP}" = "yes" ]; then
    slpd_restart
fi
    smis_restart
    ;;
 
 status)
if [ "${SUPPORTOPENSLP}" = "yes" ]; then
    slpd_status
fi
    smis_status
    ;;

 *)
  echo "Usage: smis_control {[re]start|stop|status}"
  ret=2
esac

exit $ret

