#!/bin/sh

# Shell in a Box Daemon startup script
#
# PROVIDE: shellinaboxd
# REQUIRE: LOGIN
# KEYWORD: shutdown

#
# Add the following to /etc/rc.conf[.local] to enable this service
#
# shellinaboxd_enable="YES"
#
# You can fine tune others variables too:
# shellinaboxd_pidfile="/var/run/shellinabox.pid"
# shellinaboxd_user="%%USERS%%"
# shellinaboxd_group="%%GROUPS%%"
# shellinaboxd_port="4200"
# shellinaboxd_certdir="%%ETCDIR%%"
# shellinaboxd_certfile=
# 	Example: shellinaboxd_certfile="/your/cert.pem"
# shellinaboxd_flags=

shellinaboxd_precmd() {
	if command -v check_namevarlist > /dev/null 2>&1; then
		check_namevarlist fib && return 0
	fi

	${SYSCTL} net.fibs >/dev/null 2>&1 || return 0

	shellinaboxd_fib=${shellinaboxd_fib:-"NONE"}
	case "$shellinaboxd_fib" in
	[Nn][Oo][Nn][Ee])
		;;
	*)
		command="setfib -F ${shellinaboxd_fib} ${command}"
		;;
	esac
} 

. /etc/rc.subr

name="shellinaboxd"
rcvar=shellinaboxd_enable

command="%%PREFIX%%/bin/${name}"
start_precmd="shellinaboxd_precmd"

load_rc_config $name

shellinaboxd_enable=${shellinaboxd_enable:-"NO"}
shellinaboxd_user=${shellinaboxd_user:-"%%USERS%%"}
shellinaboxd_group=${shellinaboxd_group:-"%%GROUPS%%"}
shellinaboxd_port=${shellinaboxd_port:-"4200"}
shellinaboxd_certdir=${shellinaboxd_certdir:-"%%ETCDIR%%"}

pidfile=${shellinaboxd_pidfile:-"/var/run/shellinaboxd.pid"}

command_args="--user=${shellinaboxd_user} --group=${shellinaboxd_group} --port=${shellinaboxd_port} --background=${pidfile}"

if [ "${shellinaboxd_certfile}" = "" ]; then
	required_dirs="${shellinaboxd_certdir}"
	command_args="$command_args --cert=${shellinaboxd_certdir}"
else
	command_args="$command_args --cert-fd=3 3< ${shellinaboxd_certfile}"
fi

# avoid unnecessary su(1) initiated by rc.subr
unset shellinaboxd_user shellinaboxd_group

run_rc_command "$1"
