# -*- 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 68229 2010-05-29 11:56:11Z takeshi@macports.org $

PortSystem                  1.0

name                        ncarg
version                     5.2.0
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/

distname                    ncl_${name}_src-${version}
worksrcdir                  ncl_${name}-${version}
checksums           md5     7ed710647a7eec8f500c7494853a2b65 \
                    sha1    4a5c0f2285ca49a11f56ee7148883154ec86379f \
                    rmd160  dfcd90bc94a1499b545b4191ed70b570b8c62c56

depends_lib                 port:jpeg6b     \
                            port: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:gcc43      \
                            port:triangle

use_parallel_build          no
universal_variant           no

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

set usedap y

if {$build_arch eq "x86_64"} {
    patchfiles                  patch-config-ymake.diff
}
post-patch {
    foreach f {Darwin Darwin_Intel} {
        if {[variant_isset g95]} {
            reinplace "s|-lgfortran|-lf95|" ${worksrcpath}/config/${f}
            reinplace "s| gfortran$| g95|" ${worksrcpath}/config/${f} 
        } else {
            reinplace "s|-lgfortran|-L${prefix}/lib/gcc43 -lgfortran|" \
                ${worksrcpath}/config/${f} 
            reinplace "s| gfortran$| gfortran-mp-4.3|" ${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 ${configure.cc_archflags}\\
#define COptimizeFlag ${configure.cflags}\\
#define FOptimizeFlag ${configure.fflags}\\
#define ManRoot ${destroot}${prefix}/share/man\\
#define NetCDF4lib -lnetcdf -lhdf5_hl -lhdf5 -lz -lsz -lmfhdf -ldf -lcurl -lidn -lssl -lcrypto|" ${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
}

pre-configure {
    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
${prefix}/lib/jpeg6b/lib ${prefix}/lib/netcdf-devel/lib ${prefix}/lib/hdfeos5/lib ${prefix}/lib
${prefix}/include/freetype2 ${prefix}/include/udunits2 ${prefix}/lib/jpeg6b/include ${prefix}/lib/netcdf-devel/include ${prefix}/lib/hdfeos5/include ${prefix}/include


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

build.target                Build

variant g95 description {compile with g95} {
    depends_build-delete    port:gcc43
    depends_build-append    port:g95
}

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