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

# cheated off http://tinycorelinux.net/14.x/aarch64/tcz/src/libopenaptx/libopenaptx.build
export CFLAGS="-mtune=generic -Os -pipe" 
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti "

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

cd /tmp 
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/pali/$P/archive/refs/tags/$V.tar.gz  " $USER 
tar xvf $V*gz
cd $SRC 
sed 's/CFLAGS =/CFLAGS +=/' -i Makefile
sed 's/-O3//' -i Makefile
export CC=gcc
make -j4 # seconds
make install DESTDIR=/tmp/$P
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/
mv $P/usr/local/lib/*.a $P-dev/usr/local/lib/
# submitqc says perm issues 664
chmod 644 $P-dev/usr/local/include/openaptx.h 

# main
######
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v3' > $P/usr/local/share/doc/$P/COPYING

# already stripped

# TCZ them
###########
LIST2="$P $P-dev "
for Z in $LIST2
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
done

ls -hal

echo 'Title:          libopenaptx-dev.tcz
Description:    dev files  
Version:        0.2.1
Author:         Pali Rohár    
Original-site:  https://github.com/pali/libopenaptx
Copying-policy: GPL v3
Size:           16K           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio alsa bluetooth codec
Comments:       Development

Change-log:     2024/06/21 v 0.2.1 on 15x
Current:        2024/06/21    ' > $P-dev.tcz.info
echo 'libopenaptx.tcz' > $P-dev.tcz.dep

echo 'Title:          libopenaptx.tcz
Description:    Audio Processing Technology codec  
Version:        0.2.1
Author:         Pali Rohár    
Original-site:  https://github.com/pali/libopenaptx
Copying-policy: GPL v3
Size:           20K           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio alsa bluetooth codec
Comments:       Open Source implementation of Audio Processing 
                Technology codec (aptX) 

                See website above for codec conversion examples

Change-log:     2024/06/21 v 0.2.1 on 15x
Current:        2024/06/21    ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED' # ignore TCBS
#[libopenaptx.so.0] this TCE 
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' # no extras

# no dep

submitqc --libs
rm -rf *.zsync


