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

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

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

# Remote source(s)
fetch=https://www.netfilter.org/projects/iptables/files/$tarname

description="
The main firewall program in GNU/Linux.

iptables is the userspace command line program used to configure the
Linux 2.4.x and later packet filtering ruleset.  It is targeted towards
system administrators.

Since Network Address Translation is also configured from the packet
filter ruleset, iptables is used for this, too.

The iptables package also includes ip6tables. ip6tables is used for
configuring the IPv6 packet filter.
"

homepage=https://www.netfilter.org/projects/iptables/index.html
license="GPLv2 only"

# Source documentation
docs="COMMIT_NOTES COPYING INCOMPATIBILITIES"
docsdir="${docdir}/${program}-${version}"

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

    cd "$srcdir"

    # Apply fixes from "Alpine Linux" in order to build IPTables against Musl
    patch -Np1 -i "${worktree}/patches/iptables/fix-u_int16_t.patch"
    patch -Np1 -i "${worktree}/patches/iptables/fix-xtables.patch"

    # 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} \
     --docdir=$docsdir \
     --mandir=$mandir \
     --enable-devel \
     --enable-libipq \
     --enable-bpf-compiler \
     --enable-nfsynproxy \
     --with-xt-lock-name=/var/lock/xtables.lock \
     --without-kernel \
     --build="$(gcc -dumpmachine)"

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

    # Manage dot new config file
    touch "${destdir}/etc/.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"
}

