#!/bin/sh

# $FreeBSD: head/devel/jenkins/files/jenkins.in 544211 2020-08-05 09:10:47Z lwhsu $
#
# PROVIDE: jenkins
# REQUIRE: LOGIN
# KEYWORD: shutdown

#
# Configuration settings for jenkins in /etc/rc.conf:
#
# jenkins_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable jenkins
#
# jenkins_args (str):
#   Extra arguments passed to start command
#
# jenkins_home (str)
#   Set to "/usr/local/jenkins" by default.
#   Set the JENKINS_HOME variable for jenkins process
#
# jenkins_java_home (str):
#   Set to "/usr/local/openjdk8" by default.
#   Set the Java virtual machine to run jenkins
#
# jenkins_java_opts (str):
#   Set to "" by default.
#   Java VM args to use.
#
# jenkins_user (str):
#   Set to "jenkins" by default.
#   User to run jenkins as.
#
# jenkins_group (str):
#   Set to "jenkins" by default.
#   Group for data file ownership.
#
# jenkins_log_file (str):
#   Set to "/var/log/jenkins.log" by default.
#   Log file location.
#

. /etc/rc.subr

name=jenkins
desc="Jenkins automation server"
rcvar=jenkins_enable

load_rc_config "${name}"

: ${jenkins_enable:=NO}
: ${jenkins_home="/usr/local/jenkins"}
: ${jenkins_args="--webroot=${jenkins_home}/war"}
: ${jenkins_java_home="/usr/local/openjdk8"}
: ${jenkins_user="jenkins"}
: ${jenkins_group="jenkins"}
: ${jenkins_log_file="/var/log/jenkins.log"}

pidfile=/var/run/jenkins/jenkins.pid
command=/usr/sbin/daemon
java_cmd="${jenkins_java_home}/bin/java"
procname="${java_cmd}"
command_args="-p ${pidfile} ${java_cmd} -Xmx1g -DJENKINS_HOME=${jenkins_home} ${jenkins_java_opts} -jar /usr/local/share/jenkins/jenkins.war ${jenkins_args} >> ${jenkins_log_file} 2>&1"
required_files="${java_cmd}"

start_precmd=jenkins_prestart
start_cmd=jenkins_start

jenkins_prestart()
{
	if [ ! -f "${jenkins_log_file}" ]; then
		install -o "${jenkins_user}" -g "${jenkins_group}" -m 640 /dev/null "${jenkins_log_file}"
	fi
	if [ ! -d "/var/run/jenkins" ]; then
		install -d -o "${jenkins_user}" -g "${jenkins_group}" -m 750 "/var/run/jenkins"
	fi
}

jenkins_start()
{
	check_startmsgs && echo "Starting ${name}."
	su -l ${jenkins_user} -c "exec ${command} ${command_args} ${rc_arg}"
}

run_rc_command "$1"
