#!/bin/sh

# PROVIDE: guacd
# REQUIRE: LOGIN
#
# Add these lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# guacd_enable (bool):		Set to "NO" by default.
#				Set it to "YES" to enable guacd.
# guacd_home (str):		Home directory for guacd to use
#				Default to "/usr/local/guacamole" created by the port
# guacd_user (str):		User to run guacd as
#				Default to "guacamole" created by the port
# guacd_group (str):		Group to run guacd as
#				Default to "guacamole" created by the port
# guacd_pidfile (str):		Set to "" by default will use the path

. /etc/rc.subr

name=guacd
rcvar=${name}_enable

load_rc_config ${name}

: ${guacd_enable:="NO"}
: ${guacd_home:="/usr/local/guacamole"}
: ${guacd_user:="guacamole"}
: ${guacd_group:="guacamole"}
: ${guacd_pidfile:="/var/run/${name}/${name}.pid"}

guacd_env="$guacd_env HOME=$guacd_home"

command=/usr/local/sbin/guacd
pidfile=${guacd_pidfile}

command_args="-p ${pidfile}"

start_precmd="guacd_prestart"
guacd_prestart() {
        if [ -f ${pidfile} ]; then
                rm -f ${pidfile}
                echo "Removing stale pidfile."
        elif [ ! -d ${pidfile%/*} ]; then
                install -d -o ${guacd_user} -g ${guacd_group} ${pidfile%/*}
        fi
}


run_rc_command "$1"
