#!/bin/sh

### BEGIN INIT INFO
# Provides:		user-fetchmail
# Required-Start:	$remote_fs $syslog
# Required-Stop:	$remote_fs $syslog
# Should-Start:		$network
# Should-Stop:		$network
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Enterprise Resource Management software
# Description:		Open ERP is a complete ERP and CRM software.
### END INIT INFO

PATH="/sbin:/bin:/usr/sbin:/usr/bin"
DAEMON="/usr/bin/fetchmail"
PROGNAME="user-fetchmail"
DESC="Users Fetchmail"
DEFAULT_FILE="/etc/default/$PROGNAME"

# per user variables
#
PIDFILE=
CONFIG=
LOGFILE=


# configurable variables (see $DEFAULT_FILE)
#
ENABLED="false"
OPTIONS="-vv"


test -x ${DAEMON} || exit 0

do_start_user()
{
	local USER=$1
	local home=$2
	local config="$home/.fetchmailrc.real"

	PIDFILE="$home/.fetchmail.pid"
	CONFIG="$home/.fetchmailrc"
	LOGFILE="$home/fetchmail.log"

	[ -f "$LOGFILE" ] && {
		/usr/bin/savelog -c 3 -t -p "$LOGFILE"
	}
	cp /dev/null "$LOGFILE"
	echo "STARTED $(date)" >>"$LOGFILE"

	rm -f "$LOGFILE.old"	# FIXME for old versions

	cp /dev/null "$config"
	chmod 700 "$config"
	chown $user "$config"

	# add daemon options if not in the config file
	grep -qs '^[[:space:]]*set[[:space:]]\+daemon[[:space:]]' "$CONFIG" || {
		echo "set daemon 900" >>"$config"
	}

	# force logfile on $LOGFILE
	echo "set logfile $LOGFILE" >>"$config"

	# add user defined config file
	grep -v -s '^[[:space:]]*set[[:space:]]\+logfile[[:space:]]' "$CONFIG" >>$config

	log_progress_msg $user
	start-stop-daemon --start --quiet --pidfile "$PIDFILE" \
		--chdir "$home" --chuid $user --exec ${DAEMON} \
		-- -f "$config" --nosyslog $OPTIONS >>"$LOGFILE" 2>&1
	return $?
}



do_stop_user()
{
	local USER=$1
	local home=$2

	PIDFILE="$home/.fetchmail.pid"

	log_progress_msg $user
	start-stop-daemon --stop --quiet --pidfile "$PIDFILE" --oknodo
}




do_start () {
  	$ENABLED || return 0

        log_daemon_msg "Starting $DESC: "
	
	local oifs="$IFS"
	IFS=":"
	getent passwd | while read user pass uid gid gecos home shell
	do
		[ -f "$home/.fetchmailrc" ] && do_start_user $user "$home"
	done
	IFS="$oifs"
	log_end_msg 0
	return 0
}

do_stop () {
  	$ENABLED || return 0
        log_daemon_msg "Stopping $DESC: "
	local oifs="$IFS"
	IFS=":"
	getent passwd | while read user pass uid gid gecos home shell
	do
		[ -f "$home/.fetchmail.pid" ] && do_stop_user $user "$home"
	done
	IFS="$oifs"
	log_end_msg 0
	return 0
}

do_stat()
{
	echo -e "\nrunning fetchmails:\n"
	ps axuwwwwww | fgrep $DAEMON | fgrep -v fgrep
	echo
	return 0
}

# (MAIN)



test -x $DAEMON || exit 0

. /lib/lsb/init-functions
. /etc/default/rcS

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"


[ -s $DEFAULT_FILE ] && {
    . $DEFAULT_FILE
    case "x$ENABLED" in
        xtrue|xfalse)
		;;
        *)	log_failure_msg "$DEFAULT_FILE: ENABLED must be 'true' or 'false';"
		log_failure_msg "daemon not started"
		exit 1
		;;
    esac
}


case "$1" in
  start)
  	do_start ;;
  stop)
  	do_stop ;;
  restart)
  	do_stop
	do_start
	;;
  reload|force-reload)
	do_stop
	do_start
	;;
  status)
	do_stat
	;;
  *)
	echo "usage: /etc/init.d/$PROGNAME {start|stop|reload|force-reload|restart|status}"
	exit 1
esac

exit 0
