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

export CFLAGS="-mtune=generic -Os -pipe -DNDEBUG"
export CXXFLAGS="-mtune=generic -Os -pipe -DNDEBUG -fabi-version=13   "

P1=Audacity
P=audacity
V=3.7.5
SRC=$P-$P1-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cmake wxwidgets-dev ffmpeg7-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

# no doc 
#####
rm -rf $P/usr/local/share/man 

# 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

# remove svg icon to reduce deps
rm -rf $P/usr/local/share/icons/hicolor/scalable/


# TCZ them
###########
LIST2="$P $P-locale "
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:          audacity-locale.tcz
Description:    language files  
Version:        3.7.5
Author:         audacity Team         
Original-site:  https://github.com/audacity/audacity
Copying-policy: GPL v3
Size:           6.2M           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio editor gtk3
Comments:       untested by me

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
                2024/05/12 v 3.4.2 on 15x (aus9)
Current:        2025/11/10 v 3.7.5 on 16x ' > $P-locale.tcz.info
echo 'audacity.tcz' > $P-locale.tcz.dep

                        
echo 'Title:          audacity.tcz
Description:    gtk3 alsa audio editor  
Version:        3.7.5
Author:         audacity Team         
Original-site:  https://github.com/audacity/audacity
Copying-policy: GPL v3
Size:           15M           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio editor gtk3
Comments:       multi-track audio editor and recorder. Alsa sound needs to be setup first.
                Read alsa info on saving settings over reboot
                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 
                mic image may be useful for some https://i.postimg.cc/hG45jCSH/mic1.png   

                libsuil_qt5_in_gtk3.so needs qt-5.x-base.tcz

                quick howto...for a RH mouse user to copy and paste a selection from 1 to 2 minutes
                click left mouse at 1 minute...inside the track below the 1 minute mark
                hold the left mouse down and move mouse to 2 minute mark
                release mouse.....copy selection by Control + c
                Pulldown File -> New
                kb Control + v to paste  ( choose smart or selected audio)
                play button to test.
                Pulldown Export audio choose filename - read-write pathway - format eg mp3
                Bearing in mind that car usb sticks can play mp3 but only certain players
                can handle other formats  YMMV
                
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
                2024/05/12 v 3.4.2 on 15x (aus9)
Current:        2025/08/21 v 3.7.5 on 16x   ' > $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 ABOVE  TCE

#libatomic.so.1] gcc_libs.tcz
#libgtk-3.so.0]#libgdk-3.so.0]gtk3 -> wxwidgets 
#libpangocairo-1.0.so.0]libpango-1.0.so.0]pango  wxwidgets
#libharfbuzz.so.0]harfbuzz  wxwidgets
#libatk-1.0.so.0]at-spi2-core wxwidgets
#libcairo-gobject.so.2]#libcairo.so.2]cairo  wxwidgets
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2  wxwidgets
#libgio-2.0.so.0]libgobject-2.0.so.0]libglib-2.0.so.0] glib2  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' 
readelf -d  $P/usr/local/lib/$P/modules/*.so | grep 'NEEDED' 
#[libFLAC.so.8]libFLAC++.so.6]flac  libsndfile
#libogg.so.0]libogg 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

readelf -d $P/usr/local/lib/audacity/libsuil_qt5_in_gtk3.so | grep 'NEEDED'.....showing qt5 only
# 0x0000000000000001 (NEEDED)             Shared library: [libQt5Widgets.so.5]
# 0x0000000000000001 (NEEDED)             Shared library: [libQt5Gui.so.5]
# 0x0000000000000001 (NEEDED)             Shared library: [libQt5Core.so.5]

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 lets try without
# $ audacity tce-load -i audacity # I already have alsa loaded
# wavpack.tcz: OK libogg.tcz: OK opus.tcz: OK opusfile.tcz: OK sdl2.tcz: OK libvorbis.tcz: OK flac.tcz: OK libsndfile.tcz: OK
#libpulseaudio.tcz: OK portaudio.tcz: OK mpg123.tcz: OK lame.tcz: OK libid3tag.tcz: OK portmidi.tcz: OK glu.tcz: OK
#pcre.tcz: OK libltdl.tcz: OK libcanberra.tcz: OK notification-daemon.tcz: OK libnotify.tcz: OK iso-codes.tcz: OK
#hunspell-en_US.tcz: OK hunspell.tcz: OK aspell.tcz: OK enchant2.tcz: OK gspell.tcz: OK Updating certificates in /usr/local/etc/ssl/certs...
#146 added, 0 removed; done. ca-certificates.tcz: OK curl.tcz: OK libnghttp2.tcz: OK libpsl.tcz: OK sqlite3.tcz: OK
# glib-networking.tcz: OK libsoup3.tcz: OK liblcms2.tcz: OK gsettings-desktop-schemas.tcz: OK openjpeg.tcz: OK
# harfbuzz-icu.tcz: OK libxslt.tcz: OK libmbim.tcz: OK libqmi.tcz: OK icu67.tcz: OK js78.tcz: OK polkit.tcz: OK
#modemmanager.tcz: OK libavahi.tcz: OK libsoup.tcz: OK json-glib.tcz: OK geoclue.tcz: OK libgpg-error.tcz: OK libgcrypt.tcz: OK
#libsecret.tcz: OK libwebp1.tcz: OK libdvdread.tcz: OK libdvdnav.tcz: OK libtheora.tcz: OK gstreamer.tcz: OK gst-plugins-base.tcz: OK
#gst-plugins-bad.tcz: OK webkitgtk-gtk3.tcz: OK wxwidgets.tcz: OK audacity.tcz: OK 
# $ audacity opens as expected
 
submitqc --libs
rm -rf *.zsync
