#!/bin/sh

#############################################################################
#
# Choose "verbose mode" by setting NCFTPD_VERBOSITY to "-v" while testing,
# and then set it NCFTPD_VERBOSITY to "" when testing is complete.
#
# NCFTPD_VERBOSITY="-v"
NCFTPD_VERBOSITY=""


#############################################################################
#
# Location of NcFTPd's configuration files.
#
NCFTPD_GENERAL_CF="/usr/local/etc/ncftpd/general.cf"
NCFTPD_DOMAIN_CF="/usr/local/etc/ncftpd/domain.cf"


#############################################################################
#
# Location of the startup log.  This log is used before the logger
# process has been bootstrapped, so the startup log serves as a
# location to log errors that occur before the logger has been
# started.
#
NCFTPD_STARTUP_ERROR_LOG="/usr/local/etc/ncftpd/startup_errors"


#############################################################################
#
# Rather than running $NCFTPD_PROG directly, we set the
# PATH and use a relative path.  This makes it easy to
# spot the "ncftpd" processes in "ps" or "top".
#
# This also implies that you must properly set the variable
# $NCFTPD_PROG_PATH so it contains the directory containing
# $NCFTPD_PROG.
#
NCFTPD_PROG_PATH="/usr/local/sbin:/usr/local/bin:$PATH"
NCFTPD_PROG="/usr/local/sbin/ncftpd"


#############################################################################
# No more user-configurable options below this point.
#############################################################################

PGREP="/usr/local/bin/pgrep.pl"
PKILL="/usr/local/bin/pkill.pl"

#SOURCE-RC
if [ "$rc_done" = "" ] ; then
	rc_done="OK"
	rc_failed="FAILED"
fi

es=2
if [ "$#" -eq 0 ] ; then arg="start" ; else arg="$1" ; fi

#
# The install script may create aptly named symbolic links
# to this script.
#
case "$0" in
	*restart*)
		arg="restart"
		;;
	*start*)
		arg="start"
		;;
	*stop*)
		arg="stop"
		;;
	*reload*)
		arg="reload"
		;;
esac

case "$arg" in
'start')
	# Only proceed if everything is properly installed
	if [ -f "${NCFTPD_PROG}" ] && [ -f "${NCFTPD_GENERAL_CF}" ] && [ -f "${NCFTPD_DOMAIN_CF}" ] ; then
		es=1
		echo -n 'Starting NcFTPd: '	# ECHO-N
		oPATH="$PATH"
		PATH="${NCFTPD_PROG_PATH}"
		export PATH
		ncftpd -d ${NCFTPD_VERBOSITY} "${NCFTPD_GENERAL_CF}" "${NCFTPD_DOMAIN_CF}" > "$NCFTPD_STARTUP_ERROR_LOG" 2>&1
		es="$?"
		PATH="$oPATH"
		if [ "$es" -eq 0 ] ; then
			# Successfully backgrounded
			echo -e "$rc_done"	# ECHO-E
		else
			echo -e "$rc_failed"	# ECHO-E
		fi
	fi
	;;

'stop')
	# Only proceed if everything is properly installed
	if [ -f "${NCFTPD_PROG}" ] && [ -f "${NCFTPD_GENERAL_CF}" ] && [ -f "${NCFTPD_DOMAIN_CF}" ] ; then
		#
		# This tries to figure out what you set "pid-file" to.
		#
		es=1
		echo -n 'Stopping NcFTPd: '	# ECHO-N
		NCFTPD_STOP_SCRIPT=`grep '^pid-file' "${NCFTPD_GENERAL_CF}" | cut -d= -f2`
		if [ -x "$NCFTPD_STOP_SCRIPT" ] ; then
			/bin/sh "$NCFTPD_STOP_SCRIPT"
			es=0
		fi
		if [ -x "$PKILL" ] ; then
			es=1
			sig="-15"
			for tries in 1 2 3 ; do
				"$PKILL" "$sig" -X $$ -x ncftpd
				if [ $? -eq 1 ] ; then
					# No processes remaining, done
					es=0
					break
				fi
				sleep 3
				sig="-9"
			done
		fi
		if [ "$es" -eq 0 ] ; then
			echo -e "$rc_done"	# ECHO-E
		else
			echo -e "$rc_failed"	# ECHO-E
		fi
	fi
	;;

'restart'|'force-reload')
	if [ -f "${NCFTPD_PROG}" ] && [ -f "${NCFTPD_GENERAL_CF}" ] && [ -f "${NCFTPD_DOMAIN_CF}" ] ; then
		es=1
		echo -n 'Restarting NcFTPd: '	# ECHO-N
		# First stop...
		stopped="yes"
		NCFTPD_STOP_SCRIPT=`grep '^pid-file' "${NCFTPD_GENERAL_CF}" | cut -d= -f2`
		if [ -x "$NCFTPD_STOP_SCRIPT" ] ; then
			/bin/sh "$NCFTPD_STOP_SCRIPT"
		fi
		if [ -x "$PKILL" ] ; then
			sig="-15"
			stopped="no"
			for tries in 1 2 3 ; do
				"$PKILL" "$sig" -X $$ -x ncftpd
				if [ $? -eq 1 ] ; then
					# No processes remaining, done
					stopped="yes"
					break
				fi
				sleep 3
				sig="-9"
			done
		fi

		# Then start.
		if [ "$stopped" = yes ] ; then
			oPATH="$PATH"
			PATH="${NCFTPD_PROG_PATH}"
			export PATH
			ncftpd -d ${NCFTPD_VERBOSITY} "${NCFTPD_GENERAL_CF}" "${NCFTPD_DOMAIN_CF}" > "$NCFTPD_STARTUP_ERROR_LOG" 2>&1
			es="$?"
			PATH="$oPATH"
		else
			echo "Could not stop previous NcFTPd instance." 1>&2
			es=1
		fi
		if [ "$es" -eq 0 ] ; then
			# Successfully backgrounded
			echo -e "$rc_done"	# ECHO-E
		else
			echo -e "$rc_failed"	# ECHO-E
		fi
	fi
	;;

'reload')
	if [ -f "${NCFTPD_PROG}" ] && [ -f "${NCFTPD_GENERAL_CF}" ] && [ -f "${NCFTPD_DOMAIN_CF}" ] ; then
		es=1
		#
		# NOTE: Currently only the domain.cf can be reloaded.
		# If you change the general.cf, you have to do a full restart.
		#
		echo -n 'Reloading NcFTPd domains: '	# ECHO-N
		NCFTPD_STOP_SCRIPT=`grep '^pid-file' "${NCFTPD_GENERAL_CF}" | cut -d= -f2`
		if [ -x "$NCFTPD_STOP_SCRIPT" ] ; then
			sed -n '/main/{s/-15/-1/;p;q;}' "$NCFTPD_STOP_SCRIPT" | /bin/sh
			es=0
		fi
		if [ "$es" -eq 0 ] ; then
			echo -e "$rc_done"	# ECHO-E
		else
			echo -e "$rc_failed"	# ECHO-E
		fi
	fi
	;;
#EXTRA

*)
	echo "Usage: $0 { start | stop | restart | reload }"
	es=2
	;;
esac

exit "$es"
