#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

export CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe" 
export CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe -fno-exceptions -fno-rtti"

P=net-snmp
V=5.9.1
SRC=$P-$V 
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc libtool-dev openssl-dev libnl-dev libpci-dev "   
    for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

# list may have more than loaded
cd /usr/local/lib
rm -rf *.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/plugin/*.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/*.la
rm -rf gprofng/*.la 
rm -rf python3.8/site-packages/*.la
rm -rf pkcs11/*.la
rm -rf cairo/*.la
rm -rf gtk-2.0/modules/*.la
rm -rf gtk-2.0/2.10.0/printbackends/*.la
rm -rf gtk-2.0/2.10.0/immodules/*.la
rm -rf gtk-2.0/2.10.0/engines/*.la
rm -rf imlib2/loaders/*.la
rm -rf imlib2/filters/*.la
rm -rf gdk-pixbuf-2.0/2.10.0/loaders/*.la
rm -rf libnl/cli
cd /tmp
rm -rf /usr/lib/*.la
find / -name *.la  # only tcloop hits

su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://sourceforge.net/projects/$P/files/$P/$V/$SRC.tar.gz "  $USER
tar xvf $SRC*gz
cd $SRC

# disabling snmp v2c causes make error for sane magicolor.c...consider without-pcre

./configure --prefix=/usr/local --disable-manuals --enable-ucd-snmp-compatibility  \
--enable-ipv6 --disable-snmpv1 --disable-debugging --enable-static=no --without-rpm  \
--with-logfile="/var/log/snmpd.log" --with-default-snmp-version=3 \
--with-persistent-directory="/var/net-snmp" -with-sys-contact="root@localhost" --with-sys-location="Unknown" \
--with-mib-modules="host misc/ipfwacc ucd-snmp/diskio tunnel ucd-snmp/dlmod "

# configure takes 4M
# UCD-SNMP compatability:     enabled
# SNMP Versions Supported:    2c 3
# Building for:               linux
# Net-SNMP Version:           5.9.1
# Network transport support:  Callback Unix Alias TCP UDP TCPIPv6 UDPIPv6 IPv4Base SocketBase TCPBase 
#                             UDPIPv4Base UDPBase IPBase IPv6Base
# SNMPv3 Security Modules:    usm
# Agent MIB code:             default_modules host misc/ipfwacc tunnel ucd-snmp/diskio ucd-snmp/dlmod =>
#                  snmpv3mibs mibII ucd_snmp notification notification-log-mib target agent_mibs 
#                  agentx disman/event disman/schedule utilities host host/hrh_storage host/hrh_filesys 
#                  host/hrSWInstalledTable host/hrSWRunTable host/hr_system host/hr_device 
#                  host/hr_other host/hr_proc host/hr_network host/hr_print host/hr_disk host/hr_partition 
#                  util_funcs/header_simple_table tunnel/tunnel util_funcs/header_simple_table
# MYSQL Trap Logging:         unavailable
# Embedded Perl support:      disabled
# SNMP Perl modules:          disabled
# SNMP Python modules:        disabled
# Crypto support from:        crypto
# Authentication support:     MD5 SHA1 SHA224 SHA256 SHA384 SHA512
# Encryption support:         DES AES AES128 AES192 AES192C AES256 AES256C
# Local DNSSEC validation:    disabled

make NETSNMP_DONT_CHECK_VERSION=1   # takes 4M 40s 
make install DESTDIR=/tmp/$P  # install-strip fails
cd /tmp

# dev
#####
mkdir -p $P-dev/usr/local/lib/
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
rm -rf $P/usr/local/lib/*.la 

# main
#######
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/COPYING $P/usr/local/share/doc/$P

strip --strip-unneeded $P/usr/local/bin/*
strip --strip-unneeded $P/usr/local/sbin/*
strip --strip-unneeded $P/usr/local/lib/*so

# there is no perl so 
rm -rf $P/usr/local/share/snmp/snmp_perl*

# TCZ them
###########
LIST="$P $P-dev"
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
        zsyncmake $Z.tcz
done

ls -hal

echo 'Title:          net-snmp-dev.tcz  
Description:    dev files
Version:        5.9.1
Author:         https://github.com/net-snmp/net-snmp 
                at bottom of page contributors
Original-site:  http://www.net-snmp.org
Copying-policy: accompanied
Size:           192K		
Extension_by:   aus9
Tags:           network scan 
Comments:       Dev files
                
Change-log:     2020/11/08 v 5.9 on 12.x 
Current:        2023/06/17 v 5.9.1  '> $P-dev.tcz.info

echo 'net-snmp.tcz   
libtool-dev.tcz 
openssl-dev.tcz 
libnl-dev.tcz
libpci-dev.tcz  ' > $P-dev.tcz.dep

echo 'Title:          net-snmp.tcz  
Description:    Simple Network Management Protocol
Version:        5.9.1
Author:         https://github.com/net-snmp/net-snmp
                at bottom of page contributors
Original-site:  http://www.net-snmp.org
Copying-policy: accompanied
Size:           1.6M		
Extension_by:   aus9
Tags:           network scan
Comments:       protocol for monitoring the health and 
                welfare of network equipment. Net-SNMP is 
                a suite of applications used to implement 
                this protocol

                Module info http://www.net-snmp.org/docs/mibs/
 
Change-log:     2020/11/08 v 5.9 on 12.x 
Current:        2023/06/14 v 5.9.1  '> $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED' # delete duplicates TCBs below
#[libnetsnmpmibs.so.40] this TCE
#[libnetsnmpagent.so.40] "
#[libnl-3.so.200]libnl
#[libpci.so.3]libpci
#[libnetsnmp.so.40]this TCE
#[libcrypto.so.3]openssl
#[libnetsnmp.so.40]this TCE
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' # no extras

echo 'libnl.tcz
libpci.tcz
openssl.tcz' > $P.tcz.dep

submitqc --libs

rm -rf *.zsync



