# -*- 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           waf 1.0
PortGroup           python 1.0
PortGroup           openssl 1.0

name                ntpsec
version             1.2.3
revision            0
categories          sysutils net
maintainers         {fwright.net:fw @fhgwright} openmaintainer
description         A secure, hardened, and improved implementation of NTP
license             Permissive
long_description    A secure, hardened, and improved implementation of Network \
                    Time Protocol derived from NTP Classic, Dave Mills’s original.
homepage            https://www.ntpsec.org/

conflicts           ntp openntpd

master_sites        ftp://ftp.ntpsec.org/pub/releases/
checksums           rmd160  f6ee175289710afd8c053dd39a5a825d995c7a15 \
                    sha256  750b7337b3b42b5573700a8d5a483e1bf556e447549177a670c7c00fb9049375 \
                    size    2725081

# ntpsec requires Python 2.6, 2.7, or 3.3+.
# We skip 2.6 and 3.3, but keep 2.7 and 3.4+.
# Some variants may force a more restricted list.
#
set pythons_suffixes {27 34 35 36 37 38 39 310 311 312 313}

set pythons_ports {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string range ${s} 1 99]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Build ${name} to use Python ${v}" conflicts {*}${c} ""
}

# Default to +python27
#
# Since this port provides Python modules that may be used by users, changing
# the Python version may break existing user code.  Since earlier versions of
# this port had no pythonXX variant, there is no established default variant
# to avoid such a switch on an upgrade.  Hence, the default should remain at
# +python27 for a while.
#
set pyver_no_dot "27"

foreach s ${pythons_suffixes} {
    if {[variant_isset python${s}]} {
        set pyver_no_dot ${s}
    }
}
default_variants +python${pyver_no_dot}

# make sure some python variant is selected
set pyver_no_dot ""
foreach s ${pythons_suffixes} {
    if {[variant_isset python${s}]} {
        set pyver_no_dot ${s}
    }
}
pre-fetch {
    if {${pyver_no_dot} eq ""} {
        ui_error "\n\nYou must select one of the Python variants (+pythonXY).\n"
    return -code error "Invalid Python variant selection"
    }
}

python.version          ${pyver_no_dot}
waf.python_branch       ${python.branch}

# Avoid treating python311+ differently from earlier versions.
# In particular, avoid clobbering destroot.target and adding superfluous
# dependencies.
python.pep517       no

# Note that the new ffi-based Python client library doesn't work properly
# on the Mac, so we use the old extension-based version.

openssl.branch      3
# NOTE: doesn't work with libressl

depends_build-append port:bison port:pkgconfig
#depends_lib-append  port:python${pyver_no_dot}

# Consolidated patchfile, based on GitHub/fhgwright/macports-releases
patchfiles          patch-allfixes.diff

use_configure       yes
configure.post_args-delete --nocache
configure.args      --alltests \
                    --define=CONFIG_FILE=${prefix}/etc/ntp.conf \
                    --disable-manpage \
                    --python=${python.bin} \
                    --enable-pylib=ext \
                    --pyshebang=${python.bin} \
                    --pythondir=${python.pkgd} \
                    --pythonarchdir=${python.pkgd}

# Make sure we use waf, not setup.py for build
build.cmd           ${waf.python} ./waf
destroot.cmd        ${build.cmd}

# Although the normal build procedure includes the tests, we also allow
# them separately so that "port test" works.
test.run            yes
# Avoid unnecessary dependency on pyXX-test
python.test_framework
# Override the python portgroup's inappropriate test.cmd
test.cmd            ${build.cmd}
test.target         check

default_variants    +doc

# ntpsec has issues with universal builds:
# 1) It expects endianness to be single-valued at configure time.
# 2) There are problems with Python compiled extensions and universality.
universal_variant   no

variant classic description {Enable classic mode} {
    configure.args-append   --enable-classic-mode
}
variant doc description {Build manpages and HTML documentation} {
    depends_build-append    port:asciidoctor
    configure.args-append   --enable-doc --htmldir=${prefix}/share/doc/${name}
    configure.args-delete   --disable-manpage
}
variant refclock description {Enable all reference clocks} {
    configure.args-append   --refclock=all
}
variant smear description {Enable smearing of leap seconds} {
    configure.args-append   --enable-leap-smear
}

post-destroot {
    xinstall -o root -m 644 ${filespath}/ntp.conf \
        ${destroot}${prefix}/etc/ntp.conf.dist
    
    xinstall -m 755 -d \
        ${destroot}${prefix}/var/db \
        ${destroot}${prefix}/var/run
    destroot.keepdirs \
        ${destroot}${prefix}/var/db \
        ${destroot}${prefix}/var/run

    # The install script inappropriately installs the runtests program
    # See https://gitlab.com/NTPsec/ntpsec/-/issues/786
    delete "${destroot}${prefix}/bin/runtests"
}
post-activate {
    if {![file exists ${prefix}/etc/ntp.conf]} {
        file copy ${prefix}/etc/ntp.conf.dist ${prefix}/etc/ntp.conf
    }
}

notes "See \"man 8 ntpd\" for documentation; \"man ntpd\" may reflect Apple's."
notes-append "To replace Apple's ntpd,\
             disable \"Set date and time automatically\"\
             in the Date & Time Preferences."
notes-append "Configuration is in ${prefix}/etc/ntp.conf."

startupitem.create      yes
startupitem.netchange   yes
startupitem.executable  ${prefix}/sbin/ntpd -n -g -p ${prefix}/var/run/ntpd.pid -f ${prefix}/var/db/ntp.drift -c ${prefix}/etc/ntp.conf

# Match most tarball patterns, but disallow versions with dashes to filter out
# X.Y.Z-rcN, etc.
livecheck.regex ${name}-(\[^-]+)\\.tar\\.gz
