#!/bin/sh
#
# $FreeBSD: irc/inspircd12/files/inspircd.in 302141 2012-08-05 23:19:36Z dougb $
#
# PROVIDE: inspircd
# REQUIRE: LOGIN
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable inspircd:
#
# inspircd_enable="YES"
#
# Other rc.conf variables:
# inspircd_flags="<set as needed>"
#
# inspircd_config="/usr/local/etc/inspircd/inspircd.conf"
#                          -- path to config file
# inspircd_pidfile="/var/run/inspircd/inspircd.pid"
#                          -- location of pidfile: must match setting
#                             in ${inspircd_conffile}
# inspircd_user="ircd"
#                          -- user to run inspircd as
# inspircd_group="ircd"
#                          -- group to run inspircd as
# inspircd_logfile="/var/log/inspircd/ircd.log"
#                          -- file inspircd writes logs to
#
. /etc/rc.subr

name=inspircd
rcvar=inspircd_enable

load_rc_config ${name}

: ${inspircd_enable="NO"}
: ${inspircd_config="/usr/local/etc/inspircd/inspircd.conf"}
: ${inspircd_pidfile="/var/run/inspircd/inspircd.pid"}
: ${inspircd_user="ircd"}
: ${inspircd_group="ircd"}
: ${inspircd_logfile="/var/log/inspircd/ircd.log"}

command=/usr/local/bin/inspircd
pidfile=${inspircd_pidfile}
required_files=${inspircd_config}
start_precmd=inspircd_prestart

inspircd_prestart ()
{
    piddir=$(dirname ${inspircd_pidfile})
    if [ ! -d ${piddir} ]; then
        mkdir -m 755 -p ${piddir}
        chown -R ${inspircd_user}:${inspircd_group} ${piddir}
    fi
    logdir=$(dirname ${inspircd_logfile})
    if [ ! -d ${logdir} ]; then
        mkdir -m 755 -p ${logdir}
        chown -R ${inspircd_user}:${inspircd_group} ${logdir}
    fi
}

case "${inspircd_flags}" in
    *--config\ *)
    echo "Warning \$inspircd_flags includes --config option." \
        "Please use \$inspircd_config instead."
    ;;
    *--logfile\ *)
    echo "Warning \$inspircd_flags includes --logfile option." \
         "Please use \$inspircd_logfile instead."
    ;;
    *)
    inspircd_flags="${inspircd_flags} --logfile ${inspircd_logfile} --config ${inspircd_config}"
    ;;
esac

run_rc_command "$1"
