#!/bin/sh 

# Start the proftpd FTP daemon.

PATH=/bin:/usr/bin:/sbin:/usr/sbin

# Defaults
RUN="no"
OPTIONS=""
CONF=/etc/proftpd.conf

test -f $DAEMON || exit 0

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

if [ ! -r $CONF ]; then
	echo "Missing configuration file $CONF"
	exit 0
fi

NAME=proftpd
DAEMON=/usr/sbin/$NAME

PIDFILE=$(grep -i 'pidfile' $CONF | sed -e 's/pidfile[\t ]\+//i')
if [ "x$PIDFILE" = "x" ];
then
	PIDFILE=/var/run/$NAME.pid
fi

trap "" 1
trap "" 15


#
# Test if configuration file provided is ok
#
$DAEMON -t -c $CONF >/dev/null 2>&1
if [ $? -ne 0 ]; then
	ISOK="$NAME"$($DAEMON -t 2>&1|grep ' - ')
else
	ISOK="yes"
fi

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

# /var/run could be on a tmpfs
[ ! -d /var/run/proftpd ] && mkdir /var/run/proftpd

start()
{
    if [ "$ISOK" != "yes" ]; then
	echo "$ISOK"
    else
        if start-stop-daemon --start --quiet --pidfile "$PIDFILE" --exec $DAEMON -- $OPTIONS -c $CONF; then
           echo "$NAME"
        else
           echo "failed"
        fi
    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 "failed"
	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 
	        if [ "x$ENABLED" = "xyes" ]; then
	    	  echo "ProFTPd warning: not start neither in standalone nor in inetd/xinetd mode, apparently. Check your configuration."
		else
	    	  echo "ProFTPd disabled by admin. See /etc/default/proftpd."
		fi
	    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 
	        if [ "x$ENABLED" = "xyes" ]; then
	    	  echo "ProFTPd warning: not started neither in standalone nor in inetd/xinetd mode, apparently. Check your configuration."
		else
	    	  echo "ProFTPd disabled by admin. See /etc/default/proftpd."
		fi
	    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)
	if [ "$ISOK" != "yes" ]; then
		echo "$ISOK"
	else
		echo -n "Reloading $NAME configuration..."
		signal reload
		echo " done."
    	fi
	;;

    force-reload|restart)
	if [ "x$RUN" = "xyes" ] ; then
	    if [ "$ISOK" != "yes" ]; then
	        echo "$ISOK"
	    else
	   	echo -n "Restarting ProFTPD ftp daemon."
	   	signal stop
	   	echo -n "."
	    	sleep 2
	    	echo -n "."
	    	start
	    	echo " done"
	    fi
	else
	    if [ "x$INETD" = "xyes" ] ; then
		echo "ProFTPd is started from inetd."
	    else 
	        if [ "x$ENABLED" = "xyes" ]; then
	    	  echo "ProFTPd warning: not started neithr in standalone nor in inetd/xinetd mode, apparently. Check your configuration."
		else
	    	  echo "ProFTPd disabled by admin. See /etc/default/proftpd."
		fi
	    fi
	fi
	;;

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

exit 0
