#!/bin/sh
#
# Copyright (c) 2014 VMware, Inc.  All rights reserved.
#
# Author: Laxmikant Gunda, 2014
#
# Basic support for IRIX style chkconfig
# chkconfig: 235 99 01
# description: VMware Guest Introspection Service
# processname: vsepd
# pidfile: /var/run/vsep.pid

### BEGIN INIT INFO
# Provides:             vsepd
# Required-Start:       $network $remote_fs $syslog $time
# Required-Stop:        $network $remote_fs $syslog $time
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    VMware Guest Introspection Service
# Description:          Manages the service need to run VMware Guest Introspection
### END INIT INFO

VSEP_NAME=vsep
VSEP_BIN=/usr/sbin/$VSEP_NAME
VSEP_CONFIG=/etc/vsep/vsep.cfg
LOCK_FILE=/var/lock/subsys/vsepd

# Check for missing binaries
if [ ! -f $VSEP_BIN ] ; then
   echo "$VSEP_BIN not installed"
   if [ "$1" = "stop" ] ; then
      exit 0
   else
      exit 5
   fi
fi

#
# Enable this when we have a config file
# Check for missing config file
[ -e $VSEP_CONFIG ] && . $VSEP_CONFIG

# Determine OS type based on the functions library path
OS=""
if [ -f /etc/init.d/functions ] ; then
   OS="Redhat"
   . /etc/init.d/functions

   DAEMON_COREFILE_LIMIT='ulimited'
   alias START_DAEMON=daemon
   alias STOP_DAEMON=killproc
   alias STATUS=status
   alias LOG_SUCCESS=success
   alias LOG_FAILURE=failure
   alias LOG_WARNING=warning
   alias BLANK_LINE=echo
   MODPROBE_PARAM=""
elif [ -f /lib/lsb/init-functions ] && [ -f /etc/rc.status ] ; then
   OS="Suse"
   . /lib/lsb/init-functions

   alias START_DAEMON=start_daemon
   alias STOP_DAEMON=killproc
   alias STATUS=checkproc
   alias LOG_SUCCESS=log_success_msg
   alias LOG_FAILURE=log_failure_msg
   alias LOG_WARNING=log_warning_msg
   alias BLANK_LINE='echo'
   MODPROBE_PARAM="--allow-unsupported-modules"
elif [ -f /lib/lsb/init-functions ] ; then
   OS="Ubuntu"
   . /lib/lsb/init-functions

   alias START_DAEMON=start_daemon
   alias STOP_DAEMON=killproc
   alias STATUS=status_of_proc
   alias LOG_SUCCESS=log_success_msg
   alias LOG_FAILURE=log_failure_msg
   alias LOG_WARNING=log_warning_msg
   alias BLANK_LINE='echo'
   MODPROBE_PARAM=""
else
   OS="Unsupported OS, exiting."
   LOG_FAILURE $OS
   exit 1
fi

# Start the service
start () {
   if [ "`id -u`" -ne 0 ] ; then
      LOG_FAILURE "User has insufficient privilege."
      exit 4
   fi

   #
   # Check if there's already running instance of vsep, using lock_file
   # and using pidof command.. so at any time we will run only one instance
   #
   if [ -f $LOCK_FILE -a -n "`pidof $VSEP_BIN`" ] ; then
      if [ "$OS" = "Suse" ] ; then
         STATUS $VSEP_BIN
         rc_status -v
      elif [ "$OS" = "Redhat" ] ; then
         STATUS $VSEP_BIN
      elif [ "$OS" = "Ubuntu" ] ; then
         STATUS $VSEP_BIN
      fi
      exit 7;
   fi

   echo "Starting $VSEP_NAME service"

   START_DAEMON $VSEP_BIN
   if [ "$?" -eq 0 ] ; then
      ### Create the lock file ###
      mkdir -p /var/lock/subsys
      touch $LOCK_FILE

   else
      LOG_FAILURE "$VSEP_NAME service startup error: $?"
   fi

   BLANK_LINE
}

# Stop the service
stop() {
   if [ "`id -u`" -ne 0 ] ; then
      LOG_FAILURE "User has insufficient privilege."
      exit 4
   fi

   echo "Stopping $VSEP_NAME service"

   STOP_DAEMON $VSEP_BIN -TERM
   if [ "$?" -eq 0 ] ; then
      ### Now, delete the lock file ###
      rm -f $LOCK_FILE
   else
      LOG_FAILURE "$VSEP_NAME service stop error: $?"
   fi
   BLANK_LINE
}

### main logic ###
case "$1" in
   start)
      start
      if [ "$OS" = "Suse" ] ; then
         rc_status -v
      fi
      ;;
   stop)
      stop
      if [ "$OS" = "Suse" ] ; then
         rc_status -v
      fi
      ;;
   status)
      echo -n "Checking for $VSEP_NAME service: "
      if [ "$OS" = "Suse" ] ; then
         STATUS $VSEP_BIN
         rc_status -v
      elif [ "$OS" = "Redhat" ] ; then
         STATUS $VSEP_BIN
      elif [ "$OS" = "Ubuntu" ] ; then
         STATUS $VSEP_BIN
      fi
      ;;
   restart|force-reload)
      stop
      # Sleep for few seconds for the MUX connection to get refreshed
      sleep 3
      start
      if [ "$OS" = "Suse" ] ; then
         rc_status
      fi
      ;;
   *)
      echo "Usage: $0 {start|stop|restart|status|force-reload}"
      exit 1
esac
exit 0
