# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           java 1.0

name                jenkins
version             2.555.1
revision            0
categories          devel java
maintainers         {gmail.com:slashapp @slashapp} openmaintainer

homepage            https://jenkins.io
description         The leading open source automation server,\
                    Jenkins provides hundreds of plugins to support building,\
                    deploying and automating any project.
long_description    ${description}

platforms           {darwin any}
supported_archs     noarch
license             MIT

master_sites        https://get.jenkins.io/war-stable/${version}/
dist_subdir         ${name}/${version}
set jenkins_war     ${name}.war
distfiles           ${jenkins_war}

checksums           rmd160  1a946ff985bda37806854a0ab93e06f3b980ba32 \
                    sha256  2608ba58c6a56c653f0abf5ac62771949a217437df299dff39083be37cb7bd03 \
                    size    100125231

java.version        11+
java.fallback       openjdk11

use_configure       no
extract {}
build {}
test {}

set jenkins_conf_dir    ${prefix}/etc/${name}
set jenkins_conf        ${jenkins_conf_dir}/${name}.conf
set jenkins_conf_sample ${jenkins_conf}.sample
set jenkins_exec_dir    ${prefix}/libexec/${name}
set jenkins_home_dir    ${prefix}/var/lib/${name}
set jenkins_log_dir     ${prefix}/var/log/${name}
set jenkins_run_dir     ${prefix}/var/run/${name}
set jenkins_exec        ${jenkins_exec_dir}/${jenkins_war}
set jenkins_log         ${jenkins_log_dir}/${name}.log
set jenkins_pid         ${jenkins_run_dir}/${name}.pid
set jenkins_host        127.0.0.1
set jenkins_port        8080
set jenkins_user        jenkins
set jenkins_group       jenkins

set jenkins_dir_list [list \
    ${jenkins_exec_dir} \
    ${jenkins_home_dir} \
    ${jenkins_log_dir} \
    ${jenkins_run_dir} \
]

set jenkins_exec_list [list \
    ${distpath}/${jenkins_war}
]

foreach d ${jenkins_dir_list} {
    destroot.keepdirs-append \
        ${destroot}${d}
}

destroot {
    foreach d ${jenkins_dir_list} {
        xinstall -d -m 0755 -g ${jenkins_group} -o ${jenkins_user} \
            ${destroot}${d}
    }

    foreach f ${jenkins_exec_list} {
        xinstall -m 0644 -g ${jenkins_group} -o ${jenkins_user} \
            ${f} \
            ${destroot}${jenkins_exec_dir}
    }

    xinstall -d -m 0755 ${destroot}${jenkins_conf_dir}
    xinstall -m 0644 ${filespath}/${name}.conf.sample \
        ${destroot}${jenkins_conf_sample}
}

post-activate {
    if {![file exists ${jenkins_conf}]} {
        copy ${jenkins_conf_sample} ${jenkins_conf}
    }
}

post-deactivate {
    # Cleanup all Jenkins-created files, which are significant
    foreach d ${jenkins_dir_list} {
        if {${d} eq ${jenkins_home_dir}} {
            if { [file exists "${d}/.jenkins" ]} {
                ui_msg "********************************************************"
                ui_msg "To revert the system after uninstalling the port,"
                ui_msg "delete jenkins working directory:"
                ui_msg "$ sudo rm -fr ${jenkins_home_dir}"
                ui_msg "********************************************************"
                continue
            }
        }
        if { [file exists ${d}] } {
            ui_info "post-deactivate: dir exists, deleting: ${d}"
            delete -force ${d}
        } else {
            ui_info "post-deactivate: dir doesn't exist, skipping delete: ${d}"
        }
    }
}

add_users               ${jenkins_user} \
                        group=${jenkins_group} \
                        home=${jenkins_home_dir} \
                        shell=/bin/sh \
                        realname=Jenkins

startupitem.create      yes

# Enable additional logging, for diagnostic purposes
startupitem.logevents   yes

startupitem.user        ${jenkins_user}
startupitem.group       ${jenkins_group}

# Note: To avoid breakage when a Java port is updated/removed, use 'java_home' to select at runtime
#       java_home version filter not support wildcards
set java_version [regsub {[^0-9.]} ${java.version} ""]
startupitem.init "
if test -r ${jenkins_conf}; then
    . ${jenkins_conf}
fi
: \${JENKINS_HOST:=${jenkins_host}}
: \${JENKINS_PORT:=${jenkins_port}}
: \${JENKINS_JAVA_OPTS:=}
: \${JENKINS_ARGS:=}
export JAVA_HOME=\$(/usr/libexec/java_home --failfast --version ${java_version} 2>/dev/null || /usr/libexec/java_home)
"

startupitem.start \
    "\${JAVA_HOME}/bin/java \
    \${JENKINS_JAVA_OPTS} \
    -Duser.home=${jenkins_home_dir} \
    -jar ${jenkins_exec} \
    --httpListenAddress=\${JENKINS_HOST} \
    --httpPort=\${JENKINS_PORT} \
    \${JENKINS_ARGS}"

startupitem.stop \
    "kill \$(cat ${jenkins_pid})"

startupitem.logfile \
    ${jenkins_log}

startupitem.pidfile \
    manual ${jenkins_pid}

notes-append "
To configure the Jenkins service, edit:
  - \$ sudo vi ${jenkins_conf}
Supported settings include JENKINS_HOST, JENKINS_PORT, JENKINS_JAVA_OPTS, and JENKINS_ARGS.
On activation, if ${jenkins_conf} does not exist, it will be created from:
  - ${jenkins_conf_sample}
User changes to ${jenkins_conf} are preserved across port upgrades. After upgrading,
compare ${jenkins_conf} with ${jenkins_conf_sample} for any new defaults.
Read document at https://jenkins.io/doc/book/installing/initial-settings/
Once the service is enabled, Jenkins will by default:
  - Listen by default on: http://${jenkins_host}:${jenkins_port}
The default Administrator password, created after port first loaded, will be available here:
  - \$ sudo cat ${jenkins_home_dir}/.jenkins/secrets/initialAdminPassword
"

livecheck.url "https://www.jenkins.io/download/"
livecheck.regex {war-stable/([0-9.]+)/jenkins}
