1172070Smlaier#!/bin/sh
2172070Smlaier#
3172070Smlaier# $FreeBSD$
4172070Smlaier#
5172070Smlaier
6172070Smlaier# PROVIDE: ftp-proxy
7172070Smlaier# REQUIRE: DAEMON pf
8180564Sdougb# KEYWORD: shutdown
9172070Smlaier
10172070Smlaier. /etc/rc.subr
11172070Smlaier
12172070Smlaiername="ftpproxy"
13230099Sdougbrcvar="ftpproxy_enable"
14172070Smlaiercommand="/usr/sbin/ftp-proxy"
15172070Smlaier
16172070Smlaierload_rc_config $name
17274327Sjpaetzel
18274327Sjpaetzel#
19274327Sjpaetzel# manage_pid argument
20274327Sjpaetzel#	Create or remove a pidfile manually, for daemons that can't be bothered
21274327Sjpaetzel#	to do it themselves. Takes one argument, which is the argument provided
22274327Sjpaetzel#	to the rc script. The pidfile will be named /var/run/<$name>.pid,
23274327Sjpaetzel#	unless $pidfile is defined.
24274327Sjpaetzel#
25274327Sjpaetzel#	The method used to determine the pid is rather hacky; grep ps output to
26274327Sjpaetzel#	find '$procname|$command', then grep for ${name}_flags. If at all
27274327Sjpaetzel#	possible, use another method if at all possible, to avoid that dirty-
28274327Sjpaetzel#	code feeling.
29274327Sjpaetzel#
30274327Sjpaetzelmanage_pid() {
31274327Sjpaetzel	local search_string ps_pid
32274327Sjpaetzel	case $1 in
33274327Sjpaetzel		*start)
34274327Sjpaetzel			cmd_string=`basename ${procname:-${command}}`
35274327Sjpaetzel			eval flag_string=\"\$${name}_flags\"
36274327Sjpaetzel			# Determine the pid.
37274327Sjpaetzel			ps_pid=`ps ax -o pid= -o command= | grep $cmd_string | grep -e "$flag_string" | grep -v grep | awk '{ print $1 }'`
38274327Sjpaetzel			# Write the pidfile depending on $pidfile status.
39274327Sjpaetzel			echo $ps_pid > ${pidfile:-"/var/run/$name.pid"}
40274327Sjpaetzel	       		;;
41274327Sjpaetzel		stop)
42274327Sjpaetzel	       		rm $pidfile
43274327Sjpaetzel	       		;;
44274327Sjpaetzel	esac
45274327Sjpaetzel}
46274327Sjpaetzel
47274327Sjpaetzel# Allow ftp-proxy to start up in two different ways. The typical behavior
48274327Sjpaetzel# is to start up one instance of ftp-proxy by setting ftpproxy_enable and
49274327Sjpaetzel# ftpproxy_flags. The alternate behavior allows multiple instances of ftp-
50274327Sjpaetzel# proxy to be started, allowing different types of proxy behavior. To use the
51274327Sjpaetzel# new behavior, a list of instances must be defined, and a list of flags for
52274327Sjpaetzel# each instance. For example, if we want to start two instances of ftp-proxy,
53274327Sjpaetzel# foo and bar, we would set the following vars.
54274327Sjpaetzel#	ftpproxy_enable="YES"
55274327Sjpaetzel#	ftpproxy_instances="foo bar"
56274327Sjpaetzel#	ftpproxy_foo="<arguments for foo>"
57274327Sjpaetzel#	ftpproxy_bar="<arguments for bar>"
58274327Sjpaetzel#
59274327Sjpaetzel# Starting more than one ftp-proxy?
60274327Sjpaetzelif [ "$ftpproxy_instances" ] && [ -n "${ftpproxy_instances}" ]; then
61274327Sjpaetzel	# Iterate through instance list.
62274327Sjpaetzel	for i in $ftpproxy_instances; do
63274327Sjpaetzel		#eval ftpproxy_${i}_flags=\$ftpproxy_${i}
64274327Sjpaetzel		#eval name=ftpproxy_${i}
65274327Sjpaetzel		# Set flags for this instance.
66274327Sjpaetzel		eval ftpproxy_flags=\$ftpproxy_${i}
67274327Sjpaetzel		# Define a unique pid file name.
68274327Sjpaetzel		pidfile="/var/run/ftp-proxy.$i.pid"
69274327Sjpaetzel		run_rc_command "$1"
70274327Sjpaetzel		manage_pid $1
71274327Sjpaetzel	done
72274327Sjpaetzelelse
73274327Sjpaetzel	# Traditional single-instance behavior
74274327Sjpaetzel	run_rc_command "$1"
75274327Sjpaetzelfi
76