# Build recipe for openjade.
#
# Copyright (c) 2019 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=openjade
version=1.3.2
release=1

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

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch=http://downloads.sourceforge.net/project/openjade/openjade/${version}/$tarname

description="
An implementation of DSSSL, a language to format SGML or XML documents.

OpenJade contains backends for various formats (RTF, HTML, TeX, MIF,
SGML2SGML, and FOT).
"

homepage=http://openjade.sourceforge.net
license=MIT

# Source documentation
docs="COPYING ChangeLog NEWS README VERSION"
docsdir="${docdir}/${program}-${version}"

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

    cd "$srcdir"

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

    # Update using patches from Gentoo
    patch -Np1 -i "${worktree}/patches/openjade/openjade-1.3.2-deplibs.patch"
    patch -Np1 -i "${worktree}/patches/openjade/openjade-1.3.2-ldflags.patch"
    patch -Np1 -i "${worktree}/patches/openjade/openjade-1.3.2-respect-ldflags.patch"
    patch -Np1 -i "${worktree}/patches/openjade/openjade-1.3.2-msggen.pl.patch"
    patch -Np1 -i "${worktree}/patches/openjade/openjade-1.3.2-libosp-la.patch"
    patch -Np1 -i "${worktree}/patches/openjade/openjade-1.3.2-gcc46.patch"
    patch -Np1 -i "${worktree}/patches/openjade/openjade-1.3.2-no-undefined.patch"
    patch -Np1 -i "${worktree}/patches/openjade/openjade-1.3.2-wchar_t-uint.patch"
    patch -Np1 -i "${worktree}/patches/openjade/openjade-1.3.2-chmod.patch"

    # Update for hosts based on musl
    cp -f "${worktree}/archive/common/config.guess" config/config.guess
    cp -f "${worktree}/archive/common/config.sub" config/config.sub

    ./configure CXXFLAGS="$QICXXFLAGS -fno-lifetime-dse" \
    LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --datadir=/usr/share/sgml/openjade-${version} \
     --enable-default-catalog=/etc/sgml/catalog \
     --enable-default-search-path=/usr/share/sgml \
     --enable-splibdir=/usr/lib${libSuffix} \
     --enable-http \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    mkdir -p "${destdir}/etc/sgml"
    make -j${jobs} DESTDIR="$destdir" install install-man

    # Remove libtool .la files
    rm -f "${destdir}/usr/lib${libSuffix}"/*.la

    # Create Jade equivalents

    ln -sf openjade        "${destdir}/usr/bin/jade"
    ln -sf openjade.1      "${destdir}${mandir}/jade.1"

    # Catalogs

    cp -p dsssl/catalog "${destdir}/usr/share/sgml/openjade-${version}"
    for file in dsssl/*.dtd dsssl/*.dsl dsssl/*.sgm
    do
        cp -p "$file" "${destdir}/usr/share/sgml/openjade-${version}/${file##*/}"
    done
    unset -v file
    chmod 644 "${destdir}/usr/share/sgml/openjade-${version}"/*

    # 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}"

    # Insert post-install script manually

    mkdir -p "${destdir}/var/lib/qi"
    cat << EOF > "${destdir}/var/lib/qi/${full_pkgname}.sh"

echo ""
echo "Updating etc/sgml/catalog ..."

install-catalog --add etc/sgml/openjade-${version}.cat \\
 /usr/share/sgml/openjade-${version}/catalog

install-catalog --add etc/sgml/sgml-docbook.cat \\
 /etc/sgml/openjade-${version}.cat

EOF
}

