#!/bin/sh

DATE=`date '+%Y/%m/%d'`

PROGRAM="lldpd"
VERSION="1.0.19"

OPTIMIZE="-Os"
SYMBOLS="-g"

GDEBUG="No"
# Uncomment the next line to compile a version that can be run under gdb.
#GDEBUG="Debug"

if [ "$GDEBUG" == "Debug" ]
then
	OPTIMIZE="-O0 -ggdb"
fi


PROCESSOR_TYPE=`uname -m`
echo "$PROCESSOR_TYPE detected."

case "$PROCESSOR_TYPE" in
        i686)
        CFLAGS="-flto -fuse-linker-plugin -march=i486 -mtune=i686 $OPTIMIZE $SYMBOLS $DEFINES -pipe -Wall -Wextra -fno-plt"
        CXXFLAGS="-flto -fuse-linker-plugin -march=i486 -mtune=i686 $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-exceptions -fno-rtti"
        LDFLAGS="-Wl,-T/usr/local/lib/ldscripts/elf_i386.xbn"
        ;;

        x86_64)
        CFLAGS="-flto -fuse-linker-plugin -mtune=generic $OPTIMIZE $SYMBOLS $DEFINES -pipe -Wall -Wextra -fno-plt"
        CXXFLAGS="-flto -fuse-linker-plugin -mtune=generic $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-exceptions -fno-rtti"
        LDFLAGS="-Wl,-T/usr/local/lib/ldscripts/elf_x86_64.xbn"
        ;;

        armv*)
        CFLAGS="-march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp $OPTIMIZE $SYMBOLS $DEFINES -pipe -Wall -Wextra"
        CXXFLAGS="-march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-exceptions -fno-rtti"
        LDFLAGS="-Wl,-O1"
        ;;

        aarch64)
        CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 $OPTIMIZE $SYMBOLS $DEFINES -pipe -Wall -Wextra"
        CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 $OPTIMIZE $SYMBOLS -pipe -Wall -Wextra -fno-exceptions -fno-rtti"
        LDFLAGS="-Wl,-O1"
        ;;

        *)
        echo "$PROCESSOR_TYPE: Unknown processor type. Please add an entry for it in this script."
        exit
        ;;
esac

export CFLAGS CXXFLAGS LDFLAGS

CUR=$(pwd)
clear

TOOLCHAIN="compiletc bc squashfs-tools file submitqc"
BUILD_DEPS="libcap-dev readline-dev libxml2-dev net-snmp-dev"
PACKAGE_DEPS="libcap readline libxml2 net-snmp"

# Download packages required for compiling, filter out success messages.
tce-load -w "$TOOLCHAIN $BUILD_DEPS" 2>&1 | grep -v "already downloaded"

# Install packages required for compiling, filter out success messages.
tce-load -i "$TOOLCHAIN $BUILD_DEPS" 2>&1 | grep -v "already installed"

[ -d "$PROGRAM"_all ] && rm -rf "$PROGRAM"_*
[ -d "$PROCESSOR_TYPE" ] && rm -rf "$PROCESSOR_TYPE"
[ ! -f "$PROGRAM"-"$VERSION".tar.gz ] && wget https://media.luffy.cx/files/"$PROGRAM"/"$PROGRAM"-"$VERSION".tar.gz
[ ! -d "$PROGRAM"-"$VERSION" ] && tar -zxf "$PROGRAM"-"$VERSION".tar.gz

cd "$PROGRAM"-"$VERSION"; mkdir build; cd build
[ ! -f ../.configured ] && ../configure --prefix=/usr/local --localstatedir=/var --sysconfdir=/usr/local/etc --with-embedded-libevent --with-xml --with-snmp && touch ../.configured
make -j 9
make DESTDIR=/tmp/"$PROGRAM" install
mv /tmp/"$PROGRAM" $CUR/"$PROGRAM"_all

# Strip programs and libraries
if [ "$GDEBUG" == "No" ]
then
	cd $CUR/"$PROGRAM"_all
	sudo find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-all 2> /dev/null
	sudo find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	cd $CUR
fi

mkdir -p $CUR/"$PROGRAM"_ext/usr/local \
         $CUR/"$PROGRAM"_doc/usr/local/share \
         $CUR/"$PROGRAM"_dev/usr/local/lib

# Separate files for different extensions.
# DEV
mv $CUR/"$PROGRAM"_all/usr/local/include $CUR/"$PROGRAM"_dev/usr/local/
mv $CUR/"$PROGRAM"_all/usr/local/lib/pkgconfig $CUR/"$PROGRAM"_dev/usr/local/lib/
mv $CUR/"$PROGRAM"_all/usr/local/lib/*.a $CUR/"$PROGRAM"_dev/usr/local/lib/
# .la files are no longer used.
rm -rf $CUR/"$PROGRAM"_all/usr/local/lib/*.la

# DOC
mv $CUR/"$PROGRAM"_all/usr/local/share/man $CUR/"$PROGRAM"_doc/usr/local/share/

# EXT
mv $CUR/"$PROGRAM"_all/usr/local/* $CUR/"$PROGRAM"_ext/usr/local/
mkdir -p $CUR/"$PROGRAM"_ext/usr/local/bin \
         $CUR/"$PROGRAM"_ext/usr/local/etc/init.d
cp -a lldpdAddUser $CUR/"$PROGRAM"_ext/usr/local/bin/
cp -a lldpdBackupUser $CUR/"$PROGRAM"_ext/usr/local/bin/
cp -a lldpd.initd $CUR/"$PROGRAM"_ext/usr/local/etc/init.d/lldpd

# Create TCZs

#---------------- Package program ---------------------#
# Create .tcz, .md5.txt, and .list files for program.
mksquashfs "$PROGRAM"_ext "$PROGRAM".tcz -noappend >/dev/null 2>&1
md5sum $PROGRAM.tcz > $PROGRAM.tcz.md5.txt
cd "$PROGRAM"_ext
# Find files including their path starting from the current directory,
# cut the leading dot, sort it, and save it to the .list file.
find . -not -type d | cut -c 2- | sort > ../$PROGRAM.tcz.list
cd ..

# Create dependency file.
# Set dependency file to zero length.
> "$PROGRAM".tcz.dep

for EXT in $PACKAGE_DEPS
do
	echo "$EXT".tcz >> "$PROGRAM".tcz.dep
done

# Create .info file for program.
VERSION="$VERSION"
SIZE="`du -h $PROGRAM.tcz | cut -f1`"
echo "Title:		$PROGRAM.tcz
Description:	Link Layer Discovery Protocol reception and sending.
Version:	$VERSION
Author:		Vincent Bernat, Pierre-Yves Ritschard
Original-site:	https://lldpd.github.io
Copying-policy:	ISC license
Size:		$SIZE
Extension_by:	CentralWare, Rich
Tags:		Link Layer Discovery Protocol CDP FDP SONMP EDP
Comments:	LLDP is an industry standard protocol designed to supplant
		proprietary Link-Layer protocols such as EDP or CDP. The
		goal of LLDP is to provide an inter-vendor compatible
		mechanism to deliver Link-Layer notifications to adjacent
		network devices.

		lldpd supports bridge, vlan and bonding.

		_lldpd needs to be added to users and groups to run. The
		lldpdAddUser script checks for user and group _lldpd and adds
		it if not present. The script can be run as a regular user.

		If you want to add the updated user/group related files to your
		/opt/.filetool.lst file, run lldpdBackupUser. The script checks
		if the files are already present before adding them. You must
		then initiate the actual backup yourself.

		To start lldpd:
				sudo /usr/local/etc/init.d/lldpd start

		Built with:
		CFLAGS=$CFLAGS
		LDFLAGS=$LDFLAGS

Change-log:	2024/08/14 Original release 1.0.18
Current:	$DATE $VERSION, added lldpdAddUser, lldpdBackupUser, and init.d scripts
" > $PROGRAM.tcz.info
#---------------- End package program -----------------#


#---------------- Package program-dev -----------------#
# Create .tcz, .md5.txt, and .list files for program.
mksquashfs "$PROGRAM"_dev "$PROGRAM"-dev.tcz -noappend >/dev/null 2>&1
md5sum $PROGRAM-dev.tcz > $PROGRAM-dev.tcz.md5.txt
cd "$PROGRAM"_dev
# Find files including their path starting from the current directory,
# cut the leading dot, sort it, and save it to the .list file.
find . -not -type d | cut -c 2- | sort > ../$PROGRAM-dev.tcz.list
cd ..

# Create dependency file.
echo "$PROGRAM".tcz > "$PROGRAM"-dev.tcz.dep

# Create .info file for program.
VERSION="$VERSION"
SIZE="`du -h $PROGRAM-dev.tcz | cut -f1`"
echo "Title:		$PROGRAM-dev.tcz
Description:	Link Layer Discovery Protocol reception and sending.
Version:	$VERSION
Author:		Vincent Bernat, Pierre-Yves Ritschard
Original-site:	https://lldpd.github.io
Copying-policy:	ISC license
Size:		$SIZE
Extension_by:	CentralWare, Rich
Tags:		Link Layer Discovery Protocol CDP FDP SONMP EDP
Comments:	LLDP is an industry standard protocol designed to supplant
		proprietary Link-Layer protocols such as EDP or CDP. The
		goal of LLDP is to provide an inter-vendor compatible
		mechanism to deliver Link-Layer notifications to adjacent
		network devices.

		lldpd supports bridge, vlan and bonding.

		Built with:
		CFLAGS=$CFLAGS
		LDFLAGS=$LDFLAGS

Change-log:	$DATE Original release $VERSION
Current:	$DATE Original release $VERSION
" > $PROGRAM-dev.tcz.info
#---------------- End package program-dev -------------#


#---------------- Package program-doc -----------------#
# Create .tcz, .md5.txt, and .list files for program.
mksquashfs "$PROGRAM"_doc "$PROGRAM"-doc.tcz -noappend >/dev/null 2>&1
md5sum $PROGRAM-doc.tcz > $PROGRAM-doc.tcz.md5.txt
cd "$PROGRAM"_doc
# Find files including their path starting from the current directory,
# cut the leading dot, sort it, and save it to the .list file.
find . -not -type d | cut -c 2- | sort > ../$PROGRAM-doc.tcz.list
cd ..

# Create .info file for program.
VERSION="$VERSION"
SIZE="`du -h $PROGRAM-doc.tcz | cut -f1`"
echo "Title:		$PROGRAM-doc.tcz
Description:	Link Layer Discovery Protocol reception and sending.
Version:	$VERSION
Author:		Vincent Bernat, Pierre-Yves Ritschard
Original-site:	https://lldpd.github.io
Copying-policy:	ISC license
Size:		$SIZE
Extension_by:	CentralWare, Rich
Tags:		Link Layer Discovery Protocol CDP FDP SONMP EDP
Comments:	LLDP is an industry standard protocol designed to supplant
		proprietary Link-Layer protocols such as EDP or CDP. The
		goal of LLDP is to provide an inter-vendor compatible
		mechanism to deliver Link-Layer notifications to adjacent
		network devices.

		lldpd supports bridge, vlan and bonding.

		Built with:
		CFLAGS=$CFLAGS
		LDFLAGS=$LDFLAGS

Change-log:	$DATE Original release $VERSION
Current:	$DATE Original release $VERSION
" > $PROGRAM-doc.tcz.info
#---------------- End package program-doc -------------#



# Check the newly created extension files for errors.
submitqc --libs 2>&1 > submitqc.txt

# Create archive of the packages.
busybox tar -czf "$PROGRAM".tar.gz *.tcz*

# Create archive of the source package, this build script, the init.d script, and 2 helper scripts.
busybox tar -czf "$PROGRAM"-source-v"$VERSION".tar.gz "$PROGRAM-$VERSION.tar.gz" "$0" lldpd.initd lldpdAddUser lldpdBackupUser

# Tar the package archive and build script. Include processor type as part of archive name.
busybox tar -czf "$PROGRAM-$PROCESSOR_TYPE".tar.gz "$PROGRAM".tar.gz "$PROGRAM"-source-v"$VERSION".tar.gz

# bcrypt the final file with the password  tinycore
yes tinycore | bcrypt "$PROGRAM-$PROCESSOR_TYPE".tar.gz

# Create directory to archive last build for current architecture.
mkdir "$PROCESSOR_TYPE"

# Save copies in the  $PROCESSOR_TYPE  directory in case we want
# to compile for another architecture.
# Save a copy of the extension files.
cp *.tcz* "$PROCESSOR_TYPE"
# Save a copy of all tarballs.
cp *.tar.gz* "$PROCESSOR_TYPE"
# Save a copy of the submitqc report.
cp submitqc.txt "$PROCESSOR_TYPE"

echo "Check $PACKAGEDIR/submitqc.txt for any errors."
echo "Send $PROGRAM-$PROCESSOR_TYPE.tar.gz.bfe to: tcesubmit@gmail.com"
