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

name                gildas
version             202507b
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]
revision            0

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  169c3ce7c177475f0a02268d638eeeae35324266 \
                    sha256  4d4f5476fcb394858625600024820604ee4d817b8a0d84dd8c78451d1c136a05 \
                    size    48278752

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

set py_ver          3.13
set py_ver_nodot    [string map {. {}} ${py_ver}]

depends_lib         path:lib/pkgconfig/gtk+-2.0.pc:gtk2 \
                    port:libpng \
                    port:python${py_ver_nodot} \
                    port:py${py_ver_nodot}-numpy \
                    port:py${py_ver_nodot}-setuptools

# 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 \
                    path:libexec/coreutils/libstdbuf.so:coreutils \
                    port:groff \
                    path:bin/pkg-config:pkgconfig

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

compiler.blacklist-append \
                    *gcc-4.*
compilers.choose    fc
compilers.setup     -gcc46 require_fortran
configure.python    ${prefix}/bin/python${py_ver}

# FIXME: the code has a lot of bugs and currently does not build.
# https://trac.macports.org/ticket/69630
platform powerpc {
    # This is not portable at the moment!
    patchfiles-append \
                    patch-powerpc-defs.diff
    # C-all.f:4: error: detected recursion whilst expanding macro "vector"
    # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63413
    configure.cppflags-append \
                    -Uvector -Upixel
}

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 {
    if {${configure.build_arch} in [list ppc ppc64]} {
        set my_machine  powerpc
    } else {
        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

    if {${configure.build_arch} in [list ppc ppc64]} {
        set my_machine  powerpc
    } else {
        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
}
