#!/bin/sh
#
# Fetchmail/fetchmail-ssl init script (fetchmail-common)
# $Id: fetchmail-common.init,v 1.5 2002/03/03 17:56:02 hmh Exp $
#
# A fetchmailrc file containg hosts and passwords for all local users should be
# placed in /etc/fetchmailrc.  Remember to make the /etc/fetchmailrc mode 600
# to avoid disclosing the users' passwords
#
# This script will NOT start or stop fetchmail if the /etc/fetchmailrc file
# does not exist.  Also, remember to stop fetchmail first when changing
# RUNASROOT.
#
# Don't muck around with SERVICE, use debconf/dpkg-reconfigure to change it;
# unless you have the fetchmail user properly set up, that is.
#

set -e

DAEMON=/usr/bin/fetchmail
test -f $DAEMON || exit 0

# Defaults
CONFFILE=/etc/fetchmailrc
RUNASROOT=false
SERVICE=true
OPTIONS="--daemon 300 --syslog"

# Reads config file (will override defaults above)
[ -r /etc/default/fetchmail ] && . /etc/default/fetchmail

# master switch
[ "${SERVICE}" != "true" ] && exit 0

OPTIONS="${OPTIONS} -f ${CONFFILE}"
RUNUSER=fetchmail
PIDFILE=/var/run/fetchmail/.fetchmail.pid
[ "${RUNASROOT}" = "true" ] && {
	RUNUSER=root
	PIDFILE=/var/run/fetchmail.pid
}

# sanity checks (saves on MY sanity :-P )
id ${RUNUSER} >/dev/null 2>&1 || {
	echo "$0: ${RUNUSER} user does not exist!"
	exit 1
}

# support for ephemeral /var/run
if [ "${PIDFILE%/*}" = "/var/run/fetchmail" -a ! -d ${PIDFILE%/*} ] ; then
	mkdir /var/run/fetchmail
	chown -h ${RUNUSER}:nogroup /var/run/fetchmail
	chmod 700 /var/run/fetchmail
fi

# sanity check
[ -d ${PIDFILE%/*} ] || {
	echo "$0: directory ${PIDFILE%/*} does not exist!!"
	exit 1
}

# Tries to find a better place for the UIDL 
# cache than root's homedir
[ -d /var/mail ] && UIDL=/var/mail/.fetchmail-UIDL-cache
[ "${UIDL}" != "" ] && OPTIONS="${OPTIONS} -i ${UIDL}"

# Makes sure certain files/directories have the proper owner
fixconfperms () {
   chown -h ${RUNUSER} "${CONFFILE}" >/dev/null 2>&1 || true
   chmod -f 0600 "${CONFFILE}" >/dev/null 2>&1 || true
   chown -h ${RUNUSER} "${UIDL}" >/dev/null 2>&1 || true
   [ -d /var/run/fetchmail ] && {
	chown -h -R ${RUNUSER}:nogroup "/var/run/fetchmail" >/dev/null 2>&1 ||
	   true
   }
}

testconfig () {
	[ -r ${CONFFILE} ] || {
		echo "system-wide fetchmail not configured."
		exit 0
	}
}

START="--start --quiet --exec ${DAEMON} --user ${RUNUSER} --startas /bin/su --"

case "$1" in
  start)
	echo -n "Starting mail retrieval agent: "
	fixconfperms
	testconfig
	echo -n "fetchmail"
	if start-stop-daemon ${START} \
	  "--command=${DAEMON} ${OPTIONS}" ${RUNUSER} <&- >/dev/null 2>&1 ; then
		echo "."
        else
		if start-stop-daemon --test ${START} \
		"--command=${DAEMON} ${OPTIONS}" ${RUNUSER} \
		<&- >/dev/null 2>&1 ; then
		   echo " (failed!)"
		   exit 1
		else
		   echo " already running."
		fi
	fi
	;;
  stop)
        echo -n "Stopping mail retrieval agent: "
	testconfig
        if su "--command=${DAEMON} --quit" ${RUNUSER} <&- >/dev/null 2>&1 ; then
		echo "fetchmail."
	else
		echo "system-wide fetchmail not running."
	fi
        ;;
  force-reload|restart)
        echo -n "Restarting mail retrieval agent: "
	fixconfperms
	testconfig
	echo -n "fetchmail"
        su "--command=${DAEMON} --quit" ${RUNUSER} <&- >/dev/null 2>&1 || true
        if start-stop-daemon ${START} \
	   "--command=${DAEMON} ${OPTIONS}" ${RUNUSER} <&- >/dev/null 2>&1 ; then
        	echo "."
	else
		echo " did not start."
	fi
        ;;
  awaken)
  	echo -n "Awakening mail retrieval agent: "
	testconfig
	[ -r ${PIDFILE} ] && \
	   echo -n "fetchmail" && \
	   su "--command=${DAEMON}" ${RUNUSER} <&- >/dev/null 2>&1 && \
	   echo "." && \
	   exit 0
	echo "system-wide fetchmail not running."
	exit 1
	;;
  debug-run)
  	echo "$0: Initiating debug run of system-wide fetchmail service..." 1>&2
	echo "$0: script will be run in debug mode, all output to forced to" 1>&2
	echo "$0: stdout. This is not enough to debug failures that only" 1>&2
	echo "$0: happen in daemon mode." 1>&2
	echo "$0: You might want to direct output to a file, and tail -f it." 1>&2
	[ "$2" = "strace" ] && 
		echo "$0: (running debug mode under strace. See strace(1) for options)" 1>&2
		echo "$0: WARNING: strace output may contain security-sensitive info, such as" 1>&2
		echo "$0: passwords; please clobber them before sending the strace file to a" 1>&2
		echo "$0: public bug tracking system, such as Debian's." 1>&2
	testconfig
	echo "$0: Stopping the service..." 1>&2
	"$0" stop && true
	echo "$0: exit status of service stop was: $?"
	echo "$0: RUNUSER is ${RUNUSER}"
	echo "$0: OPTIONS would be ${OPTIONS}"
	echo "$0: Starting service in nodetach mode, hit ^C (SIGINT/intr) to finish run..." 1>&2
	if [ "$2" = "strace" ] ; then
		shift
		shift
		[ $# -ne 0 ] && echo "$0: (strace options are: -tt $@)" 1>&2
		su "--command=/usr/bin/strace -tt $@ ${DAEMON} ${OPTIONS} --nosyslog --nodetach -v -v" ${RUNUSER} <&- 2>&1 && true
	else
		su "--command=${DAEMON} ${OPTIONS} --nosyslog --nodetach -v -v" ${RUNUSER} <&- 2>&1 && true
	fi
	echo "$0: End of service run. Exit status was: $?"
	exit 0
  	;;
  *)
        echo "Usage: /etc/init.d/fetchmail {start|stop|restart|force-reload|awaken|debug-run}"
	echo "  start - starts system-wide fetchmail service"
	echo "  stop  - stops system-wide fetchmail service"
	echo "  restart, force-reload - starts a new system-wide fetchmail service"
	echo "  awaken - tell system-wide fetchmail to start a poll cycle immediately"
	echo "  debug-run [strace [strace options...]] - start a debug run of the"
	echo "    system-wide fetchmail service, optionally running it under strace"
        exit 1
        ;;
esac

exit 0
