#!/bin/sh
#
# /etc/init.d/wu-ftpd  --  start/stop the wu-ftpd FTP daemon.

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

unset LANG
trap "" 1 15
test -x /usr/sbin/wu-ftpd || exit 0

RUN_DAEMON=yes
WU_OPTIONS="-l"
if [ -f /etc/default/wu-ftpd ]; then
    . /etc/default/wu-ftpd
fi

WU_OPTIONS="-S $WU_OPTIONS"

run_wu="1"

if [ "x$RUN_DAEMON" = "xno" ]; then
    run_wu=0
fi

# check that the FTP service isn't already enabled in inetd
if [ -f /etc/inetd.conf ] && egrep '^ftp[[:space:]][[:space:]]*' /etc/inetd.conf >/dev/null; then
    run_wu=0
fi

for FILE in /etc/xinetd.d/*netd.d; do
    if [ -f $FILE ] && egrep 'server[[:space:]]*=.*ftpd' $FILE > /dev/null; then
        run_wu=0                                                              
    fi
done

case "$1" in
    start)
        if [ "$run_wu" = "1" ]; then
            echo -n "Starting FTP server: wu-ftpd"
            start-stop-daemon --start --quiet --pidfile /var/run/wu-ftpd.pid \
                --exec /usr/sbin/wu-ftpd -- $WU_OPTIONS && echo "."
        fi
        ;;
    stop)
        if [ -f /var/run/wu-ftpd.pid ] && kill -0 `cat /var/run/wu-ftpd.pid` 2>/dev/null; then
            echo -n "Stopping FTP server: wu-ftpd"
            kill -3 `cat /var/run/wu-ftpd.pid` >/dev/null 2>&1 && echo "."
        fi
        ;;
    restart|force-reload)
        if [ "$run_wu" = "1" ]; then
            echo -n "Restarting FTP server: wu-ftpd"
            $0 stop >/dev/null && echo -n "."
            sleep 2 && echo -n "."
            $0 start >/dev/null && echo ".done."
        fi
        ;;
    *)
        echo "Usage: /etc/init.d/wu-ftpd {start|stop|restart|force-reload}"
        exit 1
        ;;
esac

exit 0
