# Build recipe for opensp.
#
# 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=OpenSP
pkgname=opensp
version=1.5.2
release=1

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

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

# Remote source(s)
fetch=https://downloads.sourceforge.net/openjade/$tarname

description="
The opensp package contains a C++ library for using SGML/XML files.

This is useful for validating, parsing and manipulating
SGML and XML documents.
"

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

# Source documentation
docs="AUTHORS BUGS COPYING ChangeLog NEWS README"
docsdir="${docdir}/${pkgname}-${version}"

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

    cd "$srcdir"

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

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

    # Disable warnings while running openjade
    # (Thanks to Beyond of Linux From Scratch)

    sed -i 's/32,/253,/' lib/Syntax.cxx
    sed -i 's/LITLEN          240 /LITLEN          8092/' \
     unicode/gensyntax.pl unicode/unicode.syn

    ./configure CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --enable-static=no \
     --enable-shared=yes \
     --enable-default-catalog=/etc/sgml/catalog \
     --enable-default-search-path=/usr/share/sgml \
     --enable-http \
     --enable-xml-messages \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1 \
     pkgdatadir=/usr/share/sgml/OpenSP-${version} docdir=$docsdir

    make -j${jobs} \
     pkgdatadir=/usr/share/sgml/OpenSP-${version} docdir=$docsdir \
     DESTDIR="$destdir" install

    # Make symlinks for compatiblity (e.g OpenJade).
    # (Thanks to Beyond of Linux From Scratch)

    ln -sf onsgmls    "${destdir}"/usr/bin/nsgmls
    ln -sf osgmlnorm  "${destdir}"/usr/bin/sgmlnorm
    ln -sf ospam      "${destdir}"/usr/bin/spam
    ln -sf ospcat     "${destdir}"/usr/bin/spcat
    ln -sf ospent     "${destdir}"/usr/bin/spent
    ln -sf osx        "${destdir}"/usr/bin/sx
    ln -sf osx        "${destdir}"/usr/bin/sgml2xml

    # Make symlinks for manpages
    for file in nsgmls sgmlnorm spam spcat spent sx sgml2xml
    do
        ln -sf o${file}.1 "${destdir}/${mandir}/man1/${file}.1"
    done
    unset -v file

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

