# -*- 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           muniversal 1.0

name                ghostscript
version             10.05.1
revision            0

categories          print
license             AGPL-3 BSD
maintainers         nomaintainer
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.
homepage            https://www.ghostscript.com/

master_sites        https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs[strsed ${version} {g/\.//}]:source \
                    sourceforge:project/gs-fonts/gs-fonts/6.0%20%28misc%2C%20AFPL%29/:otherfonts \
                    sourceforge:project/gs-fonts/gs-fonts/8.11%20%28base%2035%2C%20GPL%29/:stdfonts \
                    https://github.com/adobe-type-tools/mapping-resources-pdf/archive/:misc

# Note: this needs to be manually updated for new upstream commits
set mappingresources_commit \
                    2dd5e53fb74a01718b9dfd448a0d1cce6fff2aa5

distname            ghostpdl-${version}
distfiles           ${distname}.tar.gz:source \
                    ghostscript-fonts-other-6.0.tar.gz:otherfonts \
                    ghostscript-fonts-std-8.11.tar.gz:stdfonts \
                    ${mappingresources_commit}.zip:misc

checksums           ${distname}.tar.gz \
                    rmd160  d9dca9baac0c5b897f21eed1d39b66d9448f4d81 \
                    sha256  c0be073366d19471320dce13e210b4c47e14b01070d6cf3d2d6d6e8415344615 \
                    size    98843388 \
                    ghostscript-fonts-other-6.0.tar.gz \
                    rmd160  ab60dbf71e7d91283a106c3df381cadfe173082f \
                    sha256  4fa051e341167008d37fe34c19d241060cd17b13909932cd7ca7fe759243c2de \
                    size    796086 \
                    ghostscript-fonts-std-8.11.tar.gz \
                    rmd160  10a19a10d0388bc084a7c1d3da845068d7169054 \
                    sha256  0eb6f356119f2e49b2563210852e17f57f9dcc5755f350a69a46a0d641a0c401 \
                    size    3752871 \
                    ${mappingresources_commit}.zip \
                    rmd160  a65458ab9955421cf3085cf84a6eac299e8c93cb \
                    sha256  e3971985977cee4b75f6b49f6e43842d3b699c4255d010adb82796073e98fbfe \
                    size    1601563

extract.only        ${distname}.tar.gz \
                    ghostscript-fonts-other-6.0.tar.gz \
                    ghostscript-fonts-std-8.11.tar.gz

post-extract {
    system -W ${workpath} "unzip '${distpath}/${mappingresources_commit}.zip'"

    foreach d {expat freetype jbig2dec jpeg lcms2mt libpng openjpeg tiff zlib} {
        move ${worksrcpath}/${d} ${worksrcpath}/${d}_local
    }

    # https://trac.macports.org/ticket/62832
    delete ${worksrcpath}/tesseract
    delete ${worksrcpath}/leptonica

    #move ${workpath}/MappingOther/Adobe-CNS1-ETen-B5 ${workpath}/MappingOther/Adobe-CNS1-ETenms-B5
    copy -force {*}[glob ${workpath}/mapping-resources-pdf-${mappingresources_commit}/pdf2unicode/*] ${worksrcpath}/Resource/CMap
    copy -force {*}[glob ${workpath}/mapping-resources-pdf-${mappingresources_commit}/pdf2other/*]    ${worksrcpath}/Resource/CMap
}

patchfiles-append   patch-base_unixinst.mak.diff

post-patch {
    reinplace "s|ZLIBDIR=src|ZLIBDIR=${prefix}/include|" \
                    ${worksrcpath}/configure.ac

    # Ensure that MacPorts perl is used everywhere
    fs-traverse f ${worksrcpath} {
        if {[string match *.pl ${f}]} {
            reinplace -E "s:(/usr/bin/perl|/usr/bin/env perl):${prefix}/bin/perl:g" ${f}
        }
    }
}

depends_build-append \
                    port:pkgconfig

depends_lib-append \
                    port:fontconfig \
                    port:freetype \
                    port:jbig2dec \
                    port:lcms2 \
                    port:libiconv \
                    port:libidn \
                    path:include/turbojpeg.h:libjpeg-turbo \
                    port:libpaper \
                    port:libpng \
                    port:openjpeg \
                    port:tiff \
                    port:zlib

depends_run-append \
                    port:perl5

use_autoreconf      yes
autoreconf.args     -fv
configure.checks.implicit_function_declaration.whitelist-append strchr

# tell ghostscript it's OK to use the system pkg-config even when cross-compiling
# see https://trac.macports.org/ticket/66627
configure.env-append \
                    DARWIN_LDFLAGS_SO_PREFIX="${prefix}/lib/" \
                    PKGCONFIG=${prefix}/bin/pkg-config

# https://trac.macports.org/ticket/56137
configure.ldflags-prepend \
                    -Lsobin

# Make included OpenJPEG uses its own headers rather than the system ones
configure.cppflags-replace \
                    -I${prefix}/include \
                    -isystem${prefix}/include

configure.args-append \
                    --disable-compile-inits \
                    --disable-cups \
                    --disable-dbus \
                    --disable-gtk \
                    --with-system-libtiff \
                    --without-gpdl \
                    --without-pcl \
                    --without-tesseract \
                    --without-x \
                    --without-xps

build.target        so

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

    if {[variant_isset ghostpdl]} {
        ln -s gpcl6c ${destroot}/${prefix}/bin/gpcl6
        ln -s gxpsc ${destroot}/${prefix}/bin/gxps
        ln -s gpdlc ${destroot}/${prefix}/bin/gpdl
    }

    xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 0644 -W ${worksrcpath} \
        LICENSE DroidSansFallback.NOTICE \
        ${destroot}/${prefix}/share/doc/${name}

    if {[variant_isset ghostpdl]} {
        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/pcl
        xinstall -m 0644 -W ${worksrcpath}/pcl \
            COPYING.AFPL LICENSE NEWS README.txt \
            ${destroot}${prefix}/share/doc/${name}/pcl

        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/pcl/pcl
        xinstall -m 0644 -W ${worksrcpath}/pcl/pcl \
            Anomalies.txt \
            ${destroot}${prefix}/share/doc/${name}/pcl/pcl

        xinstall -m 755 -d ${destroot}${prefix}/share/doc/${name}/pcl/pxl
        xinstall -m 0644 -W ${worksrcpath}/pcl/pxl \
            pxcet.txt pxdiff.txt pxfts.txt pxlib.txt pxspec.txt \
            ${destroot}${prefix}/share/doc/${name}/pcl/pxl
    }

    # std fonts - install into FontCache-compatible directory.
    # Check: could break on case-sensitive file systems...
    xinstall -m 0755 -d ${destroot}${prefix}/share/fonts/Type1/gsfonts
    xinstall -m 0644 \
            {*}[glob -directory ${workpath}/fonts {[a-z][0-9][0-9][0-9][0-9][0-9][0-9][a-z].*} fonts.scale fonts.dir] \
            ${destroot}${prefix}/share/fonts/Type1/gsfonts

    # Delete the already copied fonts to not copy them again when installing the "other" fonts.
    delete {*}[glob -directory ${workpath}/fonts {[a-z][0-9][0-9][0-9][0-9][0-9][0-9][a-z].*}]

    # other fonts - install into private ghostscript directory.
    xinstall -m 755 -d ${destroot}${prefix}/share/${name}/fonts
    xinstall -m 644 {*}[glob -directory ${workpath}/fonts *.afm *.gsf *.pfa *.pfm] \
            ${destroot}${prefix}/share/${name}/fonts

    # std fonts - "documentation"
    xinstall -m 0755 -d ${destroot}${prefix}/share/doc/${name}/fonts
    xinstall -m 0644 -W ${workpath}/fonts \
            COPYING ChangeLog README README.tweaks TODO \
            ${destroot}${prefix}/share/doc/${name}/fonts

    if {[variant_isset ghostpdl]} {
        # Install PCL base fonts into urwfonts directory
        xinstall -m 755 -d ${destroot}${prefix}/share/${name}/urwfonts
        xinstall -m 644 {*}[glob -directory ${worksrcpath}/pcl/urwfonts *.ttf] \
            ${destroot}${prefix}/share/${name}/urwfonts
    }
}

post-activate {
    system "${prefix}/bin/fc-cache -v ${prefix}/share/fonts/Type1/gsfonts"
}

configure.universal_args-delete --disable-dependency-tracking

variant x11 {
    depends_lib-append      port:xorg-libXext
    depends_lib-append      port:xorg-libXt
    configure.args-replace  --without-x --with-x
}

variant cups description {Enable CUPS driver} {
    configure.args-replace  --disable-cups --enable-cups
}

variant ghostpdl description {Install GhostPDL} {
    license-append          noncommercial
    depends_lib-append      port:expat
    configure.args-replace  --without-pcl --with-pcl=gpcl6
    configure.args-replace  --without-xps --with-xps=gxps
    configure.args-replace  --without-gpdl --with-gpdl=gpdl
    notes "
    GhostPCL requires a set of truetype fonts to function properly. To use
    the default URW font set, add the following to your shell profile:

        export PCLFONTSOURCE='${prefix}/share/${name}/urwfonts/'

    These fonts are distributed under the Aladdin Free Public License which
    bars commercial use. For more information see
    ${prefix}/share/doc/${name}/pcl/LICENSE
    "
}

default_variants    +x11

livecheck.type      regex
livecheck.url       ${homepage}releases.html
livecheck.regex     "Ghostscript (\\d+(?:\\.\\d+)*)"
