#!/bin/sh
#
# PROVIDE: gnunet
# REQUIRE: DAEMON NETWORKING LOGIN
# KEYWORD: shutdown
#
# You will need to set some variables in /etc/rc.conf to start gnunet:
#
# gnunet=YES

if [ -f /etc/rc.subr ]
then
	. /etc/rc.subr
fi

name="gnunet"
rcvar=${name}
command="/usr/pkg/lib/gnunet/libexec/gnunet-service-arm"
command_args="-c /usr/pkg/etc/gnunet/gnunet.conf -d -l /var/chroot/gnunet/cache/gnunet-%Y-%m-%d.log"
required_files="/usr/pkg/etc/gnunet/gnunet.conf"
pidfile="/tmp/gnunet-gnunet-runtime/gnunet-service-arm.sock"
: ${gnunet_user:=gnunet}
: ${gnunet_group:=gnunet}
: ${gnunet_home:=/var/chroot/gnunet}
: ${gnunetdns_group:=gnunetdns}
start_cmd="gnunet_start"
start_precmd="gnunet_precmd"
stop_cmd="gnunet_stop"

# XXX: We need to export PATH with appended /usr/pkg/{sbin,bin} to get
# the upnpc binary and others.
# We can not use export here.
check_path="/usr/bin/su -m ${gnunet_user} -c '${PATH}'"
case "$check_path" in
*/usr/pkg/bin*|*/usr/pkg/sbin*)
	;;
*)
	PATH=${PATH}:/usr/pkg/bin:/usr/pkg/sbin
	;;
esac

gnunet_env="PATH=${PATH} HOME=${gnunet_home} USER=${gnunet_user} TMP=/tmp GNUNET_PREFIX=/usr/pkg GNUNET_CACHE_HOME=${gnunet_home}/cache"

check_pidfile()
{
	pid=$(pgrep -U "${gnunet_user}" "${command}"$)
	echo -n "${pid}"
}

gnunet_precmd()
{
	if [ ! -f ${required_files} ]; then
		warn "${required_files} does not exist."
		return 1
	fi
}

gnunet_start()
{
	echo "Starting ${name}."
	doit="/usr/bin/su -m ${gnunet_user} -c '${gnunet_env} ${command} ${command_args}'"
	eval $doit
}

gnunet_stop()
{
	echo "Stopping ${name}."
	doit="/usr/bin/su -m ${gnunet_user} -c '${gnunet_env} ${command} ${command_args} -e'"
	eval $doit
}


if [ -f /etc/rc.subr -a -f /etc/rc.conf -a -d /etc/rc.d -a -f /etc/rc.d/DAEMON ]; then
	# newer NetBSD
	load_rc_config $name
	run_rc_command "$1"
else
	# old NetBSD, Solaris and illumos, Linux, etc.
	pidfile="${gnunet_home}/${name}.pid"
	cmd=${1:-start}
	case ${cmd} in
	restart)
		( $0 stop )
		sleep 5
		$0 start
		;;
	stop)
		echo "Stopping ${name}."
		check_pidfile
		! [ -n ${pid} ] && eval ${stop_cmd}
		;;
	start)
		echo "Starting ${name}."
		eval ${start_precmd}
		eval ${start_cmd}
		;;
	*)
		echo 1>&2 "Usage: $0 [start|stop|restart]"
		exit 1
		;;
	esac
	exit 0
fi
