#!/bin/sh

# PROVIDE: viewvc
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following line(s) to /etc/rc.conf to enable ViewVC:
#
#  viewvc_enable="YES"
#  # optional
#  viewvc_flags="-h localhost -p PORT"
#  viewvc_user="www"
#
# To get available parameters and the default values, use the
# command %%PREFIX%%/viewvc/bin/standalone.py --help
# for more info.
#
# Note:
# If you choose a listen port less then 1024 then you have
# to use a privileged user.
# If the user www cannot access your cvs/svn repository,
# adjust the parameter viewvc_user and it should work.
#
# INFO:
# ViewVC default tcp port: 49152
# see http://viewvc.tigris.org/issues/show_bug.cgi?id=234

. /etc/rc.subr

name=viewvc
rcvar=viewvc_enable
load_rc_config $name

# Set defaults
viewvc_enable=${viewvc_enable:-"NO"}
viewvc_flags=${viewvc_flags:-"-p 49152"}
viewvc_user=${viewvc_user:-"www"}

pidfile=/var/run/viewvc.pid
command=%%PYTHON_CMD%%
standalone=%%PREFIX%%/viewvc/bin/standalone.py

start_precmd=viewvc_precmd
start_cmd=viewvc_start
stop_postcmd=viewvc_cleanup

viewvc_precmd()
{
    # with param -d we get another pid so prevent this
    %%ECHO%% "${viewvc_flags}" | %%EGREP%% -q "(^\-d| \-d)" 2>&1 > /dev/null
    if [ $? -eq 0 ]; then
        %%ECHO%% "Please remove parameter -d from viewvc_flags"
        %%ECHO%%
        return 1
    fi
}

viewvc_start() {
    echo "Starting viewvc."
    %%TOUCH%% ${pidfile} && %%CHOWN%% ${viewvc_user} ${pidfile}
    /usr/sbin/daemon -cf -p ${pidfile} -u ${viewvc_user} ${standalone} ${viewvc_flags}
}

viewvc_cleanup() {
    [ -f ${pidfile} ] && %%RM%% ${pidfile}
}

run_rc_command "$1"
