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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
export LDFLAGS="-Wl,-O1"

P=apulse
V=0.1.13
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc alsa-dev glib2-dev cmake"
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/i-rinat/$P/archive/v$V.zip"  $USER
unzip v$V*zip

mkdir $P
cd $SRC
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr/local  -DCMAKE_BUILD_TYPE=Release ..
make -j5 # seconds
make install DESTDIR=/tmp/$P
cd /tmp

# doc
#####
mkdir -p $P-doc/usr/local/share/doc/$P
mv $P/usr/local/share/man $P-doc/usr/local/share
cp  $SRC/LICENSE.MIT $P-doc/usr/local/share/doc/$P/LICENCE

# main
######
mkdir -p $P/usr/local/share/doc/$P
cp  $SRC/LICENSE.MIT $P/usr/local/share/doc/$P/LICENCE
cd $P/usr/local/lib/$P
strip --strip-unneeded *
cd /tmp

# TCZ them
###########
LIST2="$P $P-doc "
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:          apulse-doc.tcz
Description:    docs
Version:        0.1.13
Author:         Rinat Ibragimov
Original-site:  https://github.com/i-rinat/apulse
Copying-policy: accompanied
Size:           4.0K
Extension_by:   aus9 
Tags:           alsa 
Comments:       docs

Change-log:     2020/07/27 Original 0.1.13
Current:        2020/07/27
' > $P-doc.tcz.info

echo 'Title:	          apulse.tcz
Description:    May allow apps run without pulseaudio
Version:        0.1.13
Author:         Rinat Ibragimov 
Original-site:  https://github.com/i-rinat/apulse
Copying-policy: accompanied
Size:           48K
Extension-by:   aus9
Tags            alsa
Comments:       Designed mainly to allow firefox to run
                without pulseaudio but with alsa and have
                sound YMMV
                        
                I assume you have already set up and tested
                that alsa is working correctly without pulseaudio
                       
                formula = 
                $ apulse <exe-name> [program-parameters]

Change-log:     2020/07/27 Original 0.1.13
Current:        2020/07/27
' > $P.tcz.info

echo 'man-db.tcz' > $P-doc.tcz.dep

submitqc --libs

