#!/bin/sh
#
# /etc/init.d/omnidexd
# Subsystem file for "Omnidex" server
#
# Basic support for IRIX style chkconfig
# chkconfig: 35 80 20
# description: This service starts and stops the Omnidex Network Server \
#              that provides the critical networking communication \
#              between Omnidex clients and the Omnidex SQL Engine.
# short-description: Start and stop Omnidex Network Server
# processname: omnidex
# pidfile: /var/run/omnidex.pid

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0
prog="omnidex"
CFG=""

start() {
  local procs=$(ps aux | grep '[o]mnidex -port=7555' | wc -l)
  if [ $procs -eq 0 ]
  then
    if [ "$CFG" = "" ]
    then
      echo -n $"Starting $prog:"
      omnidex -port=7555 -daemon > /opt/omnidex/logs/service.log 2>&1
      RETVAL=$?
    else
      echo -n $"Starting $prog with $CFG:"
      omnidex -port=7555 -daemon "$CFG" > /opt/omnidex/logs/service.log 2>&1
      RETVAL=$?
    fi
    if [ "$RETVAL" = 0 ]
    then
      sleep 1
      local output=$(ps aux | grep '[o]mnidex -port=7555')
      if [ -z "$output" ]
      then
        cat /opt/omnidex/logs/service.log
        echo Failed because ps output was null
        echo_failure
      else
        set -- $output
        echo $2 > /var/run/omnidex.pid
        touch /var/lock/subsys/$prog
        echo_success
      fi
    else
      cat /opt/omnidex/logs/service.log
      echo Failed because retval from omnidex listener was not zero
      echo_failure
    fi
    echo
  else
    echo $prog is currently running
  fi
}

stop() {
  local procs=$(ps aux | grep '[o]mnidex -port=7555' | wc -l)
  if [ $procs -gt 0 ]
  then
    omnidex -port=7555 -shutdown -noprintfs
    RETVAL=$?
    if [ "$RETVAL" = 0 ]
    then
      let procs=$(ps aux | grep '[o]mnidex -port=7555' | wc -l)
      while [ $procs -gt 1 ]
      do
        echo "Waiting for omnidex processes to die. $procs still running."
        sleep 1
        let procs=$(ps aux | grep '[o]mnidex -port=7555' | wc -l)
      done
      echo -n $"Stopping $prog:"
      rm -f /var/lock/subsys/$prog
      rm -f /var/run/omnidex.pid
      echo_success
    else
      echo_failure
    fi
    echo
  else
    rm -f /var/lock/subsys/$prog
    rm -f /var/run/omnidex.pid
    echo $prog is currently not running
  fi
}

restart() {
  local procs=$(ps aux | grep '[o]mnidex -port=7555' | wc -l)
  if [ $procs -gt 0 ]
  then
    stop
    # avoid race
    sleep 3
  fi
  start
}

condrestart() {
  local procs=$(ps aux | grep '[o]mnidex -port=7555' | wc -l)
  if [ $procs -gt 0 ]
  then
    stop
    # avoid race
    sleep 3
    start
  fi
}

checkconfig() {
  local procs=$(ps aux | grep '[o]mnidex -port=7555' | wc -l)
  if [ $procs -gt 0 ]
  then
    local cfgfile=$(grep 'Configuration file designated' /opt/omnidex/logs/service.log)
    if [ "$cfgfile" != "" ]
    then
      echo ${cfgfile:18}
    fi
  fi
}

case "$1" in
  start)
    CFG="$2"
    start
    ;;
  stop)
    stop
    ;;
  restart)
    CFG="$2"
    restart
    ;;
  condrestart)
    CFG="$2"
    condrestart
    ;;
  status)
    status $prog
    RETVAL=$?
    checkconfig
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|condrestart|status} [<cnfgfile>]"
    RETVAL=1
esac
exit $RETVAL

