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

name                    cdf
epoch                   1
version                 3.8.1
revision                0
checksums               rmd160  5a418f182c856cacc219a3307dbe7f6748e6c28f \
                        sha256  6d480ebfbf778c4623021a3a79a5ead2e99753c852940de970bca3790116db61 \
                        size    2486859

set my_branch           [join [lrange [split ${version} .] 0 1] ""]
set my_version          ${my_branch}_[join [lrange [split ${version} .] 2 end] "_"]
set my_short_version    ${my_branch}_[lindex [split ${version} .] 2]
maintainers             {ryandesign @ryandesign} openmaintainer
categories              science
license                 Noncommercial

description             CDF: Common Data Format

long_description        The Common Data Format (CDF) is a self-describing \
                        data format for the storage and manipulation of \
                        scalar and multidimensional data in a platform and \
                        discipline independent fashion.

homepage                https://cdf.gsfc.nasa.gov
master_sites            https://spdf.gsfc.nasa.gov/pub/software/cdf/dist/${name}${my_short_version}/unix/

dist_subdir             ${name}/${version}
distname                ${name}${my_short_version}-dist
extract.suffix          -all${extract.suffix}

depends_lib             port:ncurses

worksrcdir              ${name}${my_short_version}-dist

post-patch {
    reinplace "s|\\\$(INSTALLDIR)/bin/definitions\\.\\(\[BCK\]\\)|${prefix}/bin/definitions.\\1|" ${worksrcpath}/Makefile
}

compilers.choose        cc fc
compilers.setup
compilers.allow_arguments_mismatch \
                        yes

pre-build {
    set cdf_os          macosx
    set cdf_env         gnu

    build.args-append   ENV=${cdf_env} \
                        OS=${cdf_os} \
                        COPTIONSld_${cdf_os}_${cdf_env}= \
                        CURSESLIB_${cdf_os}_${cdf_env}=${prefix}/lib/libncurses.dylib \
                        LDOPTIONS_${cdf_os}_${cdf_env}="-dynamiclib -install_name ${prefix}/lib/libcdf.dylib" \
                        MACOSxFopt= \
                        MACOSxlink="\$LDFLAGS -lc -lm" \
                        MACOSxmin=

    if {${os.endian} eq "little"} {
        set endian_flag i386
    } else {
        set endian_flag __ppc__
    }

    # When upgrading, verify COPTIONS still approximately reflects what’s in the Makefile.
    # Using ${configure.cc_archflags} in LD because ${configure.ld_archflags} doesn’t use -m32 / -m64 flags when they’re needed.
    build.args-append   COPTIONS_${cdf_os}_${cdf_env}="-D${endian_flag} -D__APPLE__ -D__MACH__ -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE \$CFLAGS" \
                        LD_${cdf_os}_${cdf_env}="\$CC"

    if {![fortran_variant_isset]} {
        build.args-append \
                        FORTRAN=no
    } else {
        build.args-append \
                        FORTRAN=yes \
                        FOPTIONS_${cdf_os}_${cdf_env}="\$FCFLAGS" \
    }
}

use_parallel_build      no

test.run                yes
test.target             test

destroot.destdir        INSTALLDIR=${destroot}${prefix}

post-destroot {
    reinplace "s|${destroot}||g" {*}[glob ${destroot}${prefix}/bin/definitions.*]

    set docdir ${destroot}${prefix}/share/doc/${name}
    xinstall -d ${docdir}
    xinstall -W ${worksrcpath} CDF_copyright.txt CHANGES.txt \
        README_cdf_tools.txt Release.notes Welcome.txt ${docdir}
    move ${destroot}${prefix}/CDFLeapSeconds.txt ${docdir}
}

livecheck.type          regex
livecheck.regex         {V(\d+(?:\.\d+)+)}
