#!/bin/sh
#
# proftpd      This shell script takes care of starting and stopping proftpd.
#
# description: ProFTPd is an enhanced FTP server with a focus towards
#              simplicity, security, and ease of configuration.
#              It features a very Apache-like configuration syntax,
#              and a highly customizable server infrastructure,
#              including support for multiple 'virtual' FTP servers,
#              anonymous FTP, and permission-based directory visibility.
#
# processname: proftpd
#
# config: /etc/proftpd.conf
#
# pidfile: /var/run/proftpd/proftpd.pid
#

NAME=proftpd
PROG=${NAME}

PROG_BIN=/opt/freeware/sbin/proftpd

PIDFILE=/var/run/${NAME}/${PROG}.pid


# define some generic commands
AWK=/usr/bin/awk
CAT=/usr/bin/cat
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
KILL=/usr/bin/kill
MKDIR=/usr/bin/mkdir
PRINTF=/usr/bin/printf
PS=/usr/bin/ps
RM=/usr/bin/rm
SLEEP=/usr/bin/sleep


# check for missing binaries (stale symlinks should not happen)
test -x ${PROG_BIN} ||
    {
      $ECHO "${PROG_BIN} not installed"
      if [ "$1" = "stop" ] ; then
          exit 0
      else
          exit 5
      fi
    }


# source ProFTPD configuration
PROFTPD_OPTIONS=""
if [ -f /etc/sysconfig/${NAME} ] ; then
    . /etc/sysconfig/${NAME}
fi


# Check for existence of needed config file and read it
PROFTPD_CONFIG=/etc/${NAME}.conf
test -r ${PROFTPD_CONFIG} ||
    {
      $ECHO "${PROFTPD_CONFIG} not existing";
      if [ "$1" = "stop" ] ; then
          exit 0
      else
          exit 6
      fi
    }


case "$1" in
    start)
        if [ -r ${PIDFILE} ]; then
            pid=`$CAT ${PIDFILE}`
            if [ "`$PS -ef | $GREP -v grep | $GREP ${PROG} | $GREP ${pid} | $AWK '{ print $2 }' | $GREP ${pid}`" = "${pid}" ] ; then
                $ECHO "ProFTPD daemon is already running with PID ${pid}."
                exit 1
            else
                $RM -f ${PIDFILE}
            fi
        fi
        $PRINTF "Starting ProFTPD daemon... "

        ## start daemon and write PID to file ${PIDFILE}
        $MKDIR -p /var/run/${NAME}
        ${PROG_BIN} ${PROFTPD_OPTIONS} 2>/dev/null
        $ECHO "done."
        ;;
    stop)
        $PRINTF "Stopping ProFTPD daemon... "
        ## stop daemon
        if [ -r ${PIDFILE} ]; then
            $KILL -TERM `$CAT ${PIDFILE}`
            $RM -f ${PIDFILE}
        fi
        $ECHO "done."
        ;;
    status)
        if [ -r ${PIDFILE} ]; then
            pid=`$CAT ${PIDFILE}`
            if [ "`$PS -ef | $GREP -v grep | $GREP ${PROG} | $GREP ${pid} | $AWK '{ print $2 }' | $GREP ${pid}`" = "${pid}" ] ; then
                $ECHO "ProFTPD daemon is running with PID ${pid}."
            else
                $ECHO "ProFTPD daemon is not running."
            fi
        else
            $ECHO "ProFTPD daemon is not running."
        fi
        ;;
    condrestart)
        if [ -r ${PIDFILE} ]; then
            pid=`$CAT ${PIDFILE}`
            if [ "`$PS -ef | $GREP -v grep | $GREP ${PROG} | $GREP ${pid} | $AWK '{ print $2 }' | $GREP ${pid}`" = "${pid}" ] ; then
                $0 stop
	        $ECHO "Sleeping for 2 seconds for graceful ProFTPD shutdown ..."
                $SLEEP 2
                $0 start
            fi
        fi
        ;;
    reload)
	## force ProFTPD to re-read its configuration
        $PRINTF "Re-reading ${PROG} configuration... "
        ## send the "-HUP" signal
        if [ -r ${PIDFILE} ]; then
            $KILL -HUP `$CAT ${PIDFILE}`
        fi
        $ECHO "done."
	;;
    restart)
	## stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$ECHO "Sleeping for 2 seconds for graceful ProFTPD shutdown ..."
	$SLEEP 2
	$0 start
	;;
    *)
	$ECHO "Usage: $0 {start|stop|status|condrestart|reload|restart}"
	exit 1
	;;
esac

