#!/bin/sh
################################################################################
# Simple init.d script for Ventrilo server
################################################################################

VENT_PATH="/usr/local/ventrilo"
VENT_PID="ventrilo_srv.pid"
VENT_USER="www-data"

start() {
 cd $VENT_PATH
 if [ -f "$VENT_PID" ]; then
  echo "Pidfile $VENT_PATH/$VENT_PID present.  Make sure process isn't already running and remove pidfile if it is not."
  exit 1
 fi

 echo "Starting Ventrilo server"
 su $VENT_USER -c "./ventrilo_srv -d"
}

stop() {
 cd $VENT_PATH
 if [ ! -f "$VENT_PID" ]; then
  echo "Pidfile $VENT_PATH/$VENT_PID not present.  Checking to see if ventrilo_srv is in the process table."
  process=`ps -eo pid,args | grep ventrilo_srv | grep -v grep`
  if [ "$process" ]; then
   pid=`echo $process | awk '{print $1}'`
   echo "Found process [$process] in process table.  Killing pid [$pid]."
   kill $pid
  else
   echo "No running processes found"
  fi
 else
  pid=`cat $VENT_PID`
  echo "Killing Ventrilo server process id $pid"
  kill $pid
 fi
}


case "$1" in
 start)
  start
  ;;
 stop)
  stop
  ;;
 restart)
  echo "Restarting Ventrilo server"
  stop
  start
  ;;
 *)
  echo "ventrilo (start|stop|restart)"
  ;;
esac
