# -*- 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                cspice
version             N0067
categories          science
maintainers         nomaintainer

# https://naif.jpl.nasa.gov/naif/rules.html
# this looks to be the appropriate license however IANAL
license             public-domain

universal_variant   no
supported_archs     x86_64 arm64

description         SPICE: An Observation Geometry System for Space Science Missions

long_description    NASA's Navigation and Ancillary Information Facility \
                    (NAIF) offers the "SPICE" (Spacecraft, Planet, Instrument, \
                    C-matrix, Events) observation geometry information system \
                    to assist in planning and interpreting scientific \
                    observations from space-based instruments aboard robotic \
                    planetary spacecraft, in support of engineering tasks \
                    associated with these missions, and for some heliophysics \
                    and earth science missions.\n\nThe SPICE Toolkit is \
                    offered in several languages. This port contains the \
                    toolkit for C.

homepage            https://naif.jpl.nasa.gov/naif/
distname            cspice
extract.suffix      .tar.Z

# Different packages are published for each supported OS-CPU combination,
# largely because they contain pre-built binaries. Those binaries will be
# discared by this port, and everything will be rebuilt from source. The
# architecture-specific packages do contain a few insignificant differences in
# their build files and source code. Honor the distinct packages, in case any of
# the source code differences wind up becoming more significant in the future.
if {${configure.build_arch} eq "x86_64"} {
        master_sites \
                    https://naif.jpl.nasa.gov/pub/naif/toolkit/C/MacIntel_OSX_AppleC_64bit/packages/

        # The distname contains neither the architecture nor the version, so
        # a custom dist_subdir is required.
        dist_subdir ${name}/${version}_mac-x86_64

        checksums   rmd160  f633934504de64736c1d273301e4a7f8c5e18c10 \
                    sha256  6f4980445fee4d363dbce6f571819f4a248358d2c1bebca47e0743eedfe9935e \
                    size 41938449
} elseif {${configure.build_arch} eq "arm64"} {
        master_sites \
                    https://naif.jpl.nasa.gov/pub/naif/toolkit/C/MacM1_OSX_clang_64bit/packages/

        # The distname contains neither the architecture nor the version, so
        # a custom dist_subdir is required.
        dist_subdir ${name}/${version}_mac-arm64

        checksums   rmd160  d2c914745e2c1ffb3b4ab3f42d2376b820b52524 \
                    sha256  0deae048443e11ca4d093cac651d9785d4f2594631a183d85a3d58949f4d0aa9 \
                    size 42047289
}

patchfiles-append   patch-dylib.diff \
                    patch-fix_warnings.diff

post-extract {
        # Throw away the pre-built binaries that the package came with.
        foreach file [concat [glob ${worksrcpath}/exe/*] [glob ${worksrcpath}/lib/*]] {
                delete ${file}
        }

        xinstall -m 0644 -W "${filespath}" example.c naif0012.tls "${worksrcpath}/doc/"
        reinplace "s%@PREFIX@%${prefix}%g" ${worksrcpath}/doc/example.c
}

use_configure       no

set canonicalversion [regsub {^N0*(\d+)$} ${version} {\1.0.0}]

build.env-append    TKCOMPILER=${configure.cc}\ [get_canonical_archflags cc] \
                    TKCOMPILEOPTIONS=-c\ -ansi\ ${configure.cflags}\ -Wno-shift-op-parentheses\ -Wno-logical-op-parentheses\ -Wno-parentheses \
                    TKLINKOPTIONS=${configure.ldflags} \
                    DYLIB_INSTALL_PREFIX=${prefix}/lib \
                    DYLIB_VERISON=${canonicalversion}

build.cmd           csh
build.target
build.args          makeall.csh

destroot {
    if {[variant_isset doc]} {
        copy ${worksrcpath}/doc/ ${destroot}${prefix}/share/doc/cspice/
    }

    copy ${worksrcpath}/include/ ${destroot}${prefix}/include/cspice/

    foreach file [list libcspice.${canonicalversion}.dylib libcspice.dylib] {
        copy ${worksrcpath}/lib/${file} ${destroot}${prefix}/lib/${file}
    }

    set binary_prefix ""
    if {[variant_isset prefix_binaries]} {
        set binary_prefix "CS"
    }
    foreach file {brief chronos ckbrief commnt dskbrief dskexp frmdiff inspekt \
                  mkdsk mkspk msopck simple spacit spkdiff spkmerge states \
                  subpt tictoc tobin toxfr version} {
        copy ${worksrcpath}/exe/${file} ${destroot}${prefix}/bin/${binary_prefix}${file}
    }
}

default_variants +doc +prefix_binaries

variant doc description {Install documentation} {}
variant prefix_binaries description {Prepend "CS" to installed binaries' names} {}

livecheck.type      regex
livecheck.url       ${homepage}toolkit.html
livecheck.regex     "The current SPICE Toolkit version is <b>(N.*)</b>,"
