# -*- 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 119146 2014-04-18 05:44:31Z takeshi@macports.org $

PortSystem                  1.0
PortGroup                   conflicts_build 1.0

name                        ncarg
version                     6.2.0
categories                  science
maintainers                 takeshi
license                     {UCAR-Unidata public-domain BSD LGPL-3 GPL-3}
description                 language for scientific data processing and visualization
long_description \
    The NCAR Command Language (NCL), a product of           \
    the Computational & Information Systems Laboratory      \
    at the National Center for Atmospheric Research (NCAR)  \
    and sponsored by the National Science Foundation,       \
    is a free interpreted language designed specifically    \
    for scientific data processing and visualization.

homepage                    http://www.ncl.ucar.edu/
platforms                   darwin
master_sites                http://www.earthsystemgrid.org/:ncl \
                            http://www.ncl.ucar.edu/Download/files/:g2clib

distname                    ncl_${name}-${version}
set ncl_src             ${distname}${extract.suffix}
set g2clib_version      1.5.0
set g2clib_src          g2clib-${g2clib_version}-patch${extract.suffix}
set g2clib_worksrcdir   g2clib-${g2clib_version}-patch
distfiles           ${ncl_src}:ncl \
                    ${g2clib_src}:g2clib

checksums   ${ncl_src} \
                    md5     10e17772120a17f3ff30ffd79ea43a7a \
                    sha1    9f7be65e0406a410b27d338309315deac2e64b6c \
                    rmd160  4366489193cb94e5ae6811ed4cd6d45c87bdd7d4 \
            ${g2clib_src} \
                    md5     f857f49a66918316996eb52399e04120 \
                    sha1    3113b88e0295dbc64428edd87c0b583e774fb320 \
                    rmd160  221b308be9258ed3ec9ebe126d872aa451f879cf

depends_lib                 path:lib/pkgconfig/cairo.pc:cairo \
                            port:proj       \
                            port:gdal       \
                            port:udunits2   \
                            port:vis5d      \
                            port:hdfeos     \
                            port:hdfeos5    \
                            port:wgrib2     \
                            port:curl       \
                            port:libpng     \
                            port:libxml2    \
                            port:xorg-libXaw
depends_build               port:triangle \
                            port:flex

use_parallel_build          no
universal_variant           no

pre-fetch {
    if {![file exists ${distpath}/${distname}${extract.suffix}]} {
        xinstall -d ${distpath}
        ui_error "Download ${distname}${extract.suffix} manually and place it in ${distpath}.\n \
            See instructions on ${homepage}/Download/index.shtml"
    }
}

set usedap y

# clang raises error on (assumed) non-void functions without return value.
patchfiles  patch-ni-nyacc-error.c.diff \
            patch-ni-nyacc-main.c.diff \
            patch-ni-nyacc-output.c.diff \
            patch-ni-nyacc-reader.c.diff \
            patch-ni-nyacc-verbose.c.diff \
            patch-ncarview-idt-w_idt.c.diff \
            patch-Fsplit.c.diff
if {$build_arch eq "x86_64"} {
    patchfiles-append           patch-config-ymake.diff
}

post-patch {
    reinplace "s|hdf/||" ${worksrcpath}/ni/src/ncl/FileSupport.c
    reinplace "s| -lsz||g" ${worksrcpath}/Configure
    reinplace "s| -lsz||g" ${worksrcpath}/config/Project
    reinplace "s|g\\\\+\\\\+|${configure.cc}|" ${worksrcpath}/ni/src/ncl/yMakefile
    reinplace "s|INC=-I/usr/local/include|INC=${configure.cppflags} ${configure.cflags}|" ${workpath}/${g2clib_worksrcdir}/makefile
    reinplace "s|CC=gcc -fPIC|CC=${configure.cc}|" ${workpath}/${g2clib_worksrcdir}/makefile
    if {$build_arch eq "x86_64" || $build_arch eq "ppc64"} {
        reinplace "s|-DUSE_PNG$|-DUSE_PNG -D__64BIT__|" ${workpath}/${g2clib_worksrcdir}/makefile
    }
    foreach f {Darwin Darwin_Intel} {
        if {[variant_isset g95]} {
            reinplace "s|-lgfortran|-lf95|" ${worksrcpath}/config/${f}
        } else {
            reinplace "s|-lgfortran|-lgfortran -lgomp|" ${worksrcpath}/config/${f}
        }
        reinplace "s| gfortran$| ${configure.fc}|" ${worksrcpath}/config/${f} 
        reinplace "s|gcc$|${configure.cc}|" ${worksrcpath}/config/${f}
        reinplace "/CcOptions/d" ${worksrcpath}/config/${f}
        reinplace "/ArchRecLibSearch/d" ${worksrcpath}/config/${f}
        reinplace "/ArchRecIncSearch/d" ${worksrcpath}/config/${f}
        reinplace "s|-lXext$|-lXext\\
#define ArchRecLibSearch    -L${prefix}/lib\\
#define ArchRecIncSearch    -I${prefix}/include\\
#define CcOptions -fPIC -ansi -D_DARWIN_C_SOURCE \\
#define COptimizeFlag ${configure.cflags}\\
#define FOptimizeFlag ${configure.fflags}\\
#define ManRoot ${destroot}${prefix}/share/man\\
#define NetCDFlib -lnetcdf\\
#define HDFEOS5lib -lhe5_hdfeos -lGctp -lhdf5|" ${worksrcpath}/config/${f}
# A workaround for XQuartz 2.7.3 on Mountain Lion known as ncl.xq.fix
        if {${os.version}>=12} {
            reinplace "s|-D__UNIXOS2__|-D__UNIXOS2__ -D__JIRA1530__|" ${worksrcpath}/config/${f}
        }
    }
    if {[variant_isset g95]} {
        reinplace "s|-fno-range-check||" ${worksrcpath}/config/Darwin_Intel
    }
    foreach f {ni/src/ncl/NclHDF.c \
           ni/src/ncl/NclHDFEOS.c \
           ni/src/ncl/NclHDFEOS5.c \
           ncarview/src/lib/libncarg_ras/hdf.c} {
        reinplace "s|hdf\/||" ${worksrcpath}/${f}
    }
    reinplace "s|\$\(NCARGGKSCAIROLIB\) \$\(NCARGCLIB\)|\$(NCARGGKSCAIROLIB) \$(NCARGGKSLIB) \$(NCARGCLIB)|" ${worksrcpath}/config/Project
    foreach exlib {sphere3.1_dp fftpack5_dp} {
        reinplace "s|-l${exlib}|\$(EXTERNALSRC)/${exlib}/lib${exlib}.a|g" ${worksrcpath}/config/Project
    }
    if {[variant_isset accelerate]} {
        reinplace "s|-llapack_ncl|-framework Accelerate|g" ${worksrcpath}/config/Project
        if {[variant_isset g95] || ([string match *64* $build_arch] && ${os.major} >= 10)} {
            reinplace "s|-lblas_ncl|-ldotwrp -framework Accelerate|g" ${worksrcpath}/config/Project
        } else {
            reinplace "s|-lblas_ncl|-framework Accelerate|g" ${worksrcpath}/config/Project
        }
        foreach f {Darwin Darwin_Intel} {
            reinplace "/LAPACKLIB/d" ${worksrcpath}/config/${f}
            reinplace "/BLASLIB/d" ${worksrcpath}/config/${f}
        }
        reinplace "s|-L\$(LAPACKSRC)||" ${worksrcpath}/config/Project
        reinplace "s|-L\$(BLASSRC)||" ${worksrcpath}/config/Project
        reinplace "s|blas lapack ||" ${worksrcpath}/external/yMakefile
    } else {
        foreach exlib {lapack blas} {
            reinplace "s|-l${exlib}_ncl|\$(EXTERNALSRC)/${exlib}/lib${exlib}_ncl.a|g" ${worksrcpath}/config/Project
        }
    }
}

pre-configure {
    system -W ${workpath}/${g2clib_worksrcdir} "make all"
    file copy ${workpath}/${g2clib_worksrcdir}/libgrib2c.a ${worksrcpath}
    system -W ${worksrcpath}/config \
        "CC=${configure.cc} \
        CFLAGS=${configure.cflags} \
        make -f Makefile.ini; \
        ./ymake -config `pwd`"
    foreach ext "c h" {
      file copy ${prefix}/share/triangle/triangle.${ext} ${worksrcpath}/ni/src/lib/hlu
    }
}
configure {
    system -W ${worksrcpath} "./Configure -v << EOF


y
${destroot}${prefix}

y
y
y
y
y
y
y
y
y
y
y
y
y
y
${worksrcpath} ${prefix}/lib ${prefix}/lib/hdfeos5/lib ${prefix}/lib
${prefix}/include/freetype2 ${prefix}/include/udunits2 ${prefix}/lib/hdfeos5/include ${prefix}/include


EOF"
}
post-configure {
#    reinplace "s| -lsz||g" ${worksrcpath}/makefile
#    reinplace "s| -lsz||g" ${worksrcpath}/config/Site.local
    system -W ${worksrcpath} "make Info"
    system "cat ${worksrcpath}/config/Site.local"
}

build.target                Build
if {${os.major}<10} {
    if {[variant_isset g95]} {
        build.args-append           EXTRA_CCOPTIONS=\"${configure.cc_archflags}\"
    } else {
        if {[string match *64* $build_arch]} {
            build.args-append       EXTRA_CCOPTIONS=-m64
        } else {
            build.args-append       EXTRA_CCOPTIONS=-m32
        }
    }
}

# The build script ignores the build errors.
# Check if ncl is built here.
post-build {
    if {![file exists ${worksrcpath}/ni/src/ncl/ncl]} {
        return -code error "ncl did not get built"
    }
}

set gcc_versions {4.3 4.4 4.5 4.6 4.7 4.8 4.9}
set default_fortran_variant +gcc48
set g95_conflicts {}

foreach ver ${gcc_versions} {
    set ver_no_dot [join [split ${ver} "."] ""]

    set variant_line {variant gcc${ver_no_dot} description "build with gfortran from gcc${ver_no_dot}" conflicts g95}

    foreach over ${gcc_versions} {
        if {${ver} == ${over}} {
            continue
        }

        set over_no_dot [join [split ${over} "."] ""]
        append variant_line " conflicts gcc${over_no_dot}"
    }
    append variant_line { {}}

    eval $variant_line

    append g95_conflicts " conflicts gcc${ver_no_dot}"

    if {[variant_isset gcc${ver_no_dot}]} {
        if {${default_fortran_variant} != "+gcc${ver_no_dot}"} {
            set default_fortran_variant ""
        }
    }
}

eval [concat {variant g95 description {build with g95}} $g95_conflicts {{}}]

if {[variant_isset g95]} {
    if {${default_fortran_variant} != "+g95"} {
        set default_fortran_variant ""
    }
}

if {${default_fortran_variant} != ""} {
    default_variants-append "${default_fortran_variant}"
}

foreach ver ${gcc_versions} {
    set ver_no_dot [join [split ${ver} "."] ""]

    if {[variant_isset gcc${ver_no_dot}]} {
        depends_lib-append path:lib/libgcc/libgcc_s.1.dylib:libgcc
        depends_build-append port:gcc${ver_no_dot}

        configure.compiler macports-gcc-${ver}
    }
}

if {[variant_isset g95]} {
    depends_lib-append path:lib/libgcc/libgcc_s.1.dylib:libgcc
    depends_build-append port:g95

    configure.fc ${prefix}/bin/g95
    configure.f77 ${prefix}/bin/g95
    configure.f90 ${prefix}/bin/g95

    configure.fflags-append -fno-second-underscore
}

## ncarg fails to build some binaries with xnu-headers installed 
## See #38687
# seems it works with xnu-headers installed
#if {[variant_isset g95]} {
#    conflicts_build-append xnu-headers
#}

variant accelerate description {use Accelerate.framework} {
# use of -ff2c implies -fsecond-underscore and 
# breaks the linkage between nfp (C) and nfpfort (Fortran).
# Moreover dotwrp should not require -ff2c
#    if {![variant_isset g95]} {
#        configure.fflags-append -ff2c
#    }
    if {[variant_isset g95] || ([string match *64* $build_arch] && ${os.major} >= 10)} {
        depends_lib-append      port:dotwrp
    }
}

livecheck.type              regex
livecheck.url               ${homepage}
livecheck.regex             {Current Version:.* .*([0-9]+\.[0-9]+\.[0-9]+)}
