#!/bin/sh
#
# $FreeBSD$

# 
# Copyright (C) 1994-2016 The FreeBSD Project. All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

#
# PROVIDE: ntpa
# REQUIRE: networking
# KEYWORD: shutdown
#
# Add these lines to /etc/rc.conf to enable ntpa:
#
# ntpa_enable (bool):   Set to NO by default.
#                       Set it to YES to enable ntpa.
# ntpa_config (path):   Set to %%PREFIX%%/etc/ntpa/ntpa.conf
#                       by default.
# ntpa_tempdir (path):  Set to /tmp by default.
# ntpa_user (user):     Set to ntpa by default.
#
# Run additional instances of ntpa with:
# ln -s ntpa ntpa_name
#

. /etc/rc.subr

# taken from security/openvpn.
name="$file" ;

case "$0" in
/etc/rc*)
        # during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
        # so get the name of the script from $_file
        name="$_file"
        ;;
*/service)
        # do not use this as $0
        ;;
*)
        name="$0"
        ;;
esac

# default name to "ntpa" if guessing failed
# Trailing semicolon for service(8)'s benefit:
name="${name:-ntpa}" ;
name="${name##*/}"
desc="Monitors NTP daemon"
rcvar=${name}_enable

start_cmd=ntpa_start
stop_cmd=ntpa_stop
reload_cmd=ntpa_reload
configtest_cmd=ntpa_configtest

extra_commands="reload configtest"

load_rc_config ${name}

eval ": \${${name}_enable:=\"NO\"}"
eval ": \${${name}_config:=\"%%PREFIX%%/etc/ntpa/${name}.conf\"}"
eval ": \${${name}_tempdir:=\"/tmp/\"}"
eval ": \${${name}_user:=\"ntpa\"}"

config="$(eval echo \${${name}_config})"
tempdir="$(eval echo \${${name}_tempdir})"
ntpauser="$(eval echo \${${name}_user})"

pid_dir=/var/run/ntpa
pidfile="$pid_dir/${name}.pid"

ntpa_start()
{
    if [ ! -d "$pid_dir" ]; then
        install -m 0775 -g $ntpauser -o $ntpauser -d "$pid_dir"
    fi

    if [ -f ${pidfile} ]; then
        rc_pid=`cat ${pidfile}`
        echo 1>&2 "${name} already running? (pid=$rc_pid)."
        return 1
    else
        echo "Starting ${name}."
        su -m ${ntpauser} -c "sh -c '%%PREFIX%%/sbin/ntpa --config ${config} --writepid ${pidfile} --temp ${tempdir} --daemon ${name} &'"
    fi
}

ntpa_configtest()
{
    su -m ${ntpauser} -c "sh -c '%%PREFIX%%/sbin/ntpav -v ${config}'"
}

ntpa_reload()
{
    if [ ! -f ${pidfile} ]; then
        _run_rc_notrunning
        return 1
    else
        echo "Reloading ${name} configuration."
        rc_pid=`cat ${pidfile}`
        kill -USR1 $rc_pid
    fi
}

ntpa_stop()
{
    if [ ! -f ${pidfile} ]; then
        _run_rc_notrunning
        return 1
    else
        echo "Stopping ${name}."
        rc_pid=`cat ${pidfile}`
        kill -TERM $rc_pid
        wait_for_pids ${rc_pid}
    fi
}

run_rc_command "$1"
