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

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

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

# Remote source(s)
fetch=https://github.com/rockdaboot/libpsl/releases/download/${version}/$tarname

description="
A C library to handle the Public Suffix List.

A Public Suffix List is a collection of Top Level Domains (TLDs) suffixes.
TLDs include Global Top Level Domains (gTLDs) like .com and .net; Country
Top Level Domains (ccTLDs) like .de and .cn; and Brand Top Level Domains
like .apple and .google. Brand TLDs allows users to register their own top
level domain that exist at the same level as ICANN's gTLDs.

Brand TLDs are sometimes referred to as Vanity Domains.
"

homepage=https://github.com/rockdaboot/libpsl
license=MIT

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

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

    cd "$srcdir"

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

    # https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat
    #
    # Follow the example of Slackware, by installing the public list instead of
    # creating a separate package so that the build can proceed, which after all
    # will be overwritten.  We also follow the Fedora example for the
    # configuration of this package.  Thank you.  :-)
    if test ! -r /usr/share/publicsuffix/public_suffix_list.dat
    then
        mkdir -p /usr/share/publicsuffix
        install -p -m 644 -o root -g root \
         "${worktree}"/archive/libpsl/public_suffix_list.dat \
         /usr/share/publicsuffix/
        (
            cd /usr/share/publicsuffix && \
            ln -sf public_suffix_list.dat effective_tld_names.dat
        )
        install -p -m 644 -o root -g root \
         list/tests/tests.txt /usr/share/publicsuffix/test_psl.txt
    fi

    # Force to make use of Python v3
    # (Thanks to "Beyond Linux From Scratch")
    sed -i 's/env python/&3/' src/psl-make-dafsa

    ./configure CPPFLAGS="$QICPPFLAGS" \
    CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-static=no \
     --enable-shared=yes \
     --disable-doc \
     --enable-man \
     --enable-builtin=libicu \
     --enable-runtime=libidn2 \
     --with-psl-distfile=/usr/share/publicsuffix/public_suffix_list.dafsa \
     --with-psl-file=/usr/share/publicsuffix/effective_tld_names.dat \
     --with-psl-testfile=/usr/share/publicsuffix/test_psl.txt \
     --build="$(gcc -dumpmachine)"

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

    # Delete generated charset.alias
    rm -f "${destdir}/usr/lib${libSuffix}/charset.alias"
    rmdir "${destdir}/usr/lib${libSuffix}/" 2> /dev/null || true

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

