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

# MANUALLY LOAD sdl2-dev
# fluid_dls.cpp:77:35: error: cannot use ‘typeid’ with ‘-fno-rtti
# fluid_dls.cpp:98:33: error: exception handling disabled, use ‘-fexceptions’ to enable
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fexceptions "

P=fluidsynth
V=2.5.2
SRC=fluidsynth-2.5.2
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc git cmake ninja python3.9 mpg123-dev opus-dev pulseaudio-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/FluidSynth/$P/archive/refs/tags/v$V.tar.gz
tar xvf v*gz
cd $SRC
mkdir build && cd build  # cmake -LAH
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_INSTALL_LIBDIR=/usr/local/lib -DCMAKE_BUILD_TYPE=Release \
-DFLUID_DAEMON_ENV_FILE=/usr/local/etc/fluidsynth -Denable-portaudio=ON -Denable-dbus:BOOL=OFF \
-Denable-oss:BOOL=OFF -Denable-network:BOOL=OFF -Denable-ipv6:BOOL=OFF ../

# Build Summary:FluidSynth Version:2.5.2 Library version:3.5.1 Git revision: not a git working copy
# Build type:            Release  Install Prefix:        /usr/local
# Audio / MIDI driver support:
#  ALSA:                  yes
#  CoreAudio:             no
#  CoreMIDI:              no
#  DSound:                no
#  JACK:                  no
#  MidiShare:             no
#  Oboe:                  no
#  OpenSLES:              no
#  OS/2 DART:             no
#  OS/2 KAI:              no
#  OSS:                   no
#  PipeWire:              yes
#  PortAudio:             yes
#  PulseAudio:            yes
#  SDL3:                  no
#  WASAPI:                no
#  WaveOut:               no
#  WinMidi:               no
# Support for SF3 files:  yes
# Support for DLS files:  yes
#  native:                yes
#  libinstpatch:          no
# Audio to file rendering:yes
#  libsndfile:            yes
# Miscellaneous support:
#  D-Bus:                 no
#  LADSPA support:        no
#  NETWORK Support:       no
#    IPV6 Support:        no
#  Readline:              yes (NOTE: GPL library)
#  systemd:               no
#  getopt:                yes
# Developer nerds info:
#  OS abstraction:        glib
#  Samples type:          double
#  Multithread rendering: yes
#  OpenMP 4.0:            no
#  Profiling:             no
#  Debug Build:           no
#  Trap on FPE (debug):   no
#  Check FPE (debug):     no
#  UBSan (debug):         no
#  Coverage:              no
# Configuring done (6.3s) -- Generating done (0.3s)

cmake --build . --parallel 6 # 7s
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/pkgconfig $P-dev/usr/local/lib
mv $P/usr/local/lib/cmake $P-dev/usr/local/lib

# 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 
echo 'LGPL v 2.1 '  > $P/usr/local/share/doc/$P/LICENSE
strip --strip-unneeded $P/usr/local/bin/$P
strip --strip-unneeded $P/usr/local/lib/*1

# soundfont
############
mkdir -p $P-sf/usr/local/share/soundfonts $P-sf/usr/local/share/doc/$P-sf
cp $SRC/sf2/COPYRIGHT.txt $P-sf/usr/local/share/doc/$P-sf
cp $SRC/sf2/VintageDreamsWaves-v2.sf2 $P-sf/usr/local/share/soundfonts
cd $P-sf/usr/local/share/soundfonts
ln -s VintageDreamsWaves-v2.sf2 default.sf2
cd /tmp

# TCZ them
#######
LIST="$P $P-doc $P-dev $P-sf "
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:          fluidsynth-dev.tcz
Description:    dev files 
Version:        2.5.2
Author:         Tom Moebert
Original-site:  https://github.com/FluidSynth/fluidsynth
Copying-policy: accompanied
Size:           48K
Extension_by:   aus9 
Tags:           soundfont midi 
Comments:       Manually load sdl2-dev  please
                                
Change-log:     2025/12/31 v 2.5.2  on 16x
Current:        2025/12/31  '  > $P-dev.tcz.info
echo 'mpg123-dev.tcz 
opus-dev.tcz 
pulseaudio-dev.tcz  ' > $P-dev.tcz.dep

echo 'Title:          fluidsynth-doc.tcz
Description:    man page 
Version:        2.5.2
Author:         Tom Moebert
Original-site:  https://github.com/FluidSynth/fluidsynth
Copying-policy: accompanied
Size:           8.0K
Extension_by:   aus9 
Tags:           soundfont midi 
Comments:       for an easier read of some setting see
https://github-wiki-see.page/m/FluidSynth/fluidsynth/wiki/LowLatency
                                
Change-log:     2025/12/31 v 2.5.2  on 16x
Current:        2025/12/31  '  > $P-doc.tcz.info
echo 'man-db.tcz ' > $P-doc.tcz.dep

echo 'Title:          fluidsynth-sf.tcz
Description:    soundfont from fluidsynth src 
Version:        2.5.2
Author:         Ian Wilson
Original-site:  https://github.com/FluidSynth/fluidsynth
Copying-policy: freeware
Size:           256K
Extension_by:   aus9 
Tags:           soundfont  
Comments:       soundfont that was in the source of fluidsynth
                made by Ian Wilson
                path= /usr/local/share/soundfonts/default.sf2
                which is a hard link to 
                VintageDreamsWaves-v2.sf2
                                
Change-log:     2025/12/31 v 2.5.2  on 16x
Current:        2025/12/31  '  > $P-sf.tcz.info

echo 'Title:          fluidsynth.tcz
Description:    Software synthesizer based on SoundFont2 specs 
Version:        2.5.2
Author:         Tom Moebert
Original-site:  https://github.com/FluidSynth/fluidsynth
Copying-policy: accompanied
Size:           328K
Extension_by:   aus9 
Tags:           soundfont midi 
Comments:       TCE generates audio by reading and handling MIDI events 
                from MIDI input devices by using a SoundFont. It is the 
                software analogue of a MIDI synthesizer.
                Supports sf2 sf3 dls files 
                Alsa pulseaudio & pipewire need setup first

                Can play MIDI files. First run
                $ sudo modprobe snd-seq-midi
                Tested alsa only but supports alsa file pipewire pulseaudio & 
                portaudio so next edit to suit
$ fluidsynth /usr/local/share/soundfonts/default.sf2 /path2/mid.mid

                While its runs- it runs in a daemon mode and accepts commands like help
                settings
                                
Change-log:     2025/12/31 v 2.5.2  on 16x
Current:        2025/12/31  '  > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED'
# libfluidsynth.so.3   this TCE
# libglib-2.0.so.0 glib2 pipewire
# libpipewire-0.3.so.0 pipewire 
readelf -d $P/usr/local/lib/*1 | grep 'NEEDED'
#libgthread-2.0.so.0]glib2 pipewire
# libsndfile.so.1] libsndfile pipewire
# libpulse-simple.so.0]libpulse.so.0] libpulseaudio pipewire
# libasound.so.2] libasound pipewire
# libportaudio.so.2] portaudio
# libreadline.so.8] readline pipewire
echo 'fluidsynth-sf.tcz 
pipewire.tcz
portaudio.tcz   ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

