#!/bin/sh
#
# Copyright (C) 2001-2013 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 for use in the SysV-init system.
#
# Additional command-line options 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 }
#

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

# initialise
#
PATH="$PATH:/sbin:/bin:/usr/bin"
ECHO="echo" ; if test -x /bin/echo ; then ECHO="/bin/echo" ; fi
var_run="/var/run" ; if test -d "$var_run" -a -w "$var_run" ; then : ; else var_run="/tmp" ; fi
pid_file="$var_run/emailrelay.pid"
config_file="__SYSCONF_DIR__/emailrelay.conf"
config_file_template="__SYSCONF_DIR__/emailrelay.conf.template"
emailrelay="__SBIN_DIR__/emailrelay" # (absolute path required in some environments)
if test \! -x "$emailrelay" ; then emailrelay="`pwd`/emailrelay" ; fi
local_errno=0

# 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

# choose an infrastructure -- linux-standard-base (lsb) or not
#
if test -f /lib/lsb/init-functions
then 
	. /lib/lsb/init-functions
	do_cmd_start()
	{
		lsb_text="$1"
		shift
		start_daemon -p "$pid_file" "$@"
		local_errno=$?
		if test "$local_errno" -eq 0
		then
			log_success_msg "$lsb_text"
		else
			log_failure_msg "$lsb_text"
		fi
	}
	do_cmd_stop()
	{
		lsb_text="$1"
		killproc -p "$pid_file" "`basename \"$2\"`"
		local_errno=$?
		if test "$local_errno" -eq 0
		then
			log_success_msg "$lsb_text"
		else
			log_failure_msg "$lsb_text"
		fi
	}
	do_cmd_restarted()
	{
		local_errno="$?"
	}
	do_cmd_status()
	{
		lsb_text="$1"
		base="`basename \"$2\"`"
		pids="`pidofproc -p \"$pid_file\" \"$base\" | sed 's/ *$//'`"
		if test "$pids" != ""
		then
			local_errno="0"
			log_success_msg "$lsb_text"
		else
			local_errno="1"
			log_failure_msg "$lsb_text"
		fi
	}
	do_exit()
	{
		exit "$local_errno"
	}
else
	do_cmd_start()
	{
		$ECHO -n "$1"
		shift
		"$@"
		local_errno="$?"
		if test "$local_errno" -eq 0
		then
			$ECHO " ... done"
		else
			$ECHO " ... failed"
		fi
	}
	do_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"
		local_errno="0" # (could do better)
	}
	do_cmd_restarted()
	{
		local_errno="$?"
	}
	do_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"
			local_errno="0"
		elif test -f "$pid_file" 
		then
			$ECHO " ... failed"
			local_errno="1"
		else
			$ECHO " ... not running"
			local_errno="3"
		fi
	}
	do_exit()
	{
		exit "$local_errno"
	}
fi

install_on_first_run()
{
	# create a default config file if necessary and if possible
	if test -f "$config_file_template" -a ! -f "$config_file"
	then
		cp -p "$config_file_template" "$config_file"
	fi
}

config_options()
{
	# echo --options from the config file
	cat "$config_file" 2>/dev/null | egrep -v '^#|^gui-|^ *$' | sed 's/^/--/'
}

start_options()
{
	# echo start command tail
	echo --as-server --pid-file "$pid_file" `config_options`
}

# process the command line
#
install_on_first_run
case "$1" in

	start)
		shift
		do_cmd_start "Starting E-MailRelay server" "$emailrelay" `start_options` "$@"
		;;

	stop)
		do_cmd_stop "Shutting down E-MailRelay" "$emailrelay"
		;;

	restart|force-reload)
		shift
		$0 stop
		$0 start "$@"
		do_cmd_restarted
		;;

	try-restart|reload|force-reload)
		echo `basename $0`: $1 not implemented >&2
		exit 3
		;;

	status)
		do_cmd_status "Checking for E-MailRelay" "$emailrelay"
		;;

	*)
		echo usage: `basename $0` "$usage" >&2
		exit 2
		;;
esac
do_exit

