#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          qwik
# Required-Start:    $syslog $time $local_fs
# Required-Stop:     $syslog $time $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: qwikWeb, a group communication system
# Description:       qwik's services of web server and smtp server
### END INIT INFO

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

WEBDAEMON=/usr/sbin/qwikweb-server
MLDAEMON=/usr/sbin/quickml-server

WEBNAME=qwikweb
MLNAME=quickml
DESC=qwikiWeb
PIDDIR=/var/run/qwik/

test -f $WEBDAEMON || exit 0
test -f $MLDAEMON || exit 0

#edit /etc/default/qwik change this.

# Add Your favorite options.
OPTIONS="-c /etc/qwik/config.txt"

# 0 = start on boot
# 1 = don't start on boot
START_QUICKML=0
START_QWIKWEB=0

# Start qwik process user name
QWIKUSER=qwik


# Include qwik defaults if available
if [ -f /etc/default/qwik ] ; then
        . /etc/default/qwik
fi


set -e

case "$1" in
  start)
    echo -n "Starting $DESC: "
    if [ ! -d "$PIDDIR" ] ; then
       mkdir $PIDDIR
       chown qwik:qwik $PIDDIR
    fi 
    if [ $START_QWIKWEB = "0" ] ; then
       start-stop-daemon --start --quiet --chuid $QWIKUSER --exec $WEBDAEMON -- $OPTIONS
       echo "Started $WEBNAME."
    fi
	 if [ $START_QUICKML = "0" ] ; then
       start-stop-daemon --start --quiet --chuid $QWIKUSER --exec $MLDAEMON -- $OPTIONS
       echo "Started $MLNAME."
	 fi
	 echo "Started $DESC. "
	 ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --oknodo --stop --quiet --name qwikweb-server
    echo "Stoped $WEBNAME."
    start-stop-daemon --oknodo --stop --quiet --name quickml-server
    echo "Stoped $MLNAME."
    echo "Stoped $DESC." 
    ;;
  restart|force-reload)
    echo -n "Stopping $DESC: "
    start-stop-daemon --oknodo --stop --quiet --name qwikweb-server
    echo "Stoped $WEBNAME."
    start-stop-daemon --oknodo --stop --quiet --name quickml-server
    echo "Stoped $MLNAME."
    echo "Stoped $DESC." 
    echo -n "Starting $DESC: "
    if [ ! -d "$PIDDIR" ] ; then
       mkdir $PIDDIR
       chown qwik:qwik $PIDDIR
    fi 
    if [ "$START_QWIKWEB" = "0" ] ; then
       start-stop-daemon --start --chuid $QWIKUSER --exec $WEBDAEMON -- $OPTIONS
       echo "Started $WEBNAME."
    fi
    if [ $START_QUICKML = "0" ] ; then
       start-stop-daemon --start --chuid $QWIKUSER --exec $MLDAEMON -- $OPTIONS
       echo "Starting $MLNAME."
    fi
    echo "Started $DESC. "
    ;;
  *)
    N=/etc/init.d/qwik
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0
