#!/bin/bash
#
# Startup script for the pure-ftpd FTP Server

test -f /etc/aldccp/network || exit 0
. /etc/aldccp/network

test ! x$NETWORKING = x"yes" && exit 0
test ! x$START_FTPD = x"yes" && exit 0

prog=pure-ftpd
pathprog=/usr/sbin/$prog
test -x $pathprog || exit 5

bef1=$1


start() {
if test -f /etc/pure-ftpd.conf; then
  PURE_FTPD_OPTIONS=$(egrep -v "^#|^$" /etc/pure-ftpd.conf | tr "\n" " ")

  set $PURE_FTPD_OPTIONS
  while test x$1 != x; do
    case $1 in
    "IPV4Only")
	shift
	if test x$1 = x"yes"; then opt="$opt -4"; fi
	;;
    "IPV6Only")
	shift
	if test x$1 = x"yes"; then opt="$opt -6"; fi
	;;
    "ChrootEveryone")
	shift
	if test x$1 = x"yes"; then opt="$opt -A"; fi
	;;
    "BrokenClientsCompatibility")
	shift
	if test x$1 = x"yes"; then opt="$opt -b"; fi
	;;
    "Daemonize")
	shift
#	if test x$1 = x"yes"; then opt="$opt -B"; fi
	;;
    "VerboseLog")
	shift
	if test x$1 = x"yes"; then opt="$opt -d"; fi
	;;
    "DisplayDotFiles")
	shift
	if test x$1 = x"yes"; then opt="$opt -D"; fi
	;;
    "AnonymousOnly")
	shift
	if test x$1 = x"yes"; then opt="$opt -e"; fi
	;;
    "NoAnonymous")
	shift
	if test x$1 = x"yes"; then opt="$opt -E"; fi
	;;
    "DontResolve")
	shift
	if test x$1 = x"yes"; then opt="$opt -H"; fi
	;;
    "AnonymousCanCreateDirs")
	shift
	if test x$1 = x"yes"; then opt="$opt -M"; fi
	;;
    "NATmode")
	shift
	if test x$1 = x"yes"; then opt="$opt -N"; fi
	;;
    "CallUploadScript")
	shift
	if test x$1 = x"yes"; then opt="$opt -o"; fi
	;;
    "AntiWarez")
	shift
	if test x$1 = x"yes"; then opt="$opt -s"; fi
	;;
    "AllowUserFXP")
	shift
	if test x$1 = x"yes"; then opt="$opt -w"; fi
	;;
    "AllowAnonymousFXP")
	shift
	if test x$1 = x"yes"; then opt="$opt -W"; fi
	;;
    "ProhibitDotFilesWrite")
	shift
	if test x$1 = x"yes"; then opt="$opt -x"; fi
	;;
    "ProhibitDotFilesRead")
	shift
	if test x$1 = x"yes"; then opt="$opt -X"; fi
	;;
    "AllowDotFiles")
	shift
	if test x$1 = x"yes"; then opt="$opt -z"; fi
	;;
    "AutoRename")
	shift
	if test x$1 = x"yes"; then opt="$opt -r"; fi
	;;
    "AnonymousCantUpload")
	shift
	if test x$1 = x"yes"; then opt="$opt -i"; fi
	;;
    "LogPID")
	shift
	if test x$1 = x"yes"; then opt="$opt -1"; fi
	;;
    "NoChmod")
	shift
	if test x$1 = x"yes"; then opt="$opt -R"; fi
	;;
    "KeepAllFiles")
	shift
	if test x$1 = x"yes"; then opt="$opt -K"; fi
	;;
    "CreateHomeDir")
	shift
	if test x$1 = x"yes"; then opt="$opt -j"; fi
	;;
    "NoRename")
	shift
	if test x$1 = x"yes"; then opt="$opt -G"; fi
	;;
    "CustomerProof")
	shift
	if test x$1 = x"yes"; then opt="$opt -Z"; fi
	;;
    "NoTruncate")
	shift
	if test x$1 = x"yes"; then opt="$opt -0"; fi
	;;


    "FileSystemCharset")
	shift
	opt="$opt -8$1"
	;;
    "ClientCharset")
	shift
	opt="$opt -9$1"
	;;
    "SyslogFacility")
	shift
	opt="$opt -f$1"
	;;
    "FortunesFile")
	shift
	opt="$opt -F$1"
	;;
    "ForcePassiveIP")
	shift
	opt="$opt -P$1"
	;;
    "Bind")
	shift
	opt="$opt -S$1"
	;;
    "AnonymousBandwidth")
	shift
	opt="$opt -t$1"
	;;
    "UserBandwidth")
	shift
	opt="$opt -T$1"
	;;
    "TrustedIP")
	shift
	opt="$opt -V$1"
	;;
    "AltLog")
	shift
	opt="$opt -O$1"
	;;
    "PIDFile")
	shift
	opt="$opt -g$1"
	;;


    "MaxIdleTime")
	shift
	opt="$opt -I$1"
	;;
    "MaxDiskUsage")
	shift
	opt="$opt -k$1"
	;;
    "TrustedGID")
	shift
	opt="$opt -a$1"
	;;
    "MaxClientsNumber")
	shift
	opt="$opt -c$1"
	;;
    "MaxClientsPerIP")
	shift
	opt="$opt -C$1"
	;;
    "MaxLoad")
	shift
	opt="$opt -m$1"
	;;
    "MinUID")
	shift
	opt="$opt -u$1"
	;;
    "TLS")
	shift
	opt="$opt -Y$1"
	;;
    "Umask")
	shift
	opt="$opt -U$1"
	;;
    "Quota")
	shift
	opt="$opt -n$1"
	;;
    "PerUserLimits")
	shift
	opt="$opt -y$1"
	;;

    "LimitRecursion")
	shift
	opt="$opt -L$1"
	shift
	opt="$opt:$1"
	;;
    "PassivePortRange")
	shift
	opt="$opt -p$1"
	shift
	opt="$opt:$1"
	;;
    "AnonymousRatio")
	shift
	opt="$opt -q$1"
	shift
	opt="$opt:$1"
	;;
    "UserRatio")
	shift
	opt="$opt -Q$1"
	shift
	opt="$opt:$1"
	;;

    "LDAPConfigFile")
	shift
	opt="$opt -lldap:$1"
	;;
    "MySQLConfigFile")
	shift
	opt="$opt -lmysql:$1"
	;;
    "PGSQLConfigFile")
	shift
	opt="$opt -lpgsql:$1"
	;;
    "PureDB")
	shift
	opt="$opt -lpuredb:$1"
	;;
    "ExtAuth")
	shift
	opt="$opt -lextauth:$1"
	;;
    *)
	echo "ERROR: in /etc/pure-ftpd.conf value \"$1\""
	shift
	;;
    esac

    shift
  done
fi


  echo -n $"Starting: $prog $opt"
  $pathprog -B $opt
  RETVAL=$?
#  [ $RETVAL = 0 ] && touch /var/lock/subsys/$prog
  echo

}

stop() {
  echo -n $"Stopping $prog: "
#  kill $(cat /var/run/pure-ftpd.pid)
  killall pure-ftpd
  RETVAL=$?
#  [ $RETVAL = 0 ] && rm -f /var/lock/subsys/$prog
  echo
}

#echo "$1" $opt
RETVAL=0

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	*)
		echo $"Usage: $prog {start|stop|restart}"
		RETVAL=1
esac
exit $RETVAL
