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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=faad2
V=2.11.2
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cmake "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/knik0/faad2/archive/refs/tags/$V.tar.gz \
https://raw.githubusercontent.com/void-linux/void-packages/refs/heads/master/srcpkgs/faad2/patches/fix-stack_usage.patch
tar xvf $V*gz
cd $SRC
patch -p1 < ../fix-stack_usage.patch
cmake -B build -DCMAKE_BUILD_TYPE=None -DCMAKE_INSTALL_PREFIX=/usr/local # acts like configure
cmake --build build # 10 seconds
cd build && cmake --install . # note the DOT
# Installing: /usr/local/lib/pkgconfig/faad2.pc
# Installing: /usr/local/lib/libfaad.so.2.11.2
# Installing: /usr/local/lib/libfaad.so.2
# Installing: /usr/local/lib/libfaad.so
# Installing: /usr/local/lib/libfaad_drm.so.2.11.2
# Installing: /usr/local/lib/libfaad_drm.so.2
# Installing: /usr/local/lib/libfaad_drm.so
# Installing: /usr/local/bin/faad
# Installing: /usr/local/include/faad.h
# Installing: /usr/local/include/neaacdec.h
# Installing: /usr/local/share/man/man1/faad.1
cd /tmp

# dev
#######
mkdir -p $P-dev/usr/local/lib/pkgconfig $P-dev/usr/local/include
mv /usr/local/lib/pkgconfig/$P.pc $P-dev/usr/local/lib/pkgconfig
cd /usr/local/include 
mv faad.h /tmp/$P-dev/usr/local/include
mv neaacdec.h /tmp/$P-dev/usr/local/include
cd /tmp

# doc
####
mkdir -p $P-doc/usr/local/share/man/man1 $P-doc/usr/local/share/doc/$P-doc
mv /usr/local/share/man/man1/faad.1 $P-doc/usr/local/share/man/man1
cp $SRC/docs/Ahead*pdf $P-doc/usr/local/share/doc/$P-doc/Ahead-AAC-Decoder-library-documentation.pdf # had gaps

# bin
######
mkdir -p $P-bin/usr/local/bin
mv /usr/local/bin/faad $P-bin/usr/local/bin
strip --strip-unneeded $P-bin/usr/local/bin/faad

# main
######
mkdir -p $P/usr/local/lib $P/usr/local/share/doc/$P
mv /usr/local/lib/libfaad* $P/usr/local/lib
strip --strip-unneeded $P/usr/local/lib/*so*
echo 'GPL v 2' > $P/usr/local/share/doc/$P/COPYING


# TCZ them
#######
LIST="$P $P-dev $P-doc $P-bin "
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:          faad2-bin.tcz
Description:    executable
Version:        2.11.2
Author:         https://github.com/knik0/faad2/blob/master/AUTHORS       
Original-site:  https://github.com/knik0/faad2
Copying-policy: GPL v 2
Size:           20K           
Extension_by:   aus9 @linuxquestions.org
Tags:           aac mpeg2 mpeg4 decoder
Comments:       Likely to conflict with faad
                I suggest read the doc TCE
                                
Change-log:     2025/11/15 v 2.11.2 on 16x
Current:        2025/11/15  ' > $P-bin.tcz.info
readelf -d $P-bin/usr/local/bin/* | grep 'NEEDED' 
# libfaad.so.2 faad2
echo 'faad2.tcz' > $P-bin.tcz.dep

echo 'Title:          faad2-dev.tcz
Description:    dev files
Version:        2.11.2
Author:         https://github.com/knik0/faad2/blob/master/AUTHORS       
Original-site:  https://github.com/knik0/faad2
Copying-policy: GPL v 2
Size:           8.0K           
Extension_by:   aus9 @linuxquestions.org
Tags:           aac mpeg2 mpeg4 decoder
Comments:       Likely to conflict with faad-dev
                                
Change-log:     2025/11/15 v 2.11.2 on 16x
Current:        2025/11/15  ' > $P-dev.tcz.info
echo 'faad2.tcz' > $P-dev.tcz.dep

echo 'Title:          faad2-doc.tcz
Description:    pdf and man page
Version:        2.11.2
Author:         https://github.com/knik0/faad2/blob/master/AUTHORS       
Original-site:  https://github.com/knik0/faad2
Copying-policy: GPL v 2
Size:           24K           
Extension_by:   aus9 @linuxquestions.org
Tags:           aac mpeg2 mpeg4 decoder
Comments:       -
                                
Change-log:     2025/11/15 v 2.11.2 on 16x
Current:        2025/11/15  ' > $P-doc.tcz.info
echo 'man-db.tcz ' > $P-doc.tcz..dep


echo 'Title:          faad2.tcz
Description:    Freeware Advanced Audio (AAC) Decoder
Version:        2.11.2
Author:         https://github.com/knik0/faad2/blob/master/AUTHORS       
Original-site:  https://github.com/knik0/faad2
Copying-policy: GPL v 2
Size:           336K           
Extension_by:   aus9 @linuxquestions.org
Tags:           aac mpeg2 mpeg4 decoder
Comments:       Likely to conflict with faad.tcz
                mpeg2/mpeg4 lossy sound decoder aka aac as used in iPod/iPhone
                but also used by some streaming sites   
                                
Change-log:     2025/11/15 v 2.11.2 on 16x
Current:        2025/11/15  ' > $P.tcz.info
readelf -d $P/usr/local/lib/*so*| grep 'NEEDED' # rootfs....NO DEP


# submitqc does not like my list for faad so ignored this build

