# -*- 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

name                        apcupsd
version                     3.14.14
revision                    2
categories                  sysutils
license                     GPL-2
maintainers                 {@stephenreay koalephant.com:stephen} openmaintainer
description                 APC UPS Daemon
long_description            Apcupsd can be used for power mangement and controlling most of APC’s UPS models on Unix and Windows machines.
homepage                    http://www.apcupsd.org
master_sites                https://jaist.dl.sourceforge.net/project/apcupsd/apcupsd%20-%20Stable/${version}/
checksums                   rmd160  ecab483f1a38d1cfb75c6d439f89a611efcc45f2 \
                            sha256  db7748559b6b4c3784f9856561ef6ac6199ef7bd019b3edcd7e0a647bf8f9867 \
                            size    1843409

configure.args              --prefix=${prefix} \
                            --sbindir=${prefix}/sbin \
                            --sysconfdir=${prefix}/etc/apcupsd \
                            --disable-modbus-usb \
                            --disable-usb \
                            --disable-cgi \
                            ac_cv_func_which_gethostbyname_r=no

patchfiles                  apcagent-makefile.diff \
                            darwin-makefile.diff \
                            org.apcupsd.apcupsd.plist.in.diff \
                            apcupsd-start.in.diff

startupitem.autostart       yes
startupitem.install         yes
startupitem.custom_file     ${worksrcpath}/platforms/darwin/org.apcupsd.apcupsd.plist
startupitem.type            launchd

post-destroot {
    # Move conf file to sample so it does not get overwritten
    file rename ${destroot}${prefix}/etc/apcupsd/apcupsd.conf ${destroot}${prefix}/etc/apcupsd/apcupsd.conf.sample
}

post-activate {
    # Create initial conf file if needed
    if {![file exists ${prefix}/etc/apcupsd/apcupsd.conf]} {
        file copy ${prefix}/etc/apcupsd/apcupsd.conf.sample ${prefix}/etc/apcupsd/apcupsd.conf
   }
   # Start the Agent so the LoginItem is added
   system "open ${applications_dir}/apcagent.app"
}


variant usb description {Add USB support} {
    configure.args-replace  --disable-usb --enable-usb

    depends_lib-append      port:libusb-compat

    notes "apcupsd requires a dummy Kernel Extension to allow USB connections.
To enable it run:

    sudo ln -sf ${prefix}/Library/Extensions/ApcupsdDummy.kext /Library/Extensions/
    sudo kmutil load -p /Library/Extensions/ApcupsdDummy.kext

Then open System Preferences/System Settings, and allow the extension in the Privacy & Security pane
"

}

variant modbus description {Add MODBUS support} {
    configure.args-replace  --disable-modbus-usb --enable-modbus-usb

    depends_lib-append      port:libusb-compat
}

variant cgi description {Add CGI support} {
    configure.args-replace   --disable-cgi --enable-cgi

    configure.args-append    --with-cgi-bin=${prefix}/etc/apcupsd
}
