# Build recipe for ghostscript.
#
# Copyright (c) 2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=ghostscript
version=10.0.0
release=2

# Define a category for the output of the package name
pkgcategory=printing

tarname=${program}-${version}.tar.gz
tarname_fonts_one=ghostscript-fonts-std-8.11.tar.gz
tarname_fonts_two=gnu-gs-fonts-other-6.0.tar.gz

# Remote source(s)
fetch="
 https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs1000/$tarname
 https://downloads.sourceforge.net/gs-fonts/$tarname_fonts_one
 https://downloads.sourceforge.net/gs-fonts/$tarname_fonts_two
"

description="
An interpreter for the PostScript language and PDF files.

Ghostscript is an interpreter for the PostScript language and PDF files.
It is available under either the GNU GPL Affero license or licensed for
commercial use from Artifex Software, Inc.  It has been under active
development for over 30 years and has been ported to several different
systems during this time.  Ghostscript consists of a PostScript
interpreter layer and a graphics library.
"

homepage=https://www.ghostscript.com
license=Custom

# Source documentation
docs="LICENSE"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Apply patch to fix the build (Patch borrowed from "Alpine Linux")
    patch -Np1 -i "${worktree}/patches/ghostscript/fix-sprintf.patch"

    # Apply patch to pick up the external zlib library ("Alpine Linux")
    patch -Np1 -i "${worktree}/patches/ghostscript/ghostscript-system-zlib.patch"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Build 'ghostscript' using external libraries
    rm -rf cups/libs freetype lcms2mt jpeg libpng openjpeg tiff zlib ijs

    ./configure CPPFLAGS="$QICPPFLAGS" \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --infodir=$infodir \
     --mandir=$mandir \
     --docdir=$docsdir \
     --htmldir=${docsdir}/html \
     --enable-dynamic \
     --enable-cups \
     --disable-compile-inits \
     --with-fontpath=/usr/share/fonts/X11/TTF:/usr/share/fonts/X11/Type1:/usr/share/fonts \
     --with-system-libtiff \
     --with-ijs \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install
    make -j${jobs} V=1 so
    make -j${jobs} DESTDIR="$destdir" soinstall

    # Install more headers, making symlink for compatibility

    install -p -m 644 base/*.h "${destdir}/usr/include/ghostscript"
    ( cd "${destdir}/usr/include" && ln -sf ghostscript ps )

    # Ship additional fonts
    (
        cd "${destdir}/usr/share/ghostscript"
        unpack "${tardir}"/$tarname_fonts_one
        unpack "${tardir}"/$tarname_fonts_two
        chmod -R u+w,go-w,a+rX-s .
    )
    unset -v tarname_fonts_one tarname_fonts_two

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

