#!/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 -fno-exceptions -fno-rtti"

P=apulse
V=0.1.14
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 
/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/i-rinat/$P/archive/refs/tags/v$V.tar.gz
tar xvf v$V*gz

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

# doc
#####
mkdir -p $P-doc/usr/local/share
mv $P/usr/local/share/man $P-doc/usr/local/share

# 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 . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:          apulse-doc.tcz
Description:    man page
Version:        0.1.14
Author:         Rinat Ibragimov
Original-site:  https://github.com/i-rinat/apulse
Copying-policy: accompanied
Size:           4.0K
Extension_by:   aus9  @ linuxquestions.org
Tags:           alsa 
Comments:       licence in main TCE

Change-log:     2020/07/27 Original 0.1.13 
Current:        2026/10/22 v 0.1.14 on 17x ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo "Title:	          apulse.tcz
Description:    May allow apps run without pulseaudio
Version:        0.1.14
Author:         Rinat Ibragimov 
Original-site:  https://github.com/i-rinat/apulse
Copying-policy: accompanied
Size:           48K
Extension-by:   aus9 @ linuxquestions.org
Tags:           alsa
Comments:       Designed to allow (mainly) firefox to run without pulseaudio. 
                Test that  alsa is working correctly before trying apulse stuff. 
                No alsa deps set.
                $ tce-load -i alsa alsa-utils
                $ speaker-test -D hw:Headphones
                $ speaker-test # for HDMI or analog (it depends as below)
                Optional adjust alsamixer levels
                       
                If you use analog sound....eg headphones or non-bluetooth speakers etc 
                and you can not hear sound then you may need to swap sound to the analog device.
                FOR INTEL sound only I do this....add below line to grub.cfg
                blacklist=snd-hda-intel
                Next run  $ echo '#!/bin/sh
                sudo modprobe snd-hda-intel options enable=0,1
                sleep 2
                alsactl restore -f /home/tc/asound.state ' > ~/.local/bin/snd
                
                Create  ~/.X.d/my-sound (no shebang needed)
                /home/tc/.local/bin/snd &
                Reboot to run $ aplay -l to see only analog device 
                You are likely need to set and save your mixer levels
                $ alsactl store -f /home/tc/asound.state

                So for most people with intel devices....the boot config disabled
                the intel sound module, loading into a X desktop, the my-sound file
                is executed....which loads the intel sound module but disabled the 
                first detected sound device (normally hdmi) so the next sound device,
                analog gets to be the first device and mixer levels set. 
                Obviously this assumes aplay -l has hdmi listed first. 
                So not all users can follow this method but I believe most can. YMMV
                
                Finally test with $ apulse firefox https://youtu.be/enuOArEfqGo

Change-log:     2020/07/27 Original 0.1.13
                2020/10/06 adjust info file
Current:        2026/10/22 v 0.1.14 on 17x " > $P.tcz.info

readelf -d $P/usr/local/lib/$P/* | 'NEEDED' # ALL MENTIONS OF LIBPULSE are this TCE
# libglib-2.0.so.0 glib2
# libasound.so.2 libasound....but we need sound modules that comes from alsa

echo 'glib2.tcz
alsa.tcz ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync



