# Build recipe for nss.
#
# Copyright (c) 2021-2022 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=nss
version=3_74
pkgversion=$(echo $version | tr _ .)
release=1

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

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

# Remote source(s)
fetch=https://ftp.mozilla.org/pub/security/nss/releases/NSS_${version}_RTM/src/$tarname

description="
Network security services.

Network Security Services (NSS) is a set of libraries designed to
support cross-platform development of security-enabled client and
server applications.  Applications built with NSS can support
SSL v3, TLS, PKCS #5, PKCS #7, PKCS #11, PKCS #12, S/MIME,
X.509 v3 certificates, and other security standards.
"

homepage=https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS
license="MPL v2.0, GPLv2+"

# Source documentation
docs="COPYING readme.md"
docsdir="${docdir}/${program}-${pkgversion}"

# Set 'srcdir' using previous 'pkgversion' variable
srcdir=${program}-${pkgversion}

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

    cd "${srcdir}/nss"

    # Apply a patch for build thanks to "Beyond Linux From Scratch"
    patch -Np2 -i "${worktree}/patches/nss/nss-3.74-standalone-1.patch"

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

    # Set specific options for the architecture
    case $arch in
    amd64 | x32 )
        arch_options='USE_64=1'
        ;;
    i586)
        arch_options='USE_64=0'
        ;;
    esac

    unset -v CFLAGS CXXFLAGS

    make -j${jobs} BUILD_OPT=1 \
     NSS_DISABLE_GTESTS=1 \
     NSPR_INCLUDE_DIR=/usr/include/nspr \
     USE_SYSTEM_ZLIB=1 \
     ZLIB_LIBS=-lz \
     NSS_ENABLE_WERROR=0 \
     NSS_USE_SYSTEM_SQLITE=1 \
     $arch_options

    unset -v arch_options

    # Proceed to install the software files
    cd ../dist

    mkdir -v -p "${destdir}/usr/lib${libSuffix}" \
                "${destdir}/usr/include/nss" \
                "${destdir}/usr/bin" \
                "${destdir}/usr/lib${libSuffix}/pkgconfig"

    install -v -p -m755 Linux*/lib/*.so "${destdir}/usr/lib${libSuffix}"
    install -v -p -m644 Linux*/lib/*.chk "${destdir}/usr/lib${libSuffix}"
    install -v -p -m644 Linux*/lib/libcrmf.a "${destdir}/usr/lib${libSuffix}"

    cp -v -pRL public/nss/* "${destdir}/usr/include/nss"
    cp -v -pRL private/nss/* "${destdir}/usr/include/nss"
    chmod 644 "${destdir}"/usr/include/nss/*

    for file in certutil cmsutil crlutil derdump modutil pk12util pp \
                signtool signver ssltap vfychain vfyserv nss-config ; \
    do
        install -v -p -m755 Linux*/bin/${file} "${destdir}/usr/bin"
    done
    unset -v file

    install -v -p -m644 Linux*/lib/pkgconfig/nss.pc \
     "${destdir}/usr/lib${libSuffix}/pkgconfig"

    # Strip remaining binaries and libraries
    find "$destdir" -type f | xargs file | \
     awk '/ELF/ && /executable/ || /shared object/' | \
      cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true
    strip --strip-debug "${destdir}/usr/lib${libSuffix}"/*.a

    cd ../nss
    mkdir -v -p "${destdir}/${mandir}/man1"
    cp -v -p doc/nroff/*.1 "${destdir}/${mandir}/man1"
    chmod 644 "${destdir}/${mandir}"/man1/*

    # 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 -v -p "${destdir}/$docsdir"
    cp -v -p $docs "${destdir}/$docsdir"
}

