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

compilers.setup             require_fortran
compilers.allow_arguments_mismatch \
                            yes

name                        ncarg
version                     6.6.2
revision                    33
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        https://www.ncl.ucar.edu/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

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

universal_variant           no
compilers.enforce_c         hdf5
configure.cxx_stdlib        libstdc++
use_parallel_build          no

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
if {$build_arch eq "x86_64" | $build_arch eq "arm64"} {
    patchfiles-append       patch-config-ymake.diff
}
if { $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
}

if {${os.platform} eq "darwin" && ${os.major} < 12} {
    pre-fetch {
        ui_error "${name} is not supported on Lion or older."
        error "unsupported platform"
    }
}

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 {$build_arch eq "x86_64" || $build_arch eq "ppc64"} {
       set defs "-DUSE_JPEG2000 -DUSE_PNG -D__64BIT__"
    } else {
       set defs "-DUSE_JPEG2000 -DUSE_PNG"
    }
    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 {[regexp clang ${configure.compiler}]} {
        reinplace "s| -lquadmath||" ${worksrcpath}/config/Darwin_Intel
    }
    reinplace "s|g\\\\+\\\\+$|${configure.cxx}|" ${worksrcpath}/config/Darwin_Intel
    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 "/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 -m64 -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* $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 quadmath.0} {
            system -W ${destroot}${prefix}/bin \
                "install_name_tool -change @rpath/lib${l}.dylib ${prefix}/lib/libgcc/lib${l}.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]+)}
