# -*- 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
# $Id: Portfile 119604 2014-05-02 09:12:57Z ryandesign@macports.org $

PortSystem          1.0
PortGroup           compilers 1.0

name                gildas
version             201404c
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]
categories          science
platforms           darwin
maintainers         gmail.com:sebastien.maret
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            http://www.iram.fr/IRAMFR/GILDAS/gildas.html
master_sites        http://www.iram.fr/~gildas/dist/
distname            ${name}-src-${my_version}

checksums           rmd160  8ebf90758e521e22c6ee69669832dc726c9fc78e \
                    sha256  e229d8645d78271419dafd7949afde3546665520f1835f1204ca7ee8155525aa

patch.pre_args      -p1
patchfiles          patch-admin-Makefile.diff \
                    patch-admin-Makefile.def.diff \
                    patch-admin-Makefile.stdcxxlib.diff \
                    patch-admin-Makefile.python.diff \
                    patch-admin-gildas-env.sh.diff \
                    patch-admin-define-system.sh.diff \
                    patch-lapack.diff \
                    patch-fortran.diff

depends_lib         port:gtk2 \
                    port:libpng \
                    port:python27 \
                    port:py27-numpy

depends_build       port:cfitsio \
                    port:pkgconfig \
                    port:slatec

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

compilers.choose    fc
compilers.setup     -g95 -gcc44 -gcc45 -gcc46 require_fortran
configure.python    ${prefix}/bin/python2.7

if {![fortran_variant_isset]} {
    default_variants-append +gcc48
}

set cxxstdlib {}
platform darwin {
    if {[info exists configure.cxx_stdlib] &&
        ${configure.cxx_stdlib} ne {} &&
        [string match *clang* ${configure.cxx}]} {
        set cxxstdlib ${configure.cxx_stdlib}
    } elseif {[string match *clang* ${configure.cxx}] &&
              ${os.major} >= 13} {
        set cxxstdlib libc++
    } else {
        set cxxstdlib libstdc++
    }

    if {[string length ${cxxstdlib}]} {
        set cxxstdlib [string map {lib -l} ${cxxstdlib}]
    }
}

post-extract {
    if {![gcc_variant_isset]} {
        reinplace -W ${worksrcpath}/admin "s|-Wrealloc-lhs-all||" define-system.sh
    }
}

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

build {
    system -W ${worksrcpath} "source admin/gildas-env.sh -c ${configure.fc} -s ${prefix}/include:${prefix}/lib:/System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Versions/Current/ && export GAG_SLDFLAGS='-shared -o ${prefix}/lib/gildas/x86_64-darwin-gfortran/lib/\$(notdir \$@) -install_name ${prefix}/lib/gildas/x86_64-darwin-gfortran/lib/\$(notdir \$@)' && export DYLD_LIBRARY_PATH=${worksrcpath}/integ/x86_64-darwin-gfortran/lib && make 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}/clic.in ${destroot}${prefix}/bin/clic
    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}/sic.in ${destroot}${prefix}/bin/sic

    reinplace -W ${destroot}${prefix}/bin s|@PREFIX@|${prefix}|g astro class clic greg mapping mira sic
}
