#!/sbin/openrc-run

description="ProFTPD FTP daemon"

DAEMON=/usr/sbin/proftpd
NAME=proftpd
CONFIG_FILE=/etc/proftpd/proftpd.conf

RUN=yes
OPTIONS=""
INETD=no
PIDFILE=/run/proftpd.pid

extra_commands="force_start force_stop force_reload check_config"

depend() {
	need net localmount
	use logger dns
	after boot
	provide proftpd
}

_load_defaults() {
	[ -r /etc/default/proftpd ] && . /etc/default/proftpd
	PIDFILE=$(grep -i 'pidfile' "$CONFIG_FILE" 2>/dev/null \
		| sed -e 's/[\t ]*pidfile[\t ]\+//i' | head -1)
	[ -n "$PIDFILE" ] || PIDFILE=/run/proftpd.pid
}

_detect_server_type() {
	RUN=yes
	INETD=no
	if grep -qi "^[[:space:]]*ServerType.*standalone" "$CONFIG_FILE" 2>/dev/null; then
		return 0
	fi
	if grep -qiE "server[[:space:]]*=[[:space:]]*/usr/sbin/(in\.)?proftpd" /etc/xinetd.conf 2>/dev/null || \
	   grep -qiE "server[[:space:]]*=[[:space:]]*/usr/sbin/(in\.)?proftpd" /etc/xinetd.d/* 2>/dev/null || \
	   grep -qiE "^ftp.*/usr/sbin/(in\.)?proftpd" /etc/inetd.d/* 2>/dev/null || \
	   grep -qiE "^ftp.*/usr/sbin/(in\.)?proftpd" /etc/inetd.conf 2>/dev/null; then
		RUN=no
		INETD=yes
	elif ! grep -qi "^[[:space:]]*ServerType.*inetd" "$CONFIG_FILE" 2>/dev/null; then
		RUN=yes
		INETD=no
	else
		RUN=no
		INETD=no
	fi
}

_ensure_rundir() {
	[ ! -d /run/proftpd ] && mkdir /run/proftpd
	[ -x /sbin/restorecon ] && /sbin/restorecon /run/proftpd
}

inetd_check() {
	if [ ! -x /usr/sbin/inetd ] && [ ! -x /usr/sbin/xinetd ] && \
	   [ ! -x /usr/sbin/inetutils-inetd ]; then
		ewarn "Neither inetd nor xinetd appears installed: check your configuration."
	fi
}

start_standalone() {
	ebegin "Starting ftp server"
	if start-stop-daemon --start --quiet --pidfile "$PIDFILE" --oknodo \
		--exec "$DAEMON" -- -c "$CONFIG_FILE" $OPTIONS; then
		eend 0
	else
		eend 1
		return 1
	fi
}

_do_stop() {
	local allow_fail="${1:-0}"

	ebegin "Stopping ftp server"
	if [ -f "$PIDFILE" ]; then
		if start-stop-daemon --stop --signal TERM --quiet --pidfile "$PIDFILE" \
			--retry TERM/30/KILL/60; then
			eend 0
		elif start-stop-daemon --stop --signal TERM --quiet --pidfile "$PIDFILE" \
			--retry TERM/30/KILL/60; then
			eend 0
		else
			eend 1
			[ "$allow_fail" = "0" ] || return 0
			return 1
		fi
		rm -f "$PIDFILE"
	else
		eend 0
	fi
}

start_pre() {
	[ -x "$DAEMON" ] || return 0
	_load_defaults
	_detect_server_type
	_ensure_rundir
	return 0
}

start() {
	start_pre || return $?
	if [ "x$RUN" = "xyes" ]; then
		start_standalone
	elif [ "x$INETD" = "xyes" ]; then
		ewarn "ProFTPD is started from inetd/xinetd."
		inetd_check
	else
		ewarn "ProFTPD warning: cannot start neither in standalone nor in inetd/xinetd mode. Check your configuration."
	fi
}

stop() {
	start_pre || return $?
	if [ "x$RUN" = "xyes" ]; then
		_do_stop 0
	elif [ "x$INETD" = "xyes" ]; then
		ewarn "ProFTPD is started from inetd/xinetd."
		inetd_check
	else
		ewarn "ProFTPD warning: cannot start neither in standalone nor in inetd/xinetd mode. Check your configuration."
	fi
}

reload() {
	_load_defaults
	_detect_server_type
	ebegin "Reloading ftp server"
	if [ -f "$PIDFILE" ]; then
		start-stop-daemon --stop --signal HUP --quiet --pidfile "$PIDFILE"
		eend $?
		return $?
	fi
	eend 1
	return 7
}

restart() {
	_load_defaults
	_detect_server_type
	if [ "x$RUN" = "xyes" ]; then
		_do_stop 1
		start_standalone
	elif [ "x$INETD" = "xyes" ]; then
		ewarn "ProFTPD is started from inetd/xinetd."
		inetd_check
	else
		ewarn "ProFTPD warning: cannot start neither in standalone nor in inetd/xinetd mode. Check your configuration."
	fi
}

force_start() {
	start_pre || return $?
	if [ "x$INETD" = "xyes" ]; then
		ewarn "Warning: ProFTPD is started from inetd/xinetd (trying to start anyway)."
		inetd_check
	fi
	start_standalone
}

force_stop() {
	start_pre || return $?
	if [ "x$INETD" = "xyes" ]; then
		ewarn "Warning: ProFTPD is started from inetd/xinetd (trying to kill anyway)."
		inetd_check
	fi
	_do_stop 0
}

force_reload() {
	restart
}

check_config() {
	if "$DAEMON" -t >/dev/null; then
		echo "ProFTPD configuration OK"
		return 0
	fi
	return 1
}

status() {
	_load_defaults
	_detect_server_type
	if [ "x$INETD" = "xyes" ]; then
		echo "ProFTPD is started from inetd/xinetd."
		inetd_check
		return 0
	fi
	if [ -f "$PIDFILE" ]; then
		pid=$(cat "$PIDFILE")
	else
		pid=x
	fi
	if pidof proftpd 2>/dev/null | grep -q "$pid"; then
		echo "ProFTPD is started in standalone mode, currently running."
		return 0
	fi
	echo "ProFTPD is started in standalone mode, currently not running."
	return 3
}
