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

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

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

# Remote source(s)
fetch=https://sourceforge.net/projects/net-snmp/files/net-snmp/${version}/$tarname

description="
Simple Network Management Protocol (SNMP).

Net-SNMP is a Simple Network Management Protocol for monitoring the health
and welfare of network equipment (eg. routers), computer equipment and
even devices like UPSs.  It includes a suite of applications used to
implement SNMP v1, SNMP v2c and SNMP v3 using both IPv4 and IPv6.
"

homepage=http://www.net-snmp.org/
license="BSD 3-clause"

# Source documentation
docs="CHANGES COPYING ChangeLog FAQ NEWS README README.agentx README.sql README.thread TODO"
docsdir="${docdir}/${program}-${version}"

# Limit parallel compiler jobs for this package
jobs=1

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

    cd "$srcdir"

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

    # Apply fixes from "Alpine Linux".  Thanks!
    patch -Np1 -i "${worktree}/patches/net-snmp/fix-includes.patch"
    patch -Np1 -i "${worktree}/patches/net-snmp/netsnmp-swinst-crash.patch"

    # Apply some patches from Fedora.  Thanks!
    patch -Np1 -i "${worktree}/patches/net-snmp/net-snmp-5.7.2-cert-path.patch"
    patch -Np1 -i "${worktree}/patches/net-snmp/net-snmp-5.7.3-iterator-fix.patch"
    patch -Np1 -i "${worktree}/patches/net-snmp/net-snmp-5.8-Remove-U64-typedef.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} \
     --sysconfdir=/etc/snmp \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-static=no \
     --enable-as-needed \
     --enable-blumenthal-aes \
     --enable-ipv6 \
     --enable-ucd-snmp-compatibility \
     --disable-debugging \
     --disable-embedded-perl \
     --with-default-snmp-version=3 \
     --with-sys-contact="root@localhost" \
     --with-sys-location=unknown \
     --with-logfile=/var/log/snmpd \
     --with-temp-file-pattern=/tmp/snmpdXXXXXXXXXXXX \
     --with-copy-persistent-files=no \
     --with-persistent-directory=/var/lib/net-snmp \
     --with-python-modules \
     --with-perl-modules="INSTALLDIRS=vendor" \
     --without-rpm \
     --without-pcre \
     --build="$(gcc -dumpmachine)"

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

    # Follow Alpine Linux's example in removing things we don't want
    # to distribute, request it if this is **really necessary**.
    rm -f \
     "${destdir}"/usr/lib${libSuffix}/libsnmp* \
     "${destdir}"/usr/bin/snmpcheck \
     "${destdir}"/usr/bin/fixproc \
     "${destdir}"/usr/share/man/man1/fixproc* \
     "${destdir}"/usr/bin/ipf-mod.pl \
     "${destdir}"/usr/bin/snmpinform
     ln -sf snmptrap "${destdir}/usr/bin/snmpinform"

    install -p -m 644 EXAMPLE.conf -D "${destdir}/etc/snmp/snmpd.conf"
    install -m 755 -d "${destdir}/var/lib/net-snmp"

    # Install SNMP perp service

    mkdir -p "${destdir}/etc/perp/snmpd"

    cp -p "${worktree}/archive/net-snmp/rc.log" \
          "${worktree}/archive/net-snmp/rc.main" \
          "${destdir}/etc/perp/snmpd/"

    chmod 755 "${destdir}"/etc/perp/snmpd/rc.*

    # THIS SERVICE IS DISABLED BY DEFAULT
    chmod -t "${destdir}/etc/perp/snmpd"

    # Manage (dot) new files via graft(1)
    touch "${destdir}/etc/perp/snmpd/.graft-config" \
          "${destdir}/etc/snmp/.graft-config"

    # Clean up inventory file for Perl

    # This line removes 'destdir' adding the suffix '.lz' for manpages
    find "${destdir}/" -type f -name '.packlist' | while read -r file
    do
         sed -i \
          -e "s|${destdir}||g" \
          -e "s|\.[0-9][a-z]*|&.lz|g" \
          $file
    done

    find "${destdir}/" -type f -name 'perllocal.pod' -exec rm -f {} +

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

