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

# forum post new recommendation
# So based on that info, if SSE3 or later are required, it requires a x86_64-v2 or later CPU (which I suggest should be noted in the extension's info file). SSE2 should work on all x86_64 CPUs.
# load manually sdl2-dev
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe "

P=openal125
V=1.25.0
SRC=openal-soft-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cmake pipewire-dev portaudio-dev "
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/kcat/openal-soft/archive/refs/tags/$V.tar.gz
tar xvf $V*gz
cd $SRC
cd build # dir already exists
# cmake -LAH neon is arm related
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_INSTALL_LIBDIR=/usr/local/lib -DCMAKE_BUILD_TYPE=Release \
-DALSOFT_BACKEND_JACK:BOOL=OFF -DALSOFT_BACKEND_OSS:BOOL=OFF -DALSOFT_BACKEND_PIPEWIRE:BOOL=ON \
-DALSOFT_BACKEND_PORTAUDIO:BOOL=ON -DALSOFT_BACKEND_SDL2:BOOL=ON -DALSOFT_BACKEND_SOLARIS:BOOL=OFF \
-DALSOFT_CPUEXT_NEON:BOOL=OFF -DALSOFT_RTKIT:BOOL=OFF -DALSOFT_UPDATE_BUILD_VERSION:BOOL=OFF \
-DALSOFT_CPUEXT_SSE3:BOOL=OFF -DALSOFT_CPUEXT_SSE4_1:BOOL=OFF ../

# Building OpenAL with support for the following backends: PipeWire PulseAudio ALSA PortAudio SDL2 WaveFile Null
# Building with support for CPU extensions: SSE, SSE2
# Embedding HRTF datasets Installing AmbDec presets
# Configuring done (4.1s) -- Generating done (0.1s)

cmake --build . --parallel 6 # 59 seconds
DESTDIR=/tmp/$P make install
cd /tmp

# dev
#####
mkdir -p $P-dev/usr/local/lib
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/cmake $P-dev/usr/local/lib
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib


# main
######
mkdir -p $P/usr/local/share/doc/$P 
echo 'LGPL V 2' > $P/usr/local/share/doc/$P/COPYING
strip --strip-unneeded $P/usr/local/lib/*
strip --strip-unneeded $P/usr/local/bin/*
# fix files with empty space
mv $P/usr/local/share/openal/hrtf/Default*  $P/usr/local/share/openal/hrtf/Default-HRTF.mhr

# TCZ them
#######
LIST="$P $P-dev "
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:          openal125-dev.tcz
Description:    dev files 
Version:        1.25.0
Author:         kcat
Original-site:  https://github.com/kcat/openal-soft
Copying-policy: LGPL v 2
Size:           44K
Extension_by:   aus9 
Tags:           game audio
Comments:       Manually load sdl2-dev please
                May conflicts with openal-dev
                
Change-log:     2025/12/31 v 1.25.0 on 16x
Current:        2026/01/02 rebuilt to support SSE & SSE2 only credits CNK forum post   '  > $P-dev.tcz.info
echo 'openal125.tcz
portaudio-dev.tcz
pulseaudio-dev.tcz ' > $P-dev.tcz.dep

echo 'Title:          openal125.tcz
Description:    software implementation OpenAL 3D audio API. 
Version:        1.25.0
Author:         kcat
Original-site:  https://github.com/kcat/openal-soft
Copying-policy: LGPL v 2 
Size:           1.6M
Extension_by:   aus9 
Tags:           game audio
Comments:       alsa pipewire  pulseaudio need setup
                portaudio nor sdl2 need setup
                Test you have sound and mixer levels are
                correct before trying out this TCE please

                May conflict with openal
                If you need  /dev/snd/seq
                $ sudo modprobe snd-seq-midi

                If you are not sure how sound works in uzdoom
                correct midi player = OPL Synth Emulation
                then click advanced and click restart sound
                
Change-log:     2025/12/31 v 1.25.0 on 16x
Current:        2026/01/02 rebuilt to support SSE & SSE2 only credits CNK forum post  '  > $P.tcz.info
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED'
# libSDL2-2.0.so.0 sdl2
readelf -d $P/usr/local/bin/* | grep 'NEEDED'
# libopenal.so.1] this TCE
# libsndfile.so.1] libsndfile

echo 'sdl2.tcz
libsndfile.tcz ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync


