#!/bin/sh 

# Start the proftpd FTP daemon.

PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/sbin/proftpd
NAME=proftpd

# Defaults
RUN="no"
OPTIONS=""

PIDFILE=`grep -i 'pidfile' /etc/proftpd.conf | sed -e 's/pidfile[\t ]\+//i'`
if [ "x$PIDFILE" = "x" ];
then
	PIDFILE=/var/run/proftpd.pid
fi

# Read config (will override defaults)
[ -r /etc/default/proftpd ] && . /etc/default/proftpd

trap "" 1
trap "" 15

test -f $DAEMON || exit 0

#
# Servertype could be inetd|standalone|none.
# In all cases check against inetd and xinetd support.
#
if ! egrep -qi "^[[:space:]]*ServerType.*standalone" /etc/proftpd.conf
then
    if [ $(dpkg-divert --list xinetd|wc -l) -eq 1 ] 
    then
	if egrep -qi "server[[:space:]]*=[[:space:]]*/usr/sbin/proftpd" /etc/xinetd.conf -o \
	   egrep -qi "server[[:space:]]*=[[:space:]]*/usr/sbin/proftpd" /etc/xinetd.d/* 2>/dev/null
	then
    		RUN="no"
    		INETD="yes"
	else
		if ! egrep -qi "^[[:space:]]*ServerType.*inetd" /etc/proftpd.conf
		then
    			RUN="yes"
			INETD="no"
		else
			RUN="no"
			INETD="no"
		fi
	fi
    else
    	if egrep -qi "^ftp.*/usr/sbin/proftpd" /etc/inetd.conf 2>/dev/null
    	then
    		RUN="no"
    		INETD="yes"
    	else
		if ! egrep -qi "^[[:space:]]*ServerType.*inetd" /etc/proftpd.conf
		then
    			RUN="yes"
			INETD="no"
		else
			RUN="no"
			INETD="no"
		fi
    	fi
    fi
fi

start()
{
    if start-stop-daemon --start --quiet --pidfile "$PIDFILE" \
        --exec $DAEMON -- $OPTIONS ; then
        echo "$NAME."
    else
        echo "."
    fi
}

signal()
{
    if [ "$1" = "stop" ]; then
	SIGNAL="TERM"
    else
	if [ "$1" = "reload" ]; then
	    SIGNAL="HUP"
	else
	    echo "ERR: wrong parameter given to signal()"
	fi
    fi
    if start-stop-daemon --stop --signal $SIGNAL --quiet --pidfile "$PIDFILE"; then
        echo "$NAME."
    else
	SIGNAL="KILL"
	if start-stop-daemon --stop --signal $SIGNAL --quiet --pidfile "$PIDFILE"; then
		echo "$NAME."
	else
        	echo "."
	fi
    fi
    if [ "$SIGNAL" = "KILL" ]; then
	    rm -f "$PIDFILE"
    fi
}

case "$1" in
    start)
	if [ "x$RUN" = "xyes" ] ; then
	    echo -n "Starting ProFTPD ftp daemon: "
	    start
	else
	    if [ "x$INETD" = "xyes" ] ; then
		echo "ProFTPd is started from inetd/xinetd."
	    else 
	    	echo "ProFTPd warning: cannot start neither in standalone nor in inetd/xinetd mode. Check your configuration."
	    fi
	fi
	;;

    force-start)
	if [ "x$INETD" = "xyes" ] ; then
	    echo "Warning: ProFTPd is started from inetd/xinetd (trying to start anyway)."
	fi
	echo -n "Starting ProFTPD ftp daemon: "
	start
	;;	
    
    stop)
	if [ "x$RUN" = "xyes" ] ; then
	    echo -n "Stopping ProFTPD ftp daemon: "
	    signal stop
	else
	    if [ "x$INETD" = "xyes" ] ; then
		echo "ProFTPd is started from inetd/xinetd."
	    else 
	    	echo "ProFTPd warning: cannot start neither in standalone nor in inetd/xinetd mode. Check your configuration."
	    fi
	fi
	;;

    force-stop)
	if [ "x$INETD" = "xyes" ] ; then
	    echo "Warning: ProFTPd is started from inetd/xinetd (trying to kill anyway)."
	fi
	echo -n "Stopping ProFTPD ftp daemon: "
	signal stop
	;;

    reload)
	echo -n "Reloading $NAME configuration..."
	signal reload
	echo " done."
	;;

    force-reload|restart)
	if [ "x$RUN" = "xyes" ] ; then
	    echo -n "Restarting ProFTPD ftp daemon."
	    signal stop
	    echo -n "."
	    sleep 2
	    echo -n "."
	    start
	    echo " done."
	else
	    if [ "x$INETD" = "xyes" ] ; then
		echo "ProFTPd is started from inetd."
	    else 
	    	echo "ProFTPd warning: cannot start neither in standalone nor in inetd/xinetd mode. Check your configuration."
	    fi
	fi
	;;

    *)
	echo "Usage: /etc/init.d/$NAME {start|force-start|stop|force-stop|reload|restart|force-reload}"
	exit 1
	;;
esac

exit 0
