#!/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 /usr/local/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=/usr/local/bin/python3.9
standalone=/usr/local/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}" | /usr/bin/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."
    /usr/bin/touch ${pidfile} && /usr/sbin/chown ${viewvc_user} ${pidfile}
    /usr/sbin/daemon -cf -p ${pidfile} -u ${viewvc_user} ${standalone} ${viewvc_flags}
}

viewvc_cleanup() {
    [ -f ${pidfile} ] && /bin/rm -f ${pidfile}
}

run_rc_command "$1"
