# $Id: Portfile 66405 2010-04-11 18:24:55Z takanori@macports.org $

PortSystem      1.0

name            ghostscript
version         8.71
revision        3
categories      print
maintainers     takanori openmaintainer
description     GPL Ghostscript, An interpreter for PostScript and PDF
long_description \
                Ghostscript is the well-known PostScript interpreter which \
                is available for all common and most esoteric platforms and \
                supports many different printers and some displays.
platforms       darwin
homepage        http://www.cs.wisc.edu/~ghost/
master_sites    sourceforge:${name}:source \
                sourceforge:gs-fonts:fonts \
                macports:pTeX:misc
distfiles       ${distname}.tar.gz:source \
                ghostscript-fonts-other-6.0.tar.gz:fonts \
                mappingresources4pdf_2unicode-20091208.tar.Z:misc \
                mappingresources4pdf_other-20091208.tar.Z:misc
patchfiles      patch-base_unix-dll.mak.diff \
                patch-base_stdpre.h.diff \
                libpng14-compat.diff
checksums       ${distname}.tar.gz rmd160 efce74cf22cf99b2b1a145df466e79a86e3dfefb \
                ghostscript-fonts-other-6.0.tar.gz rmd160 ab60dbf71e7d91283a106c3df381cadfe173082f \
                mappingresources4pdf_2unicode-20091208.tar.Z rmd160 bc1b86e6a5f0e022f88070195eb3e01e167114a7 \
                mappingresources4pdf_other-20091208.tar.Z    rmd160 1e2ea9af6be8b60c9a54963e542fa06ceb2daccd

depends_lib     port:xorg-libXext \
                port:xorg-libXt \
                path:lib/pkgconfig/cairo.pc:cairo \
                port:expat \
                port:fontconfig \
                port:jpeg \
                port:libiconv \
                port:libpaper \
                port:libpng \
                port:tiff \
                port:zlib \
                path:bin/perl:perl5

depends_build   port:pkgconfig

post-extract {
    foreach d {zlib libpng jpeg} {
        move "${worksrcpath}/${d}" "${worksrcpath}/${d}_local"
    }

    move ${workpath}/MappingOther/Adobe-CNS1-ETen-B5 ${workpath}/MappingOther/Adobe-CNS1-ETenms-B5
    eval copy -force [glob ${workpath}/ToUnicode/*]    ${worksrcpath}/Resource/CMap
    eval copy -force [glob ${workpath}/MappingOther/*] ${worksrcpath}/Resource/CMap
}

post-patch {
    reinplace "s|__PREFIX__|${prefix}|" ${worksrcpath}/base/unix-dll.mak

    # Ensure that MacPorts perl is used
    fs-traverse f ${worksrcpath} {
        if [string match "*.pl" ${f}] {
            reinplace "s|#!/usr/bin/perl|#!${prefix}/bin/perl|" ${f}
        }
    }
}

use_autoreconf yes
autoreconf.args -fv

# Make included JasPer uses its own headers rather than the system ones
configure.cppflags-delete       "-I${prefix}/include"
configure.cppflags-append       "-isystem${prefix}/include"

# Support for building ppc64 binaries. (#24380)
configure.ldflags-append        ${configure.cc_archflags}

# gs8.71: The default optimization flag (-O2) causes pdfwrite's strange 'Memory fault' problem.
#         See also: http://bugs.ghostscript.com/show_bug.cgi?id=691005
configure.optflags  -Os

configure.args  --disable-compile-inits \
                --disable-cups \
                --disable-gtk

pre-configure {
    # rebuild jbig2dec/configure manually
    system "cd ${worksrcpath}/jbig2dec && autoreconf ${autoreconf.pre_args} ${autoreconf.args}"
}

build.target        so
use_parallel_build  no

destroot.target     soinstall
post-destroot {
    system "strip ${destroot}${prefix}/bin/gsc ${destroot}${prefix}/bin/gsx"
    ln -s gsc ${destroot}${prefix}/bin/gs

    xinstall -m 755 -d ${destroot}${prefix}/share/${name}/fonts
    foreach f {*.afm *.gsf *.pfa *.pfm} {
        eval xinstall -m 644 [glob ${workpath}/fonts/${f}] ${destroot}${prefix}/share/${name}/fonts
    }

    xinstall -m 755 -d ${destroot}${prefix}/share/doc
    ln -s ../${name}/${version}/doc ${destroot}${prefix}/share/doc/${name}

    xinstall -m 755 -d ${destroot}${prefix}/share/${name}/${version}/examples/cjk
    eval xinstall -m 644 [glob ${worksrcpath}/examples/cjk/*.ps] ${destroot}${prefix}/share/${name}/${version}/examples/cjk
}

livecheck.distname      GPL Ghostscript

universal_variant       no

variant no_x11 {
    depends_lib-delete port:xorg-libXext
    depends_lib-delete port:xorg-libXt
    configure.args-append --without-x
}

variant cups description {Enable cups support} {
    configure.args-delete --disable-cups
    configure.args-append --enable-cups

    # Informing user enable cups will install files outside of the common directory structure
    destroot.violate_mtree yes
}

livecheck.type  regex
livecheck.url   http://ghostscript.com/releases/
livecheck.regex ${name}-(\\d+(?:\\.\\d+)*)
