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

name                    ncarg
version                 6.6.2
revision                38
epoch                   1
set version_no_dot [join [split ${version} "."] ""]
categories              science
maintainers             {takeshi @tenomoto} openmaintainer
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            https://www.ncl.ucar.edu

distname            ncl-${version}
master_sites        ${homepage}/Download/files/:g2clib \
                    https://github.com/NCAR/ncl/archive/:ncl

set ncl_src             ${version}${extract.suffix}

set g2clib_version      1.6.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} \
                    rmd160  061d4230adfc9e601393a396aff06704ffa68c3c \
                    sha256  cad4ee47fbb744269146e64298f9efa206bc03e7b86671e9729d8986bb4bc30e \
                    size    60523044 \
                    ${g2clib_src} \
                    rmd160  112d90fd297f772643d6223041f741d6e4101b49 \
                    sha256  ec79fb768ce9e45ca6c2c94fa4dfc99b30600d7f8f44d272cdb36ab542d59468 \
                    size    184817

compilers.setup     require_fortran
compilers.allow_arguments_mismatch \
                    yes

depends_build               port:triangle
depends_lib                 path:lib/pkgconfig/cairo.pc:cairo \
                            port:curl \
                            port:flex \
                            port:gdal \
                            port:gsl \
                            port:hdf4 \
                            port:hdf5 \
                            port:hdfeos \
                            port:hdfeos5 \
                            port:jasper \
                            port:libpng \
                            port:libxml2 \
                            port:netcdf \
                            port:proj5 \
                            port:udunits2 \
                            port:vis5d \
                            port:wgrib2 \
                            port:xorg-libice \
                            port:xorg-libsm \
                            port:xorg-libX11 \
                            port:xorg-libXaw \
                            port:xorg-libXext \
                            port:xorg-libXmu \
                            port:xorg-libXt \
                            port:xpm
depends_run                 bin:ESMF_RegridWeightGen:esmf \
                            port:rangs-gshhs-ncarg
conflicts_build             ncarg

set usedap y

patchfiles                  patch-Configure.diff \
                            patch-ncarg2d-src-libncarg-Iftran-yMakefile.diff \
                            patch-ncarview-src-bin-ictrans-yMakefile.diff \
                            patch-ncarg2d-src-bin-ezmapdemo-yMakefile.diff \
                            patch-ncarg2d-src-bin-tdpackdemo-yMakefile.diff \
                            patch-ngmath-src-bin-nnalg-yMakefile.diff \
                            patch-ni-src-lib-nfpfort-yMakefile.diff \
                            patch-ni-src-lib-hlu-Format.c.diff \
                            patch-ni-src-ncl-global-vars.diff \
                            patch-ni-src-ncl-MakeNcl.diff \
                            patch-ni-src-ncl-remove-nc-set-log.diff \
                            patch-ni-src-ncl-yMakefile.diff \
                            patch-ncarg2d-src-libncarg_gks-bwi-argb2ci.f.diff \
                            patch-includes.diff \
                            patch-use-right-cxx.diff

if {${configure.build_arch} in [list arm64 x86_64]} {
    patchfiles-append       patch-config-ymake.diff
}
if {${configure.build_arch} eq "arm64"} {
    patchfiles-append       patch-common-src-libncarg_c-yMakefile.diff
}
if {[variant_isset accelerate]} {
    patchfiles-append       patch-config-Project-accelerate.diff \
                            patch-external-yMakefile.diff
} else {
    patchfiles-append       patch-config-Project.diff
}

universal_variant           no
use_parallel_build          no

compilers.enforce_c         hdf5
configure.cxx_stdlib        libstdc++

# Compiler choice must match hdf5 port, see below.
compiler.blacklist-append   *gcc-4.0 *gcc-4.2 {clang < 500}

configure.cflags-append     -Wno-error=implicit-function-declaration
configure.cflags-append     -Wno-error=implicit-int
configure.cflags-append     -Wno-error=int-conversion
configure.cflags-append     -Wno-error=incompatible-pointer-types
configure.cflags-append     -DH5_USE_110_API

if {${configure.sdkroot} ne ""} {
    configure.cflags-append -isysroot ${configure.sdkroot}
    configure.fflags-append -isysroot ${configure.sdkroot}
}

pre-configure {
    if {[file exists ${prefix}/bin/h5pcc]} {
        configure.cc        ${prefix}/bin/h5pcc
    } else {
        configure.cc        ${prefix}/bin/h5cc
    }
    # build modified g2clib
    if {${configure.build_arch} in [list arm i386 ppc]} {
        set defs "-DUSE_JPEG2000 -DUSE_PNG"
    } else {
        # Sensible default is 64-bit:
        set defs "-DUSE_JPEG2000 -DUSE_PNG -D__64BIT__"
    }
    system -W ${workpath}/${g2clib_worksrcdir} "patch -p0 < ${filespath}/patch-enc_jpeg2000.c.diff"
    system -W ${workpath}/${g2clib_worksrcdir} "patch -p0 < ${filespath}/patch-dec_jpeg2000.c.diff"
    system -W ${workpath}/${g2clib_worksrcdir} \
        "make CC=${configure.cc} INC=\"${configure.cppflags} ${configure.cflags}\" DEFS=\"${defs}\" all"
    file copy ${workpath}/${g2clib_worksrcdir}/libgrib2c.a ${worksrcpath}

    # edit configurations
    if {${configure.build_arch} eq "i386" || [regexp clang ${configure.compiler}]} {
        reinplace "s| -lquadmath||" ${worksrcpath}/config/Darwin_Intel
    }
    reinplace "s|-L/usr/X11R6/lib -L/sw/lib|-L${prefix}/lib|" ${worksrcpath}/config/Darwin
    reinplace "s|-I/usr/X11R6/include -I/sw/include|-I${prefix}/include|" ${worksrcpath}/config/Darwin
    foreach f {Darwin Darwin_Intel} {
        if {[regexp clang ${configure.compiler}]} {
            reinplace "s|-fopenmp||" ${worksrcpath}/config/${f}
        }
        if {[variant_isset g95]} {
            reinplace "s|-lgfortran|${compilers.libfortran}|" ${worksrcpath}/config/${f}
        } else {
            reinplace "s|-lgfortran|-L ${prefix}/lib/libgcc -lgfortran -lgomp|" ${worksrcpath}/config/${f}
        }
        reinplace "s| gfortran$| ${configure.fc}|" ${worksrcpath}/config/${f}
        reinplace "s|gcc$|${configure.cc}|" ${worksrcpath}/config/${f}
        reinplace "s|g\\\\+\\\\+$|${configure.cxx}|" ${worksrcpath}/config/${f}
        reinplace "/CcOptions/d" ${worksrcpath}/config/${f}
        reinplace "/FcOptions/d" ${worksrcpath}/config/${f}
        reinplace "/ArchRecLibSearch/d" ${worksrcpath}/config/${f}
        reinplace "/ArchRecIncSearch/d" ${worksrcpath}/config/${f}
        reinplace "s|-lXext$|-lXext\\
#define ArchRecLibSearch    ${configure.ldflags} \\
#define ArchRecIncSearch    ${configure.cppflags} \\
#define CcOptions -fPIC -D_DARWIN_C_SOURCE ${configure.cflags}\\
#define FcOptions -fPIC -fno-range-check -Wall ${configure.fflags}\\
#define ManRoot ${destroot}${prefix}/share/man\\
#define NetCDFlib -lnetcdf\\
#define HDFEOS5lib -lhe5_hdfeos -lGctp|" ${worksrcpath}/config/${f}
# A workaround for XQuartz 2.7.3 on Mountain Lion known as ncl.xq.fix
        if {${os.platform} eq "darwin" && ${os.major} >= 12} {
            reinplace "s|-D_XOPEN_SOURCE|-D_XOPEN_SOURCE -D__JIRA1530__|" ${worksrcpath}/config/${f}
        }
    }
    if {[variant_isset g95]} {
        reinplace "s|-fno-range-check||" ${worksrcpath}/config/Darwin_Intel
    }

    # run ymake
    system -W ${worksrcpath}/config \
        "CC=${configure.cc} \
        CFLAGS=[shellescape ${configure.cflags}] \
        make -f Makefile.ini && \
        ./ymake -config `pwd`"

    # copy triangle.[ch]
    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
y
${worksrcpath} ${prefix}/lib/hdfeos5/lib ${prefix}/lib/proj5/lib ${prefix}/lib
${prefix}/include/freetype2 ${prefix}/include/udunits2 ${prefix}/lib/hdfeos5/include ${prefix}/lib/proj5/include ${prefix}/include


EOF"
}
post-configure {
    system -W ${worksrcpath} "make Info"
    system "cat ${worksrcpath}/config/Site.local"
}

# Does not seem to set rpath
build.args-append               EXTRA_LDFLAGS=\"-Wl,-rpath,${prefix}/lib/libgcc\"
build.target                    Build
if {${os.major} < 10} {
    if {[variant_isset g95]} {
        build.args-append       EXTRA_CCOPTIONS=\"${configure.cc_archflags}\"
    } else {
        if {[string match *64* ${configure.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"
    }
}

# Adhoc fix
post-destroot {
    foreach f {ConvertMapData WriteLineFile WriteNameFile WritePlotcharData
               ezmapdemo findg fontc graphc graphc ncargworld ncl nnalg
               pre2ncgm.prog pre2ncgm.prog psblack pswhite pwritxnt tdpackdemo tgks0a tlocal} {
        foreach l {gfortran.5 gomp.1} {
            system -W ${destroot}${prefix}/bin \
                "install_name_tool -change @rpath/lib${l}.dylib ${prefix}/lib/libgcc/lib${l}.dylib ${f}"
        }
        # Do we have libquadmath on ppc64 and riscv64?
        if {${configure.build_arch} in [list arm64 x86_64]} {
            system -W ${destroot}${prefix}/bin \
                "install_name_tool -change @rpath/libquadmath.0.dylib ${prefix}/lib/libgcc/libquadmath.0.dylib ${f}"
        }
    }
}

default_variants            +accelerate

if {[variant_isset g95]} {
    configure.fflags-append -fno-second-underscore
}

variant accelerate description {use Accelerate.framework} {
    depends_lib-append      port:vecLibFort
}

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