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

# _faccessat, _clock_gettime
legacysupport.newest_darwin_requires_legacy 15

name                hamclock
version             4.22
revision            1
checksums           rmd160  0851160f6b7b42cc564dd6cfb348df6ec252e57c \
                    sha256  358c30e5c296d9ce6ff6d9206d958cc2771bdd731590349a6873d501e39c8ad6 \
                    size    1967130
categories          x11
license             MIT
maintainers         {@Dave-Allured noaa.gov:dave.allured} \
                    openmaintainer

description         Handy clock with features for amateur radio operators
long_description    HamClock is a kiosk-style application that provides\
                    real time space weather, radio propagation models,\
                    operating events and other information particularly\
                    useful to the radio amateur.\
                    Update, 2026 March:  This MacPorts version uses\
                    Elwood's original code, but has been ported to the\
                    replacement backend server at hamclock.com.

# 2026 Feb 27: New home page for replacement hamclock website.
# Note: Competing websites exist.
homepage            https://hamclock.com

# For now, use Elwood's original website to fetch the original source code.
master_sites        https://www.clearskyinstitute.com/ham/HamClock/
distname            ESPHamClock-V${version}
extract.suffix      .tgz

worksrcdir          ESPHamClock

post-patch {
    # Respect build environment:
    reinplace "s|CXXFLAGS =|CXXFLAGS +=|" ${worksrcpath}/Makefile
    reinplace "s|LIBS =|LIBS +=|" ${worksrcpath}/Makefile
    reinplace "s|CXX =|CXX ?=|" ${worksrcpath}/ArduinoLib/Makefile

    # Fix X11 paths:
    reinplace "s|/opt/X11|${prefix}|g" ${worksrcpath}/Makefile

    # Switch to new backend server:
    reinplace "s|clearskyinstitute.com|hamclock.com|" ${worksrcpath}/wifi.cpp
    # No port number change this time.  Both old and new servers use port 80.
}

depends_run-append      port:desktop-file-utils

compiler.cxx_standard   2017

configure.cxxflags-append \
                        -DNO_UPGRADE

# Default build, is overwritten if any variant is selected explicitly.
build.target            hamclock-1600x960

variant x800x480        description {X11 window 800x480}   {build.target hamclock-800x480  }
variant x1600x960       description {X11 window 1600x960}  {build.target hamclock-1600x960 }
variant x2400x1440      description {X11 window 2400x1440} {build.target hamclock-2400x1440}
variant x3200x1920      description {X11 window 3200x1920} {build.target hamclock-3200x1920}

variant web800x480      description {Web 800x480}      {build.target hamclock-web-800x480  }
variant web1600x960     description {Web 1600x960}     {build.target hamclock-web-1600x960 }
variant web2400x1440    description {Web 2400x1440}    {build.target hamclock-web-2400x1440}
variant web3200x1920    description {Web 3200x1920}    {build.target hamclock-web-3200x1920}

# Add xorg-libX11 dependency for x11 variants.
# Check the first character after "hamclock-" in the build target.
# A digit "0-9" indicates an X11 variant.  "w" indicates a web variant.

if {[string is digit [string index ${build.target} 9]]} {
    depends_lib-append  port:xorg-libX11
}

if {${os.platform} eq "darwin" && ${os.major} < 16} {
    build.env-append    LIBS=-lMacportsLegacySupport
}

destroot {
    copy ${worksrcpath}/${build.target} ${destroot}${prefix}/bin
    # Make a symlink, so that it can be invoked with ${name}
    ln -s ${build.target} ${destroot}${prefix}/bin/${name}
    xinstall -d ${destroot}${prefix}/share/applications
    xinstall -d ${destroot}${prefix}/share/icons/hicolor/48x48/apps
    copy ${worksrcpath}/${name}.desktop ${destroot}${prefix}/share/applications
    copy ${worksrcpath}/${name}.png ${destroot}${prefix}/share/icons/hicolor/48x48/apps
}

# Only run this for applications, not icons, to avoid GTK dependency:
post-activate {
    system "${prefix}/bin/update-desktop-database ${prefix}/share/applications"
}

livecheck.regex     version \(\[0-9.\]+\).

notes-append {
To run web versions:  Launch in background with "hamclock &".\
Then point your browser to "http://localhost:8081/live.html".\
Alternate backend example: "hamclock -b clearskyinstitute.com:80"
}
