# -*- 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 90820 2012-03-15 03:13:40Z jmr@macports.org $

PortSystem          1.0

name                pgplot
conflicts           miriad
version             5.2.2
revision            6
categories          graphics devel
platforms           darwin
maintainers         mcalhoun openmaintainer

description         C/Fortran graphics library

long_description    The PGPLOT Graphics Subroutine Library is a Fortran- or \
                    C-callable, device-independent graphics package for making \
                    simple scientific graphs. It is intended for making \
                    graphical images of publication quality with minimum \
                    effort on the part of the user. For most applications, the \
                    program can be device-independent, and the output can be \
                    directed to the appropriate device at run time.

homepage            http://www.astro.caltech.edu/~tjp/pgplot/
master_sites        ftp://ftp.astro.caltech.edu/pub/pgplot/

distname            ${name}[join [split ${version} .] ""]
worksrcdir          ${name}
configure.dir       ${workpath}/build
build.dir           ${configure.dir}

checksums           md5     e8a6e8d0d5ef9d1709dfb567724525ae \
                    sha1    1f1c9aa17eeec9a2fb23fd15a0e4a91dcc49ddc1 \
                    rmd160  4c21cad6613772f4104c65ac3d8d9a42e3c4afe1

depends_build       bin:perl:perl5 \
                    port:pkgconfig

depends_lib         port:libpng \
                    port:zlib \
                    port:xorg-libX11

patchfiles          patch-makemake.diff \
                    patch-drivers.list.patch \
                    patch-proccom.c.diff

# Tell the compiler not to treat backslash characters as C-style escape sequences.
configure.fcflags-append -fno-backslash

set compat_version  [join [lrange [split ${version} .] 0 0] .]

post-extract {
    file mkdir ${build.dir}
}

post-patch {
    # Ensure MacPorts libraries are being used.
    reinplace "s|\\(-L\\\\`pwd\\\\`\\)|\\1 -L${prefix}/lib|" ${worksrcpath}/makemake

    # Look in the correct place for run-time data files.
    reinplace "s|/usr/local/pgplot/|${prefix}/share/${name}/|" ${worksrcpath}/src/grgfil.f

    # /usr/local/bin/perl may not exist.
    reinplace "s|#!/usr/local/bin/perl|#!/usr/bin/env perl|" ${worksrcpath}/makehtml ${worksrcpath}/maketex

    copy ${worksrcpath}/drivers.list ${build.dir}

    set drivers "GIF VGIF LATEX PNG TPNG PS VPS CPS VCPS"

    if {![variant_isset no_x11]} {
        # XDISP does not work because the program figdisp can not be found.
        lappend drivers XWINDOW XSERVE
    }

    if {[variant_isset aquaterm]} {
        lappend drivers AQT
    }

    foreach driver ${drivers} {
        reinplace "s|!\\(.*/${driver} .*\\)| \\1|" ${build.dir}/drivers.list
    }
}

pre-configure {
    set shlib_opts \
        "-dynamiclib -single_module -current_version ${version} -compatibility_version ${compat_version}"

    if {![variant_isset no_x11]} {
        # XQuartz or MacPorts X11 (pkg-config gives precedence to MacPorts X11)
        eval configure.ldflags-append   [exec pkg-config x11 --libs-only-L]
        set x11Libs                     [exec pkg-config x11 --libs-only-l]
        eval configure.cppflags-append  [exec pkg-config x11 --cflags]
    } else {
        set x11Libs ""
    }

    if {[variant_isset aquaterm]} {
        set aqtLibs "-laquaterm -Wl,-framework -Wl,Foundation"
    } else {
        set aqtLibs ""
    }

    # Create configuration file.
    #    For a description of options, see ${worksrcpath}/sys_linux/g77_gcc.conf
    lappend options XINCL           "${configure.cppflags}"
    lappend options MOTIF_INCL      ""
    lappend options ATHENA_INCL     ""
    lappend options TK_INCL         ""
    lappend options RV_INCL         ""
    lappend options FCOMPL          "${configure.fc}"
    lappend options FFLAGC          "${configure.fcflags}"
    lappend options FFLAGD          ""
    lappend options CCOMPL          "${configure.cc}"
    lappend options CFLAGC          "${configure.cppflags} ${configure.cflags}"
    lappend options CFLAGD          ""
    lappend options PGBIND_FLAGS    "bsd"
    lappend options LIBS            "${configure.ldflags} ${x11Libs} ${aqtLibs}"
    lappend options MOTIF_LIBS      ""
    lappend options ATHENA_LIBS     ""
    lappend options TK_LIBS         ""
    lappend options RANLIB          "/usr/bin/ranlib"
    lappend options SHARED_LIB      "libpgplot${version}.dylib"
    lappend options SHARED_LD       "${configure.fc} ${shlib_opts} ${configure.ldflags} ${x11Libs} -install_name ${prefix}/lib/libpgplot${compat_version}.dylib"
    lappend options SHARED_LIB_LIBS "${configure.ldflags} -lpng ${aqtLibs}"
    lappend options MCOMPL          "${configure.objc}"
    lappend options MFLAGC          "${configure.objcflags}"
    lappend options SYSDIR          "\\\$SYSDIR"

    # The following variables are not part of the unpatched pgplot.
    #     They have been added to facilitate building a shared C library.
    lappend options CSHARED_LIB     "libcpgplot${version}.dylib"
    lappend options CSHARED_LD      "${configure.fc} ${shlib_opts} ${configure.ldflags} ${x11Libs} -install_name ${prefix}/lib/libcpgplot${compat_version}.dylib"

    file mkdir ${worksrcpath}/sys_darwin
    foreach {var val} ${options} {
        system "/bin/echo \"   ${var}=\\\"${val}\\\"\" >> ${worksrcpath}/sys_darwin/marcports.conf"
    }

    # Create optional routine which makemake will find and compile.
    system "/bin/echo \"int iand_ (int *a, int *b) { return *a & *b;}\" >> ${worksrcpath}/sys_darwin/iand.c"
}

configure.cmd       ${worksrcpath}/makemake
configure.pre_args
configure.args      ${worksrcpath} ${os.platform}

# When linking against fortran code, PG_PPU gets number of underscores correct.
configure.cppflags-append -DPG_PPU

# Build the C PGPLOT wrapper library, HTML documentation, and TeX files.
build.target-append cpg pgplot.html pgplot-routines.tex

destroot {
    # pgxwin_server is only built if the X11 drivers are requested.
    # pgdisp is only built if the XDISP driver is requested.
    foreach fl [glob -directory ${destroot.dir} -nocomplain pgxwin_server pgdisp] {
        xinstall -m 755 ${fl} ${destroot}${prefix}/bin
    }

    foreach fl [glob -directory ${destroot.dir} -nocomplain *.dylib] {
        xinstall -m 755 ${fl} ${destroot}${prefix}/lib
    }

    foreach fl [glob -directory ${destroot.dir} -nocomplain *.a] {
        xinstall -m 644 ${fl} ${destroot}${prefix}/lib
    }

    foreach fl [glob -directory ${destroot.dir} -nocomplain *.h] {
        xinstall -m 644 ${fl} ${destroot}${prefix}/include
    }

    # Install run-time data files.
    xinstall -d -m 755 ${destroot}${prefix}/share/${name}
    foreach fl [glob -directory ${destroot.dir} -nocomplain *.dat *.txt] {
        xinstall -m 644 ${fl} ${destroot}${prefix}/share/${name}
    }

    # Install docs.
    xinstall -d -m 755 ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 ${worksrcpath}/copyright.notice ${destroot}${prefix}/share/doc/${name}
    foreach fl [glob -directory ${destroot.dir} -nocomplain *.doc *.html *.pdf] {
        xinstall -m 644 ${fl} ${destroot}${prefix}/share/doc/${name}
    }

    # Install examples.
    xinstall -d -m 755 ${destroot}${prefix}/share/${name}/examples
    foreach fl [glob -directory ${worksrcpath} examples/pgdemo*.f cpg/cpgdemo*.c drivers/*/pg*demo.*] {
        xinstall -m 644 ${fl} ${destroot}${prefix}/share/${name}/examples
    }
    foreach fl [glob -directory ${destroot.dir} *demo*] {
        xinstall -m 755 ${fl} ${destroot}${prefix}/share/${name}/examples
    }

    # Create more library names.
    ln -s libcpgplot${version}.dylib ${destroot}${prefix}/lib/libcpgplot.dylib
    ln -s libcpgplot${version}.dylib ${destroot}${prefix}/lib/libcpgplot[join [lrange [split ${version} .] 0 0] .].dylib
    ln -s libcpgplot${version}.dylib ${destroot}${prefix}/lib/libcpgplot[join [lrange [split ${version} .] 0 1] .].dylib

    ln -s libpgplot${version}.dylib ${destroot}${prefix}/lib/libpgplot.dylib
    ln -s libpgplot${version}.dylib ${destroot}${prefix}/lib/libpgplot[join [lrange [split ${version} .] 0 0] .].dylib
    ln -s libpgplot${version}.dylib ${destroot}${prefix}/lib/libpgplot[join [lrange [split ${version} .] 0 1] .].dylib
}

use_parallel_build  no

universal_variant   no

variant no_x11 {
    depends_lib-delete      port:xorg-libX11
    depends_build-delete    port:pkgconfig
}

variant aquaterm description {install driver for aquaterm} {
    depends_lib-append  port:aquaterm

    post-extract {
        copy ${prefix}/share/aquaterm/adapters/pgplot/aqdriv.m ${worksrcpath}/drivers
    }
}

variant gcc42 description {build with gfortran from gcc42} conflicts gcc43 gcc44 gcc45 gcc46 g95 {
    depends_lib-append  port:gcc42
    configure.fc        ${prefix}/bin/gfortran-mp-4.2
}

variant gcc43 description {build with gfortran from gcc43} conflicts gcc42 gcc44 gcc45 gcc46 g95 {
    depends_lib-append  port:gcc43
    configure.fc        ${prefix}/bin/gfortran-mp-4.3
}

variant gcc44 description {build with gfortran from gcc44} conflicts gcc42 gcc43 gcc45 gcc46 g95 {
    depends_lib-append  port:gcc44
    configure.fc        ${prefix}/bin/gfortran-mp-4.4
}

variant gcc45 description {build with gfortran from gcc45} conflicts gcc42 gcc43 gcc44 gcc46 g95 {
    depends_lib-append  port:gcc45
    configure.fc        ${prefix}/bin/gfortran-mp-4.5
}

variant gcc46 description {build with gfortran from gcc46} conflicts gcc42 gcc43 gcc44 gcc45 g95 {
    depends_lib-append  port:gcc46
    configure.fc        ${prefix}/bin/gfortran-mp-4.6
}

variant g95 description {build with g95} conflicts gcc42 gcc43 gcc44 gcc45 gcc46 {
    depends_lib-append  port:g95
    configure.fc        ${prefix}/bin/g95
}

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

variant docs description {Create and install docs} {
    depends_build-append bin:pdflatex:texlive

    post-build {
        system "cd ${build.dir} && pdflatex pgplot-routines.tex && pdflatex pgplot-routines.tex"
    }
}
