# Build recipe for pkgconf.
#
# Copyright (c) 2016-2021 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=pkgconf
version=1.8.0
release=1

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

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

# Remote source(s)
fetch=https://distfiles.dereferenced.org/pkgconf/$tarname

description="
Package compiler and linker metadata toolkit.

pkgconf is a program which helps to configure compiler and linker flags
for development frameworks.  It is similar to pkg-config from
freedesktop.org.

libpkgconf is a library which provides access to most of pkgconf's
functionality, to allow other tooling such as compilers and IDEs
to discover and use frameworks configured by pkgconf.
"

homepage=https://git.dereferenced.org/pkgconf/pkgconf
license=Custom

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

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

    cd "$srcdir"

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

    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-static=yes \
     --enable-shared=yes \
     --with-pkg-config-dir="/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig" \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    #make -j${jobs} check;	# It needs "kyua" ...
    make -j${jobs} DESTDIR="$destdir" install-strip

    # Create symlink-compatibility for those
    # programs looking for `pkg-config'
    ln -s pkgconf "${destdir}/usr/bin/pkg-config"

    # Include profile file
    mkdir -p "${destdir}/etc/profile.d"
    cp -p "${worktree}/archive/pkgconf/etc/profile.d/pkgconf.sh" \
           "${destdir}/etc/profile.d/"
    chmod 644 "${destdir}/etc/profile.d/pkgconf.sh"

    # To handle config file(s)
    touch "${destdir}/etc/profile.d/.graft-config"

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

