# -*- 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 80320 2011-07-09 01:08:09Z takeshi@macports.org $

PortSystem                  1.0

name                        ncarg
version                     6.0.0
revision                    1
categories                  science
maintainers                 takeshi
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
master_sites                http://www.ncl.ucar.edu/Download/files/:g2clib

distname                    ncl_${name}-${version}
worksrcdir                  ncl_${name}-${version}
set ncl_src             ${distname}${extract.suffix}
set g2clib_version      1.2.1
set g2clib_src          g2clib_with_changes-${g2clib_version}${extract.suffix}
set g2clib_worksrcdir   g2clib-${g2clib_version}-patches
distfiles           ${ncl_src}:ncl \
                    ${g2clib_src}:g2clib
checksums   ${ncl_src} \
                    md5     0afca904b8f51abdd3efada4c0dbaa42 \
                    sha1    949dc8fc823256c1a036aa06a3e68805a4099009 \
                    rmd160  e5214051eb23e8cc58e0d579a382c80476d570d7 \
            ${g2clib_src} \
                    md5     253327b963bf054c69f70df227e83cfb \
                    sha1    a0ae0849da6c2aba427cf4384c88433e67cc8f77 \
                    rmd160  0bea38746c9fb4eeeedb4546a5cb8b1a3936f7ee


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:libxml2    \
                            port:xorg-libXaw \
                            lib:libXm:lesstif
depends_build               port:triangle

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

if {$build_arch eq "x86_64"} {
    patchfiles-append           patch-config-ymake.diff
}
post-patch {
    reinplace "s|INC=-I/usr/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_arc 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}
            reinplace "s| gfortran$| g95|" ${worksrcpath}/config/${f} 
        } elseif {[variant_isset gcc43]} {
            reinplace "s|-lgfortran|-L${prefix}/lib/gcc43 -lgfortran|" \
                ${worksrcpath}/config/${f}
            reinplace "s| gfortran$| gfortran-mp-4.3|" ${worksrcpath}/config/${f}
        } elseif {[variant_isset gcc45]} {
            reinplace "s|-lgfortran|-L${prefix}/lib/gcc45 -lgfortran|" \
                ${worksrcpath}/config/${f}
            reinplace "s| gfortran$| gfortran-mp-4.5|" ${worksrcpath}/config/${f}
        } else {
            reinplace "s|-lgfortran|-L${prefix}/lib/gcc44 -lgfortran|" \
                ${worksrcpath}/config/${f}
            reinplace "s| gfortran$| gfortran-mp-4.4|" ${worksrcpath}/config/${f}
        }
        reinplace "s|gcc$|${configure.cc}|" ${worksrcpath}/config/${f}
        reinplace "/CcOptions/d" ${worksrcpath}/config/${f}
        reinplace "s|-lXext$|-lXext\\
#define CcOptions -fPIC -ansi -D_DARWIN_C_SOURCE -DH5_USE_16_API \\
#define COptimizeFlag ${configure.cflags}\\
#define FOptimizeFlag ${configure.fflags}\\
#define ManRoot ${destroot}${prefix}/share/man\\
#define NetCDF4lib -lnetcdf\\
#define HDFEOS5lib -lhe5_hdfeos -lGctp -lhdf5|" ${worksrcpath}/config/${f}
        reinplace "s|-L/usr/X11R6/lib -L/sw/lib|-L${prefix}/lib|" ${worksrcpath}/config/${f}
        reinplace "s|-I/usr/X11R6/include -I/sw/include|-I${prefix}/include|" ${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
}
#define NetCDF4lib -lnetcdf -lhdf5_hl -lhdf5 -lz -lsz -lmfhdf -ldf -lcurl -lidn -lssl -lcrypto|" ${worksrcpath}/config/${f}

pre-configure {
    system "cd ${workpath}/${g2clib_worksrcdir}; make all"
    file copy ${workpath}/${g2clib_worksrcdir}/libgrib2c.a ${worksrcpath}
    system "cd ${worksrcpath}/config; \
        make -f Makefile.ini; \
        ./ymake -config `pwd`"
    system "unzip -o ${prefix}/var/macports/distfiles/triangle/triangle.zip -d /tmp; \
        mv /tmp/triangle.* ${worksrcpath}/ni/src/lib/hlu"
}
configure {
    system "cd ${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 {
    system "cd ${worksrcpath}; make Info"
    system "cat ${worksrcpath}/config/Site.local"
}

build.target                Build
build.args-append           EXTRA_CCOPTIONS=\"${configure.cc_archflags}\"

variant g95 conflicts gcc43 gcc44 gcc45 description {compile with g95} {
    depends_build-append    port:g95
}

variant gcc43 conflicts gcc44 gcc45 g95 description {compile with gcc 4.3} {
    depends_build-append    port:gcc43
}

variant gcc44 conflicts gcc43 gcc45 g95 description {compile with gcc 4.4} {
    depends_build-append    port:gcc44
}

variant gcc45 conflicts gcc43 gcc44 g95 description {compile with gcc 4.5} {
    depends_build-append    port:gcc45
}

if {![variant_isset gcc43] && ![variant_isset gcc45] && ![variant_isset g95]} {
    default_variants +gcc44
}

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