# -*- 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                xtide
version             2.15.5
maintainers         {dstrubbe @dstrubbe} openmaintainer

description         Tide prediction software, with a large database of locations.
long_description    XTide is a package that provides tide and current predictions \
                    in a wide variety of formats. Graphs, text listings, and calendars \
                    can be generated, or a tide clock can be provided on your desktop. \
                    For x11, plain command line, and as an http server.

use_xz              yes
homepage            https://www.flaterco.com/xtide/
master_sites        https://flaterco.com/files/xtide/

set docdir          ${prefix}/share/doc/${subport}
set harmonics_dir   ${prefix}/share/${name}/harmonics
set wvs_dir         ${prefix}/share/${name}/wvs
set conf_dir        ${prefix}/etc/${name}
set conf_file       ${conf_dir}/xtide.conf

livecheck.type      regex
livecheck.url       ${homepage}files.html

if {${name} eq ${subport}} {
    categories          science x11
    revision            0
    license             GPL-3+

    checksums           rmd160  511be213fbed76a78f993c4bc60dbdd6dd280604 \
                        sha256  b9460dcf167e8d9544dcb34a751516d14a97fc3f4aebc6a8bb5ca5f2710d7164 \
                        size    574972

    depends_lib         port:libpng \
                        port:libtcd
    depends_run         port:${name}-data

    post-extract {
        # Put config file path within MacPorts filesystem hierarchy, and reflect this in documentation.
        reinplace -locale C -W ${worksrcpath} "s|/etc/xtide\\.conf|${conf_dir}/xtide\\.conf|g" \
            libxtide/Global.cc tide.1 xtide.1 xttpd.8
    }

    # Per the configure script: "if using GCC, version 4.4 or later is required"
    compiler.blacklist  *gcc-4.2 gcc-4.0 gcc-3.3 {clang < 300}

    configure.args-append   --without-x
    
    variant xwindows description {Build xtide executable for use with X-Windows} {
        depends_lib-append      port:xorg-libXaw
        configure.args-delete   --without-x
    }

    default_variants-append +xwindows

    # Set up config file to where xtide-data and xtide-wvs will put files.
    post-destroot {
        xinstall -d -m 755 ${destroot}${conf_dir}
        system "echo '${harmonics_dir}\n${wvs_dir}' >> ${destroot}${conf_file}"
    }

    notes "You may optionally install the port xtide-wvs to enable plotting of the shorelines on the globe view."

    livecheck.regex     ${name}-(\[0-9.\]+)${extract.suffix}
}

subport ${name}-wvs {
    categories          science
    version             0
    revision            0
    license             public-domain
    supported_archs     noarch
    platforms           any

    description         World Vector Shoreline data for XTide.

    long_description    {*}${description}

    worksrcdir
    distname            wvs

    checksums           rmd160  ca4512c44fc0a7d13b83f2f83fb97526ea321372 \
                        sha256  4b505d80099cf9c43405f687b55aa2c0f9a4412f3172c8c9897035e51834bc67 \
                        size    32521228

    use_configure       no

    build {}

    destroot {
        xinstall -d -m 755 ${destroot}${wvs_dir}
        foreach wvsfile {wvs1.dat wvs12.dat wvs250k.dat wvs3.dat wvs43.dat wvsfull.dat} {
            xinstall -m 644 -W ${workpath} ${wvsfile} ${destroot}${wvs_dir}
        }
    }

    livecheck.type      none
}

subport ${name}-data {
    categories          science
    version             20240104
    revision            0
    license             public-domain
    supported_archs     noarch
    platforms           any

    description         Harmonics data for XTide.

    long_description    {*}${description}

    worksrcdir          harmonics-dwf-${version}
    distname            ${worksrcdir}-free

    checksums           rmd160  af2aed1336ff375fa6be466e3b53e13859172ce5 \
                        sha256  2a50e29051f72b5df51a763b4c61b0fe7df5209208b18a76443d02e990f6da85 \
                        size    863340

    use_configure       no

    build {}

    destroot {
        xinstall -d -m 755 ${destroot}${harmonics_dir} ${destroot}${docdir}
        xinstall -m 644 -W ${worksrcpath} ${distname}.tcd ${destroot}${harmonics_dir}
        xinstall -m 644 -W ${worksrcpath} COPYING ChangeLog ${destroot}${docdir}
    }

    livecheck.regex     harmonics-dwf-(\[0-9\]+)-free${extract.suffix}

    notes "Only public-domain harmonics for the United States are included in this port.\
Additional harmonics for other countries, licensed for non-commercial use only, can be downloaded from\
[lindex ${master_sites} 0]harmonics-dwf-20111230-nonfree${extract.suffix}\
and extracted into ${harmonics_dir} to make them available to ${name}. See http://www.flaterco.com/xtide/faq.html#60."
}
