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

name                gildas
version             202601a
set my_version      [string tolower [clock format [clock scan 2000-[string range ${version} 4 5]-10] -format %b]][string range ${version} 2 3][string range ${version} 6 end]

supported_archs     arm64 x86_64
categories          science
maintainers         {iram.fr:bardeau @bardeau}
license             permissive

description         Radioastronomy data analysis software

long_description    GILDAS is a collection of state-of-the-art softwares \
                    oriented toward (sub-)millimeter radioastronomical \
                    applications (either single-dish or interferometer). \
                    It is daily used to reduce all data acquired with the \
                    IRAM 30M telescope and Plateau de Bure Interferometer \
                    PDBI (except VLBI observations). GILDAS is easily \
                    extensible. GILDAS is written in Fortran-90, with a \
                    few parts in C/C++ (mainly keyboard interaction, \
                    plotting, widgets).

homepage            https://www.iram.fr/IRAMFR/GILDAS/gildas.html
master_sites        https://www.iram.fr/~gildas/dist/ \
                    https://www.iram.fr/~gildas/dist/archive/gildas/
distname            ${name}-src-${my_version}
use_xz              yes

checksums           rmd160  87ffbb37bd3d62bb3922a0b4ee687865eb7b7115 \
                    sha256  2765f1d3106872d814900ea8d09261203ffb651894a612ef7772d57561a26bba \
                    size    27650616

patch.pre_args-replace  -p0 -p1
patchfiles          patch-admin-Makefile.def.diff \
                    patch-admin-gildas-env.sh.diff \
                    patch-admin-define-system.sh.diff

depends_lib         path:lib/pkgconfig/gtk+-2.0.pc:gtk2 \
                    port:libpng \
                    port:python313 \
                    port:py313-setuptools \
                    port:py313-numpy

# coreutils needed to use GNU gsort instead of native BSD sort as older version
# of sort do not support sort -V:
depends_build       port:cfitsio \
                    port:pkgconfig \
                    port:groff \
                    path:libexec/coreutils/libstdbuf.so:coreutils

set my_build_opts   ""
variant openmp description {Add OpenMP support} {set my_build_opts "-o openmp"}

# need x11 and cannot be used with quartz; see ticket #42886
require_active_variants gtk2 x11 quartz

# need the fortran interface; see ticket #50543
compilers.enforce_some_fortran cfitsio

compilers.add_gcc_rpath_support no

compilers.choose    fc
compilers.setup     -gcc46 require_fortran
configure.python    ${prefix}/bin/python3.13

configure {
    set cxxstdlib {}
    if {[string match *clang* ${configure.cxx}]} {
        set cxxstdlib [string map {lib -l} ${configure.cxx_stdlib}]
    }

    reinplace -W ${worksrcpath}/admin "s|@PREFIX@|${prefix}|g" define-system.sh gildas-env.sh
    reinplace -W ${worksrcpath}/admin "s|@CC@|${configure.cc}|g" define-system.sh
    reinplace -W ${worksrcpath}/admin "s|@CCFLAGS@|${configure.cflags}|g" Makefile.def
    reinplace -W ${worksrcpath}/admin "s|@FC@|${configure.fc}|g" define-system.sh
    reinplace -W ${worksrcpath}/admin "s|@FCFLAGS@|${configure.fcflags}|g" Makefile.def
    reinplace -W ${worksrcpath}/admin "s|@CXX@|${configure.cxx}|g" define-system.sh
    reinplace -W ${worksrcpath}/admin "s|@CPP@|${configure.cpp}|g" Makefile.def define-system.sh
    reinplace -W ${worksrcpath}/admin "s|@CPPFLAGS@|${configure.cppflags}|g" Makefile.def
    reinplace -W ${worksrcpath}/admin "s|@LDFLAGS@|${configure.ldflags}|g" Makefile.def
    reinplace -W ${worksrcpath}/admin "s|@PYTHON@|${configure.python}|g" define-system.sh
}

build {
    set my_machine   [if {[string match *arm* ${os.arch}]} {list arm64} {list x86_64}]
    set my_env_vers  [string tolower [exec sw_vers -productName | tr -d " "]]
    system -W ${worksrcpath} \
           "source admin/gildas-env.sh -c ${configure.fc} ${my_build_opts} -s ${prefix}/include:${prefix}/lib:/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Versions/Current/ && \
            export GAG_SLDFLAGS='-shared -o ${prefix}/lib/gildas/${my_machine}-${my_env_vers}-gfortran/lib/\$(notdir \$@) -install_name ${prefix}/lib/gildas/${my_machine}-${my_env_vers}-gfortran/lib/\$(notdir \$@)' && \
            export DYLD_LIBRARY_PATH=${worksrcpath}/integ/${my_machine}-${my_env_vers}-gfortran/lib && \
            export GAG_ADDONS=yes && \
            make -w install"
}

destroot {
    copy ${workpath}/gildas-exe-${my_version} ${destroot}${prefix}/lib/gildas

    xinstall ${filespath}/astro.in ${destroot}${prefix}/bin/astro
    xinstall ${filespath}/class.in ${destroot}${prefix}/bin/class
    xinstall ${filespath}/cube.in ${destroot}${prefix}/bin/cube
    xinstall ${filespath}/greg.in ${destroot}${prefix}/bin/greg
    xinstall ${filespath}/mapping.in ${destroot}${prefix}/bin/mapping
    xinstall ${filespath}/mira.in ${destroot}${prefix}/bin/mira
    xinstall ${filespath}/mrtcal.in ${destroot}${prefix}/bin/mrtcal
    xinstall ${filespath}/sic.in ${destroot}${prefix}/bin/sic
    xinstall ${filespath}/sched-30m.in ${destroot}${prefix}/bin/sched-30m

    set my_machine   [if {[string match *arm* ${os.arch}]} {list arm64} {list x86_64}]
    set my_env_vers  [string tolower [exec sw_vers -productName | tr -d " "]]

    reinplace -W ${destroot}${prefix}/bin s|@PREFIX@|${prefix}|g astro class cube greg mapping mira mrtcal sic sched-30m
    reinplace -W ${destroot}${prefix}/bin s|@ARCH@|${my_machine}|g astro class cube greg mapping mira mrtcal sic sched-30m
    reinplace -W ${destroot}${prefix}/bin s|@OSNAME@|${my_env_vers}|g astro class cube greg mapping mira mrtcal sic sched-30m

}
