# -*- 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           github 1.0
PortGroup           legacysupport 1.0

# strnlen
legacysupport.newest_darwin_requires_legacy 10

github.setup        networkupstools nut 2.8.2 v
github.tarball_from releases
revision            1

homepage            https://www.networkupstools.org/

description         Network based UPS monitoring tool

long_description    Network UPS Tools is a collection of programs \
                    which provide a common interface for monitoring \
                    and administering UPS hardware.

categories          sysutils
maintainers         nomaintainer
# The files in scripts/perl and scripts/python are under other licenses
# but we don't use them in this port.
license             GPL-2+

checksums           rmd160  b4de79fe2a4dfe331efe59a2ec356ab6962ceee2 \
                    sha256  e4b4b0cbe7dd39ba9097be7f7d787bb2fffbe35df64dff53b5fe393d659c597d \
                    size    5599935

# nut uses telnetlib.py which was removed in Python 3.13
set python_branch   3.12
set python_version  [string map {. {}} ${python_branch}]

depends_build-append \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:libtool \
                    port:libusb-compat \
                    port:neon \
                    port:tcp_wrappers \
                    port:nss \
                    port:python${python_version}

patchfiles          dynamic_lookup-11.patch

# https://trac.macports.org/ticket/71514
patchfiles-append   patch-legacy-macos.diff

set userName        _nut
set groupName       _nut
add_users           ${userName} group=${groupName}
set stateDir        ${prefix}/var/db/ups
destroot.keepdirs   ${destroot}${stateDir}

configure.args-append \
                    --disable-silent-rules \
                    --with-serial=no \
                    --with-usb=auto \
                    --with-snmp=no \
                    --with-hal=auto \
                    --with-cgi=no \
                    --with-neonxml=no \
                    --with-ssl=yes \
                    --with-ipv6=no \
                    --with-statepath=${stateDir} \
                    --with-pidpath=${prefix}/var/run \
                    --with-hotplug-dir=${prefix}/etc/nut/hotplug \
                    --with-udev-dir=${prefix}/etc/nut/udev \
                    --without-python2 \
                    --with-python=${prefix}/bin/python${python_branch} \
                    --with-python3=${prefix}/bin/python${python_branch}

if {[getuid] == 0} {
    configure.args-append \
                    --with-user=${userName} \
                    --with-group=${groupName}
}

pre-destroot {
    if {[getuid] == 0} {
        set userargs [list -g ${groupName} -o ${userName}]
    } else {
        set userargs [list]
    }
    xinstall -d -m 0770 {*}${userargs} ${destroot}${stateDir}
}

github.livecheck.regex  {([0-9.]+)}
