#!/bin/sh

# PROVIDE: wedgetail
# REQUIRE: NETWORK SERVERS
# KEYWORD: shutdown

# 
# Add the following lines to /etc/rc.conf to enable or configure wedgetail:
# wedgetail_enable (bool):	Set to "NO" by default.
#				Set it to "YES" to enable wedgetail.

# wedgetail_path (str):         Path to the wedgetail install directory.
#                               Set to "/opt/wedgetail" by default.

# wedgetail_user (str):         User ID to use to run wedgetail. Default is "nobody".
#                               This user must be able to read server.ini, read your
#                               certificate files, and write your logfile and pidfile.
#                               If you need to open a low port number you can set this
#                               to "root" and use the uid and gid options in server.ini
#                               to downgrade privileges after opening the socket.

# wedgetail_pid (str):          Location of the pidfile. Defaults to
#                               /var/run/wedgetail/wedgetail.pid. You must configure
#                               pidFile in server.ini to match.
#                               Note that this file must be writable by wedgetail_user
#                               or the server will abort.


# NOTE: YOU ALSO MUST CUSTOMIZE server.ini IN THE WEDGETAIL DIRECTORY.

. /etc/rc.subr

name="wedgetail"
rcvar=wedgetail_enable

load_rc_config $name
: ${wedgetail_enable:=no}
: ${wedgetail_path:=/opt/wedgetail}
: ${wedgetail_user=nobody}
: ${wedgetail_pid}=/var/run/wedgetail/wedgetail.pid

pidfile="${wedgetail_pid}"    
required_dirs="${wedgetail_path}"
required_files="${wedgetail_path}/server.ini /usr/local/bin/node"
required_vars=wedgetail_enable

sig_reload="HUP"
extra_commands="reload"

command="/usr/local/bin/node"
command_args="server.js &"
wedgetail_chdir=${wedgetail_path}

start_precmd=wedgetail_prestart

wedgetail_prestart() {
    # Create the pid directory if needed. Useful if /var/run is cleaned out
    # on reboots.
    piddir=`dirname ${pidfile}`
    if [ ! -d $piddir ]; then
	mkdir -p $piddir
    fi
    # Make sure the wedgetail user can access this directory
    chown $wedgetail_user $piddir
    chmod u+w $piddir
}

run_rc_command "$1"

