#!/bin/sh
#
# Copyright (C) 2001-2008 Graeme Walker <graeme_walker@users.sourceforge.net>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or 
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ===
# 
# emailrelay
# 
# A shell-script wrapper for E-MailRelay designed for use in the SysV-init 
# system (/etc/init.d).
#
# Additional command-line switches for the emailrelay daemon are sourced 
# from the file "/etc/emailrelay.conf" if it exists. Uncommented lines in this 
# file have "--" prepended to them and then they are pasted onto the command 
# line.
#
# usage: emailrelay { start | stop | restart | force-reload | status }
#
# See also: LSB, start_daemon (lsb), startproc (suse), install_initd (lsb), 
#           insserv (suse), /usr/share/doc/initscripts*/sysvinitfiles (redhat)
#

##
# LSB comment block...
#
### BEGIN INIT INFO
# Provides:       emailrelay
# Required-Start: $network
# Required-Stop:  $network
# Default-Start:  3 4 5
# Default-Stop:   3 4 5
# Description:    E-MailRelay SMTP proxy and store-and-forward MTA.
### END INIT INFO
##

# choose an infrastructure style -- linux-standard-base (lsb) or unix
#
if test -f /lib/lsb/init-functions
then 
	style="lsb" 
	. /lib/lsb/init-functions
else
	style="unix"
fi

# script configuration
#
var_run="/var/run"
emailrelay="__SBIN_DIR__/emailrelay" # (absolute path required in some environments)
if test \! -d "${var_run}" ; then var_run="/tmp" ; fi
if test \! -x "${emailrelay}" ; then emailrelay="`pwd`/emailrelay" ; fi
pid_file="${var_run}/emailrelay.pid"
PATH="${PATH}:/sbin:/bin:/usr/bin"
ECHO="echo" ; if test -x /bin/echo ; then ECHO="/bin/echo" ; fi
cfg_file="__SYSCONF_DIR__/emailrelay.conf"

# server configuration using the config file
#
start_switches="--as-server --pid-file ${pid_file} `cat \"${cfg_file}\" 2>/dev/null | egrep -v '^#|^ *$' | sed 's/^/--/'`"

# functions...
#
# <style>_reset() -- initialise
# <style>_cmd_stop() -- stop command
# <style>_cmd_start() -- start command
# <style>_cmd_restarted() -- called after stop/start
# <style>_cmd_status() -- status command
# <style>_exit() -- exit with saved errno

unix_reset()
{
	unix_errno="0"
}

unix_cmd_start()
{
	$ECHO -n "${1}"
	shift
	"$@"
	unix_errno="$?"
	if test "${unix_errno}" -eq 0
	then
		echo " ... done"
	else
		echo " ... failed"
	fi
}

unix_cmd_stop()
{
	$ECHO -n "${1}"
	if test -f "${pid_file}" && test "`cat \"${pid_file}\"`" != ""
	then
		kill "`cat ${pid_file}`"
		rm -f "${pid_file}" 2>/dev/null
	fi
	echo " ... done"
	unix_errno="0" # (could do better)
}

unix_cmd_restarted()
{
	unix_errno="$?"
}

unix_cmd_status()
{
	$ECHO -n "${1}"
	if test -f "${pid_file}" && test "`cat \"${pid_file}\"`" != "" && kill -0 "`cat \"${pid_file}\"`" 2>/dev/null
	then
		echo " ... running"
		unix_errno="0"
	elif test -f "${pid_file}" 
	then
		echo " ... failed"
		unix_errno="1"
	else
		echo " ... not running"
		unix_errno="3"
	fi
}

unix_exit()
{
	exit "${unix_errno}"
}

##

lsb_reset()
{
	lsb_errno="0"
}

lsb_cmd_start()
{
	lsb_text="${1}"
	shift
	start_daemon "$@"
	lsb_errno=$?
	if test "${lsb_errno}" -eq 0
	then
		log_success_msg "${lsb_text}"
	else
		log_failure_msg "${lsb_text}"
	fi
}

lsb_cmd_stop()
{
	lsb_text="${1}"
	killproc "`basename \"${2}\"`"
	lsb_errno=$?
	if test "${lsb_errno}" -eq 0
	then
		log_success_msg "${lsb_text}"
	else
		log_failure_msg "${lsb_text}"
	fi
}

lsb_cmd_restarted()
{
	lsb_errno="$?"
}
 
lsb_cmd_status()
{
	lsb_text="${1}"
	base="`basename \"${2}\"`"
	pids="`pidofproc \"${base}\" | sed 's/ *$//'`"
	if test "${pids}" != ""
	then
		lsb_errno="0"
		log_success_msg "${lsb_text}"
	else
		lsb_errno="1"
		log_failure_msg "${lsb_text}"
	fi
}

lsb_exit()
{
	exit ${lsb_errno}
}

# check the command line
#
usage="{ start | stop | restart | force-reload | status }"
if test $# -eq 0
then
	echo usage: `basename $0` "${usage}" >&2
	exit 2
fi

# process the command line
#
${style}_reset
case "${1}" in

	start)
		shift
		${style}_cmd_start "Starting E-MailRelay server" "${emailrelay}" ${start_switches} "$@"
		;;

	stop)
		${style}_cmd_stop "Shutting down E-MailRelay" "${emailrelay}"
		;;

	restart|force-reload)
		shift
		$0 stop
		$0 start "$@"
		${style}_cmd_restarted
		;;

	reload)
		echo usage: `basename $0` reload: not implemented >&2
		exit 3
		;;

	status)
		${style}_cmd_status "Checking for E-MailRelay" "${emailrelay}"
		;;

	*)
		echo usage: `basename $0` "${usage}" >&2
		exit 2
		;;
esac
${style}_exit

