#!/bin/sh
#
# pureftpd	PureFTPD server
#
# chkconfig:	345 85 15
# description:	PureFTPD is fast, production-quality, standard-conformant FTP server
#

# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/pure-ftpd ] && . /etc/sysconfig/pure-ftpd

# Check for available parsers
if [ -x /usr/sbin/pure-config ] ; then
	CFG=/usr/sbin/pure-config
elif [ -x /usr/sbin/pure-config.pl -a -x /usr/bin/perl ] ; then
	CFG=/usr/sbin/pure-config.pl
elif [ -x /usr/sbin/pure-config.py -a -x /usr/bin/python ] ; then
	CFG=/usr/sbin/pure-config.py
else
	echo 'Error: pure-config{,.pl,.py} not found. Giving up.'
	exit 1
fi

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down pure-ftpd
		exit 1
	fi
else
	exit 0
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/pure-ftpd ]; then
		msg_starting pure-ftpd
		rm -f /var/run/pure-ftpd/client*
		if [ x"$CFG" = "x/usr/sbin/pure-config" ] ; then
			daemon /usr/sbin/pure-ftpd \
				$(/usr/sbin/pure-config -f /etc/ftpd/pureftpd.conf) \
				--daemonize
			RETVAL=$?
		else
			daemon $CFG /etc/ftpd/pureftpd.conf --daemonize
			RETVAL=$?
		fi
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/pure-ftpd
	else
		msg_already_running pure-ftpd
	fi
	;;
  stop)
	# Stop daemons.
	if [ -f /var/lock/subsys/pure-ftpd ]; then
		msg_stopping pure-ftpd
		killproc pure-ftpd
		rm -f /var/lock/subsys/pure-ftpd > /dev/null 2>&1
	else
		msg_not_running pure-ftpd
	fi
	;;
  status)
	status pure-ftpd
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		pure-ftpwho
	fi
	;;
  restart|force-reload)
	$0 stop
	$0 start
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
	;;
esac

exit $RETVAL
