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

# ref https://audacious-media-player.org/news/45-audacious-4-0-released
# they prefer Qt or gtk2
# build audacious then the plugins. Ensure you have no plugins TCE in tcedir to build plugins so
# I am not loading the old libavformat4 and similar
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe "

P=audacious
V=4.3.1
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc gtk2-dev gettext "
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://distfiles.$P-media-player.org/$SRC.tar.bz2   " $USER 
tar jxvf $SRC*bz2
cd $SRC
./configure --prefix=/usr/local --localstatedir=/var --disable-qt 
# disable dbus does not build audtool so dbus needed   
# Install path:                           /usr/local
# D-Bus support:                          yes
# GTK+ support:                           yes
# Qt support:                             no
# libarchive support:                     no
# Valgrind analysis support:              no
find . -name buildsys.mk -type f -exec sed -i 's/-g -O2//g' {} \;
make -j5 # 10 seconds
make install DESTDIR=/tmp/$P # no strip
cd /tmp

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

# 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/

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

# main
######
# audacious expects docs non-std pathway u loc share $P (not doc $P)

mkdir -p $P/usr/local/share/pixmaps
cp $P/usr/local/share/icons/hicolor/48x48/apps/$P.png $P/usr/local/share/pixmaps
echo 'X-FullPathIcon=/usr/local/share/pixmaps/audacious.png' >> $P/usr/local/share/applications/$P.desktop

mkdir -p $P/usr/local/share/doc/$P
strip --strip-unneeded $P/usr/local/lib/*
strip --strip-unneeded $P/usr/local/bin/*

# due to some strange error....use install script to make exe 755 later
mkdir -p $P/usr/local/share/$P/files
mv $P/usr/local/bin/$P  $P/usr/local/share/$P/files

# 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 

# there is some strange activity with normal build for me, fix below
P=audacious
FILE=/tmp/tcloop/$P/usr/local/share/$P/files/$P
[ -x /usr/local/bin/$P ] || cp $FILE /usr/local/bin/
chmod 755 /usr/local/bin/$P
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# TCZ them
#######
LIST="$P $P-dev $P-doc $P-locale "
for Z in $LIST
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:          audacious-dev.tcz
Description:    dev files
Version:        4.3.1
Author:         https://github.com/audacious-media-player/audacious/blob/master/AUTHORS         
Original-site:  https://distfiles.audacious-media-player.org/
Copying-policy: multiple see COPYING
Size:           60K           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio media player gtk2
Comments:       development
                
Change-log:     2014/02/11 v 3.4.3 (Juanito)
                2016/02/07 v 3.7.1
                2019/04/19 v 3.9
Current:        2024/05/30 v 4.3.1 on 15x, gtk3->2 (aus9) ' > $P-dev.tcz.info
echo 'audacious.tcz
gtk2-dev.tcz' > $P-dev.tcz.dep

echo 'Title:          audacious-doc.tcz
Description:    man page
Version:        4.3.1
Author:         https://github.com/audacious-media-player/audacious/blob/master/AUTHORS         
Original-site:  https://distfiles.audacious-media-player.org/
Copying-policy: multiple see main TCE, COPYING
Size:           8.0K           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio media player gtk2
Comments:       man page contains keybindings info
                
       Control + Return        Play
       Space, Control + ,      Pause
       Control + .             Stop
       Alt + Up                Previous song
       Alt + Down              Next song
       Right arrow             Seek forward (by default 5 seconds)
       Left arrow              Seek backward (by default 5 seconds)
       Escape                  Scroll to current song
       Control + a             Select all songs in playlist
       Shift + Control + a     Cancel selection
       Control + + (plus)      Increase volume 5 percent
       Control + - (minus)     Decrease volume 5 percent
       Control + s             Toggle shuffle
       Control + r             Toggle repeat
       Control + n             Toggle advancing in playlist
       Control + m             Toggle stopping after current song
       Control + e             Display Equalizer
       Control + y             Display Search Tool
       Control + i             Display Song Information dialog
       Control + k             Display Jump to Time dialog
       Control + j             Display Jump to Song dialog
       Control + p             Display Playlist Manager dialog
       Control + u             Display Queue Manager dialog
       Control + o             Display Open Files dialog
       Shift + Control + o     Display Add Files dialog
       Control + l             Display Open URL dialog
       Shift + Control + l     Display Add URL dialog
                
Change-log:     2024/05/30 v 4.3.1 on 15x
Current:        2024/05/30 v 4.3.1 on 15x, gtk3->2 (aus9) ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          audacious-locale.tcz
Description:    langauge files
Version:        4.3.1
Author:         https://github.com/audacious-media-player/audacious/blob/master/AUTHORS         
Original-site:  https://distfiles.audacious-media-player.org/
Copying-policy: multiple see COPYING
Size:           472K           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio media player gtk2
Comments:       locale
                
Change-log:     2014/02/11 v 3.4.3 (Juanito)
                2016/02/07 v 3.7.1
                2019/04/19 v 3.9
Current:        2024/05/30 v 4.3.1 on 15x, gtk3->2 (aus9) ' > $P-locale.tcz.info
echo 'audacious.tcz' > $P-locale.tcz.dep

echo 'Title:          audacious.tcz
Description:    gtk2 GUI audio player
Version:        4.3.1
Author:         https://github.com/audacious-media-player/audacious/blob/master/AUTHORS         
Original-site:  https://distfiles.audacious-media-player.org/
Copying-policy: multiple see COPYING
Size:           376K           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio media player gtk2
Comments:       Assumes alsa or pulseaudio setup. 
                To play cd -> Services -> play cd
                See doc info for keybindings 
                
                Some common issues discussed here
                https://audacious-media-player.org/problems

                Optional TCES 
                curl, libnotify & neon for streaming
                faad for aac file formats
                Most other audio files supported

                Without libnotify play stream stutters with
                ALSA lib pcm.c:8740:(snd_pcm_recover) underrun occurred
                test URL was http://stream.zeno.fm/2ptupyw61fxvv
                
                QUIRK, dbus is a make dependency but 
                not a run dependency. 
                
Change-log:     2014/02/11 v 3.4.3 (Juanito)
                2016/02/07 v 3.7.1
                2019/04/19 v 3.9
Current:        2024/05/30 v 4.3.1 on 15x, gtk3->2 (aus9) ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED'
#libglib-2.0.so.0]libgio-2.0.so.0]libgobject-2.0.so.0glib2 -> at-spi2-core -> gtk2
readelf -d $P/usr/local/share/$P/files/* | grep 'NEEDED'
#libaudcore.so.5]this TCE

readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' 
#libglib-2.0.so.0]libgmodule-2.0.so.0]libgio-2.0.so.0]libgobject-2.0.so.0]glib2 -> at-spi2-core -> gtk2
#libgtk-x11-2.0.so.0]libgdk-x11-2.0.so.0]gtk2
#libpangocairo-1.0.so.0]libpango-1.0.so.0]libpangoft2-1.0.so.0] pango -> gtk2
#libatk-1.0.so.0]at-spi2-core -> gtk2
#libcairo.so.2]cairo -> pango -> gtk2
#libgdk_pixbuf-2.0.so.0]gdk-pixbuf2 -> gtk2
#libharfbuzz.so.0]harfbuzz -> freetype -> fontconfig -> cairo -> pango -> gtk2
#libfontconfig.so.1]fontconfig -> cairo -> pango -> gtk2
#[libfreetype.so.6]freetype -> fontconfig -> cairo -> pango -> gtk2

ldd audacious/usr/local/bin/audtool
# libpcre2-8.so.0  -> libpcre21042
# libffi.so.7 -> libffi
ldd audacious/usr/local/share/audacious/files/audacious # no extras

# gtk2 will be a dep of plugins....to be built next 
# no mention for readelf or ldd for dbus

echo 'shared-mime-info.tcz
gtk-update-icon-cache.tcz
pcre21042.tcz
libffi.tcz  ' > $P.tcz.dep

submitqc --libs # adds gtk2 back in because does not understand our reversed dep file
rm -rf *.zsync

# pass 1 dep has no plugins....pass 2 done after plugins built changes dep

##### test 
audacious -V -m

# add plugins TCE to dep after build
