# Build recipe for isapnptools.
#
# Copyright (c) 2017-2018 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.

program=isapnptools
version=1.27
release=3

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/tools"

tarname=${program}-${version}.tgz

# Remote source(s)
fetch=http://www.roestockfox.co.uk/files/isapnptools/$tarname

description="
ISA plug and play configuration utility.

Two programs, one allows the dumping of resource data and generation of
a skeleton configuration file, the other configures ISA PnP hardware
using a configuration file.
"

homepage=http://www.roestockfox.co.uk/isapnptools/index.html
license=GPLv2+

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

build()
{

    # Check architecture
    case $arch in
    i?86)
        true
        ;;
    *)
        echo "isapnptools is for the x86 platform-only ..." 1>&2
        return 0
        ;;
    esac

    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

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

    # Fix compilation on musl
    patch -p0 < "${worktree}/patches/isapnptools/isapnptools-musl.patch"

    # Apply fixes from NetBSD (thanks!)
    # ftp://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/sysutils/isapnptools/patches/
    for file in patch-aa patch-ab patch-ac patch-ad patch-ae
    do
        patch -p0 < "${worktree}/patches/isapnptools/$file"
    done

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

    ./configure CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS -static" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --disable-realtime \
     --disable-pci \
     --build="$(cc -dumpmachine)"

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

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

