#!/bin/sh

SIG=-int
inetd=no
cy_buffering=no
sshd=no
snmp=no
syslog=no
stop=no
start=no
PGMS=""

while [ ! "x$1" = "x" ] ; do
  case $1 in
   start)
     start=yes
     ;;
   stop)
     stop=yes
     ;;
   inetd)
     if [ "$inetd" = "no" ] ; then
       inetd=yes
       PGMS="$PGMS inetd" 
     fi
     ;;
   sshd)
     if [ "$sshd" = "no" ] ; then
       sshd=yes
       PGMS="$PGMS sshd" 
     fi
     ;;
   snmp)
     if [ "$snmp" = "no" ] ; then
       snmp=yes
       PGMS="$PGMS snmpd" 
     fi
     ;;
   syslog)
     if [ "$syslog" = "no" ] ; then
       syslog=yes
       PGMS="$PGMS syslog-ng" 
     fi
     ;;
   buf_only)
     if [ "$cy_buffering" = "no" ] ; then
       cy_buffering=yes
       PGMS="cy_buffering" 
     fi
     ;;
   buffering)
     if [ "$cy_buffering" = "no" ] ; then
       cy_buffering=yes
       PGMS="$PGMS cy_buffering" 
     fi
     ;;
   *)
    SIG=-$1
  esac
  shift
done


if [ "$start" = "yes" ] ; then
  MPID=`ps ax | grep syslog-ng | grep -v grep | cut -b -6`
  if [ "$MPID" = "" ] ; then
    sh_write_evt 11 Us syslog-ng
    /sbin/syslog-ng
  fi
  MPID=`ps ax | grep inetd | grep -v grep | cut -b -6`
  if [ "$MPID" = "" ] ; then
    sh_write_evt 11 Us inetd
    /sbin/inetd
  fi
  MPID=`ps ax | grep sshd | grep -v grep | cut -b -6`
  if [ "$MPID" = "" ] ; then
    sh_write_evt 11 Us sshd
    /sbin/sshd -f /etc/ssh/sshd_config
  fi
  MPID=`ps ax | grep cy_ras | grep -v grep | cut -b -6`
  if [ "$MPID" = "" ] ; then
    sh_write_evt 11 Us cy_ras
    /sbin/cy_ras &
  fi
  MPID=`ps ax | grep cy_buffering | grep -v grep | cut -b -6`
  if [ "$MPID" = "" ] ; then
    sh_write_evt 11 Us cy_buffering
    /sbin/cy_buffering &
  fi
  MPID=`ps ax | grep snmpd | grep -v grep | cut -b -6`
  if [ "$MPID" = "" ] ; then
    sh_write_evt 11 Us snmpd
    /sbin/snmpd
  fi
  exit 0
fi

if [ "$stop" = "yes" ] ; then
  SIG=-int
  PGMS="inetd cy_buffering cy_ras sshd snmp syslog-ng"
fi

if [ "$PGMS" = "" ] ; then
	PGMS="cy_ras"
fi

for pgm in $PGMS ; do
  MPID=`ps ax | grep $pgm | grep -v grep | cut -b -6`
  if [ ! "$MPID" = "" ] ; then
    if [ "$pgm" = "cy_ras" -a "$sig" = "-usr2" ] ; then
      sleep 1
    fi
    kill $SIG $MPID
    
    if [ "$stop" = "yes" ] ; then
	sh_write_evt 12 Us pgm
    else
	sh_write_evt 15 U
    fi

  else
    if [ ! "$pgm" = "cy_ras" -a ! "$sig" = "-usr2" ] ; then
      echo "*** ERROR *** $pgm is not running !!!"
    fi
  fi
done

