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

# slackware use v 3.4.2 not 3.5 series...
# extra packages added after portaudio to see if they make a diff to compile errors
export CFLAGS="-mtune=generic -Os -pipe -DNDEBUG"
export CXXFLAGS="-mtune=generic -Os -pipe -DNDEBUG -fabi-version=13   "

P1=Audacity
P=audacity
V=3.4.2
SRC=$P-$P1-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cmake wxwidgets-dev ffmpeg5-dev libid3tag-dev libmad-dev opusfile-dev \
rapidjson-dev  qt-5.x-dev mpg123-dev wavpack-dev portmidi-dev portaudio-dev nasm wv-dev  "
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/$P/$P/archive/refs/tags/$P1-$V.tar.gz"  $USER
tar xvf $P1*gz
cd $SRC
cmake -B build -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=Release -DAUDACITY_BUILD_LEVEL=2 \
-DCMAKE_CXX_STANDARD=11 -Daudacity_conan_enabled=Off -Daudacity_has_tests=Off -Daudacity_has_vst3=Off \
-Daudacity_use_vst:BOOL=Off -Daudacity_has_crashreports=Off -Daudacity_has_sentry_reporting=Off \
-Daudacity_has_updates_check=Off  

# Using system version of ZLIB 
# Using system version of PNG 
# Using system version of JPEG 
# Using system version of EXPAT 
# Using system version of wxWidgets 
# Using system version of libmp3lame 
# Using system version of mpg123 
# Using system version of libid3tag 
# Using system version of WavPack 
# Using system version of Ogg 
# Using system version of FLAC 
# Using system version of Opus 
# Using system version of opusfile 
# Using system version of Vorbis 
# Using system version of SndFile 
# Using system version of PortAudio 
# Using system version of RapidJSON 
# Using system version of PortMidi

cmake --build build # 20M
cd build
make -j5 # seconds
make install DESTDIR=/tmp/$P # -strip fails
cd /tmp

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

# locale
#########
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# main
#####
strip --strip-unneeded $P/usr/local/bin/$P
strip --strip-unneeded $P/usr/local/lib/$P/*.so
strip --strip-unneeded $P/usr/local/lib/$P/modules/*.so
rm -rf $P/usr/local/share/doc/$P/README.md

# install script
################
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
gtk-update-icon-cache -q -f -t /usr/local/share/icons/hicolor
update-mime-database /usr/local/share/mime > /dev/null 2>&1
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# desktop - icon
################
mkdir -p $P/usr/local/share/pixmaps
ICON=$P/usr/local/share/icons/hicolor/48x48/audacity.png
cp $ICON $P/usr/local/share/pixmaps/
echo 'X-FullPathIcon=/usr/local/share/pixmaps/audacity.png' >> $P/usr/local/share/applications/$P.desktop
# there are xpm pixmaps already here left as repo has not deleted them


# TCZ them
###########
LIST2="$P $P-doc $P-locale "
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:          audacity-doc.tcz
Description:    man page 
Version:        3.4.2
Author:         audacity Team        
Original-site:  https://github.com/audacity/audacity
Copying-policy: GPL v3
Size:           4.0K          
Extension_by:   aus9 @linuxquestions.org
Tags:           audio editor gtk+ gtk3
Comments:       -
                
Change-log:     2024/05/12 v 3.4.2 on 15x 
Current:        2024/05/12                   ' > $P-doc.tcz.info
echo 'man-db.tcz                             ' > $P-doc.tcz.dep        

echo 'Title:          audacity-locale.tcz
Description:    language files  
Version:        3.4.2
Author:         audacity Team         
Original-site:  https://github.com/audacity/audacity
Copying-policy: GPL v3
Size:           5.8M           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio editor gtk+ gtk3
Comments:       locales
                
Change-log:     2024/05/12 v 3.4.2 on 15x (aus9)  
Current:        2024/05/12                  ' > $P-locale.tcz.info
echo 'audacity.tcz                          ' > $P-locale.tcz.dep

echo 'Title:          audacity.tcz
Description:    gtk3 alsa audio editor  
Version:        3.4.2
Author:         audacity Team         
Original-site:  https://github.com/audacity/audacity
Copying-policy: GPL v3
Size:           13M           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio editor gtk+ gtk3
Comments:       multi-track audio editor and recorder
                Alsa sound needs to be setup first.
                Clicking the Audio setup may work for 
                some members for output/input choices but does
                not change volume settings

                To mute or unmute sound in alsamixer
                use arrow key to get to relevant control
                Press m key to toggle off or on each control

                Press page up or arrow up to raise volumes etc 
                Some controls that do not use m, use up/down arrows

                To activate mic in alsamixer, Press F4 & use arrow key 
                to get to relevant Capture (mic) control
                Press space bar to toggle on or off each mic control as a tower
                often has front and rear inputs. Laptops should be simple.
                Raise volumes similar to outputs

                Read alsa info on saving settings over reboot

                mic image may be useful for some https://i.postimg.cc/hG45jCSH/mic1.png   
                
Change-log:     2019/05/03 v 2.3.1 (Juanito)
                2019/10/03 v 2.3.2
                2020/07/28 adjusted desktop file
                2021/02/21 v 2.4.2
Current:        2024/05/12 v 3.4.2 on 15x (aus9)                  ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED'
#lib-theme-resources.so]lib-graphics.so]lib-project-file-io.so]lib-wx-init.so]......................this TCE
#lib-time-track.so]lib-snapping.so]lib-audio-io.so]lib-import-export.so]lib-export-ui.so]
#lib-file-formats.so]lib-lv2.so]lib-ladspa.so]lib-tags.so]lib-shuttlegui.so]lib-theme.so]
#lib-wx-wrappers.so]lib-effects.so]lib-wave-track.so]lib-stretching-sequence.so]
#lib-time-and-pitch.so]lib-sample-track.so]lib-mixer.so]lib-audio-graph.so]lib-playable-track.so]
#lib-track-selection.so]lib-track.so]lib-time-frequency-selection.so]lib-screen-geometry.so]
#lib-command-parameters.so]lib-numeric-formats.so]lib-project-rate.so]lib-audio-devices.so]
#lib-realtime-effects.so]lib-project-history.so]lib-project.so]lib-transactions.so]
#lib-math.so]lib-channel.so]lib-registries.so]lib-module-manager.so]lib-xml.so]
#lib-string-utils.so]lib-files.so]lib-preferences.so]lib-components.so]lib-exceptions.so]
#lib-basic-ui.so]lib-strings.so]lib-ipc.so]lib-utility.so]
# ^ all this TCE

#libatomic.so.1] gcc_libs.tcz
#libgtk-3.so.0]#libgdk-3.so.0]gtk3 -> dep of wxwidgets 
#libpangocairo-1.0.so.0]libpango-1.0.so.0]pango -> gtk3 -> dep wxwidgets
#libharfbuzz.so.0]harfbuzz -> freetype -> fontconfig -> cairo -> pango -> gtk3 dep wxwidgets
#libatk-1.0.so.0]at-spi2-core -> gtk3 -> dep wxwidgets
#libcairo-gobject.so.2]#libcairo.so.2]cairo -> pango -> gtk3-> wxwidgets
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2 -> gtk3 -> wxwidgets
#libgio-2.0.so.0]libgobject-2.0.so.0]libglib-2.0.so.0] glib2 -> cairo -> pango -> gtk3 -> wxwidgets
#libasound.so.2]libasound -> portaudio (below)
#libwx_baseu-3.2.so.0]libwx_gtk3u_core-3.2.so.0]libwx_gtk3u_html-3.2.so.0]libwx_gtk3u_qa-3.2.so.0] continued next line
#libwx_baseu_xml-3.2.so.0]libwx_baseu_net-3.2.so.0]wxwidgets

#libportmidi.so.2]portmidi
#libsndfile.so.1]libsndfile
#libportaudio.so.2]portaudio
readelf -d $P/usr/local/lib/$P/*.so | grep 'NEEDED' # dupes
readelf -d  $P/usr/local/lib/$P/modules/*.so | grep 'NEEDED' 
#[libFLAC.so.8]libFLAC++.so.6]flac -> libsndfile
#libogg.so.0]libogg -> flac -> libsndfile
#libid3tag.so.0]libid3tag
#libmp3lame.so.0]lame
#libmpg123.so.0]mpg123
#[libvorbis.so.0][libvorbisenc.so.2]libvorbis -> libsndfile
#[libopus.so.0]opus -> opusfile
#[libopusfile.so.0]opusfile

# qt-5.x-base.tcz wavpack.tcz are these added by submitqc?

echo 'wxwidgets.tcz
gcc_libs.tcz
portaudio.tcz
portmidi.tcz
libsndfile.tcz
libid3tag.tcz
lame.tcz
mpg123.tcz
opusfile.tcz
wavpack.tcz
gtk-update-icon-cache.tcz
shared-mime-info.tcz           ' > $P.tcz.dep

# pass 1 added qt-5.x-base.tcz & wavpack   lets try without
# launch error = Unable to load the "mod-wavpack" module-> qt not required!!
 
submitqc --libs
rm -rf *.zsync
