#!/bin/sh
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides:          mediatexd
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Mediatex daemon
# Description:       Mediatex server startup script
### END INIT INFO

# Author: Nicolas Roche <nroche@narval.tk>

DESC="Mediatex archival daemon"
USER=mdtx
PIDDIR=/var/run/mediatex
PIDFILE=$PIDDIR/${USER}d.pid
DAEMON=/usr/bin/mediatexd
DAEMON_ARGS="--conf-label $USER --background
 --facility local2 --severity notice"

#
# /var/run/mediatex is lost when system halt, because now /var/run
# is a tmpfs filesystem
# 
do_start_prepare()
{
    if [ ! -d $PIDDIR ]; then
	mkdir -m 0777 $PIDDIR
    fi
}

#
# Function that sends a SIGHUP to the daemon/service, 
# so as daemon can reload its configuration without restarting
#
do_reload()
{
	start-stop-daemon --stop --signal HUP --quiet \
	    --pidfile $PIDFILE
	return 0
}

#
# Function that stops the daemon/service
#
do_stop_cmd() {
	start-stop-daemon --stop --quiet --retry=TERM/10/KILL/5 \
	    --pidfile $PIDFILE --exec $DAEMON --user $USER
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	rm -f $PIDFILE
	return $RETVAL
}