#! /bin/bash
#
#
# This is an init (startup) script that runs a bunch of VOS 
# server daemons on Debian. It searches the /etc/omnivos/init directory for
# omnivos config files, and runs an instance of omnivos for each one.

# Based on the "skeleton" Debian init script written originally by Miquel van 
# Smoorenburg <miquels@cistron.nl>, modified for Debian GNU/Linux
# by Ian Murdock <imurdock@gnu.ai.mit.edu>.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DAEMON=@bindir@/omnivos
CONFDIR=/etc/omnivos
PIDDIR=/var/run
LOGDIR=/var/log
SETUID=daemon
SETGID=daemon

function checkUsableConfDir()
{
          if test ! -d $CONFDIR
          then
            echo "Did not detect configuration directory $CONFDIR; exiting."
            exit 0
          fi
          if test $(ls $CONFDIR/init | wc -l) == 0
          then
            echo "Did not detect any active configurations in $CONFDIR/init; exiting."
            exit 0
          fi
}

case "$1" in
      start)
        echo -n "Starting VOS servers:"
        shift
        if test -z "$1"
        then
          checkUsableConfDir
          files=${CONFDIR}/init/*
        else
          files=$*
        fi
        for f in $files
        do
           echo -n " $(basename $f)"
           pidfile=${PIDDIR}/omnivos-$(basename $f).pid
           logfile=${LOGDIR}/omnivos-$(basename $f).log
           touch $logfile
           chown $SETUID:$SETGID $logfile
           touch $pidfile
           chown $SETUID:$SETGID $pidfile
           USER=$SETUID
           LOGNAME=$SETUID
           export USER
           export LOGNAME
           start-stop-daemon --start --quiet --chuid $SETUID:$SETGID \
            --startas $DAEMON --pidfile=$pidfile \
            -- -q --pidfile=$pidfile --logfile=$logfile --config=$f
        done
        echo
        ;;
      stop)
        echo -n "Stopping VOS servers:"
        shift
        if test -z "$1"
        then
          checkUsableConfDir
          files=${CONFDIR}/init/*
        else
          files=$*
        fi
        for f in $files
        do
           echo -n " $(basename $f)"
           pidfile=${PIDDIR}/omnivos-$(basename $f).pid
           start-stop-daemon --stop --quiet --chuid $SETUID:$SETGID --pidfile=$pidfile
           /bin/rm -f $pidfile
        done
        echo
        ;;

        # reload)
        #
        # If the daemon can reload its config files on the fly
        # for example by sending it SIGHUP, do it here.
        #
        # If the daemon responds to changes in its config file
        # directly anyway, make this a do-nothing entry.
        #
        # echo -n "Reloading $DESC configuration..."
        # start-stop-daemon --stop --signal 1 --quiet --pidfile \
        # /var/run/$NAME.pid --exec $DAEMON
        # echo "done."
        #;;

      restart|force-reload)
        shift
        $0 stop $*
        sleep 1
        $0 start $*
        ;;

  #
  # If the "reload" option is implemented, move the "force-reload"
  # option to the "reload" entry above. If not, "force-reload" is
  # just the same as "restart".
  #

  #echo -n "Restarting $DESC: $NAME"
  #start-stop-daemon --stop --quiet --pidfile \
#
#   /var/run/$NAME.pid --exec $DAEMON
# sleep 1
# start-stop-daemon --start --quiet --pidfile \
#   /var/run/$NAME.pid --exec $DAEMON
# echo "."
# ;;
  *)
  # N=/etc/init.d/$NAME
  # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
  echo "Usage: $0 {start|stop|restart} [conf files]" >&2
  echo "       (if no [conf files] given, use all files found in ${CONFDIR}/init/)" >&2
  exit 1
  ;;
esac

exit 0
