#!/bin/sh
#
# VMTux.net
#
# /etc/init.d/example
#
PATH=/bin:/sbin

umask 022

if [ "$(id -u)" -ne 0 ]; then echo "This command can only be used by root" >&2 ; exit 1; fi

pidfile=/var/run/example.pid

start() 
{
  echo -n "Starting example... "  
  RETVAL=$?
  PID=$!
  if [ $RETVAL != 0 ]; then
    echo "error"
  else
    echo
    echo $PID > $pidfile
  fi
}

stop() 
{
  echo -n "Stopping example... "
  if [ -f  $pidfile ]; then
    pid=$(cat $pidfile)
    if [ -d /proc/${pid}/ ]; then
      kill -QUIT $pid 2>/dev/null
      RETVAL=$?
      if [ $RETVAL != 0 ]; then
        echo "error"
      else
        echo
        rm -f $pidfile
      fi
    fi
  fi
}

status()
{
  if [ ! -f $pidfile ]; then
    echo "example is stopped"
  else
    pid=$(cat $pidfile)
    if [ -d /proc/${pid}/ ]; then
      echo "example is running"
    else
      echo "example is stopped"
      rm -f $pidfile
    fi
  fi
}

reload()
{
  echo "Reloading example..."
  if [ -f $pidfile ]; then
    kill -HUP $(cat $pidfile) 2>/dev/null
    if [ $? -ne 0 ]; then echo "error" ; fi
  fi
}

restart()
{
    echo "Restarting example"
    stop
    sleep 1
    start
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    reload)
        reload
    ;;
    restart)
        restart
    ;;
    status)
        status
    ;;
    *)
        echo "Usage: $0 {start|stop|status|reload|restart}"
    ;;
esac

exit 0
