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

# checking if gcc supports -fno-rtti -fno-exceptions... no
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe "

P=mpg123
V=1.33.3
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc portaudio-dev sdl2-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://sourceforge.net/projects/$P/files/$P/$V/$SRC.tar.bz2  
tar jxvf $SRC*bz2
cd $SRC # with optim* should remove need for -O2 sed
./configure --prefix=/usr/local --disable-static --localstatedir=/var --with-optimization=0 \
--with-audio=alsa,dummy,portaudio,sdl --enable-network --with-cpu=x86-64 

# Install path ............ /usr/local
# Components .............. programs libmpg123 libout123 libout123-modules libsyn123
# CPU Optimization ........ x86-64
# Compiler Optimization ... 0 # replaces the makefile sed 
# Use yasm (for AVX only) . disabled
# Gapless Support ......... enabled
# Debugging ............... disabled
# Terminal control ........ posix
# Extreme debugging ....... disabled
# Seek table size ......... 1000
# FIFO support ............ enabled
# Buffer .................. enabled
# External network ........ exec
# Internal network type ... posix
# IPv6 (getaddrinfo) ...... enabled
# largefile sensitive ..... no
# default offsets ......... 64
# explicit 64 bit offsets . no
# forced 64 bit offsets ... no
# only portable API ....... no

# Core libmpg123 features:
# Layer I ................. enabled
# Layer II ................ enabled
# Layer III ............... enabled
# NtoM resampling ......... enabled
# downsampled decoding .... enabled
# Feeder/buffered input ... enabled
# ID3v2 parsing ........... enabled
# String API .............. enabled
# ICY parsing/conversion .. enabled
# Error/warning messages .. enabled
# Win32 Unicode File Open.. unneeded
# Feature Report Function.. enabled
# Output formats (nofpu will disable all but 16 or 8 bit!):
# 8 bit integer ........... enabled
# 16 bit integer .......... enabled
# 32/24 bit integer ....... enabled
# real (32 bit float) ..... enabled
# Equalizer ............... enabled
# Optimization detail:
# Integer conversion ...... quality
# IEEE 754 hackery ........ enabled
# New/old WRITE_SAMPLE .... disabled
# new Huffman scheme ...... enabled
# runtime tables .......... disabled
# Note: Disabling core features is not commonly done and some combinations might
# not build/work. If you encounter such a case, help yourself (and provide
# a patch) or just poke the maintainers.
# libsyn123 special cases . enabled
# Modules ................. enabled
# Checked audio modules ... alsa dummy portaudio sdl  # not supporting pulseaudio
# Detected audio support .. alsa dummy portaudio sdl
# Runtime order ........... alsa portaudio pulse sdl

# CPPFLAGS='-DOPT_MULTI -DOPT_X86_64 -DOPT_GENERIC -DOPT_GENERIC_DITHER -DREAL_IS_FLOAT -DOPT_AVX '
# CFLAGS=' -mtune=generic -Os -pipe'
# PROG_LIBS='-lm '
# (derived from LIBS, only used for end-user binaries and modules)
# LIBDL=''
# LIBMPG123_LIBS='-lm '
# LIBSYN123_LIBS='-lm'
# LIBOUT123_LIBS=' -lm '

make -j6 # seconds
make install-strip 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
#####
rm -rf $P/usr/local/lib/*.la 
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/

# libmpg123
############
mkdir -p lib$P/usr/local/lib
mv $P/usr/local/lib/libmpg* lib$P/usr/local/lib

# main
######
mkdir -p $P/usr/local/share/doc/$P
echo "LGPL 2.1 " > $P/usr/local/share/doc/$P/COPYING

# TCZ them
###########
LIST2="$P $P-dev $P-doc lib$P"
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:          mpg123-dev.tcz
Description:    dev files
Version:        1.33.3
Author:         https://github.com/madebr/mpg123/blob/master-with-github-ci/AUTHORS
Original-site:  http://mpg123.org/
Copying-policy: multiple see COPYING
Size:           76K
Extension_by:   aus9 @linuxquestions.org
Tags:           audio player mpeg mp3
Comments:       Development
                Manually load sdl2-dev 
                
Change-log:     2016/02/07 v 1.23.0 (Juanito)
                2016/11/20 recompiled for updated pulseaudio
                2020/07/21 v 1.26.3 (Sashank999)
                2024/05/26 v 1.32.6 + sdl2 portaudio support (aus9) 
                2025/03/29 v 1.32.10 on 16x 
                2025/12/17 v 1.33.3 on 16x  
Current:        2025/12/20 v 1.33.3 rebuilt for circular dependency in dev TCE  ' > $P-dev.tcz.info
echo 'mpg123.tcz
portaudio-dev.tcz  ' > $P-dev.tcz.dep

echo 'Title:          mpg123-doc.tcz
Description:    man pages
Version:        1.33.3
Author:         https://github.com/madebr/mpg123/blob/master-with-github-ci/AUTHORS
Original-site:  http://mpg123.org/
Copying-policy: multiple see COPYING
Size:           20K
Extension_by:   aus9 @linuxquestions.org
Tags:           man
Comments:       -
                
Change-log:     2016/02/07 v 1.23.0 (Juanito)
                2016/11/20 recompiled for updated pulseaudio
                2020/07/21 v 1.26.3 (Sashank999)
                2024/05/26 v 1.32.6 + sdl2 portaudio support (aus9) 
                2025/03/29 v 1.32.10 on 16x 
                2025/12/17 v 1.33.3 on 16x  
Current:        2025/12/20 v 1.33.3 rebuilt ' > $P-doc.tcz.info
echo 'man-db.tcz ' > $P-doc.tcz.dep

echo 'Title:          libmpg123.tcz
Description:    3x lib files for mpg123
Version:        1.33.3
Author:         https://github.com/madebr/mpg123/blob/master-with-github-ci/AUTHORS
Original-site:  http://mpg123.org/
Copying-policy: multiple see COPYING
Size:           132K
Extension_by:   aus9 @linuxquestions.org
Tags:           audio player mpeg mp3
Comments:       Balance of lib files are in main TCE

Change-log:     2025/12/20 v 1.33.3 split from main TCE
Current:        2025/12/20             ' > libmpg123-doc.tcz.info
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' # only TCBs

echo 'Title:          mpg123.tcz
Description:    console or terminal mp3 player/streamer
Version:        1.33.3
Author:         https://github.com/madebr/mpg123/blob/master-with-github-ci/AUTHORS
Original-site:  http://mpg123.org/
Copying-policy: multiple see COPYING
Size:           184K
Extension_by:   aus9 @linuxquestions.org
Tags:           audio player mpeg mp3
Comments:       Supports alsa, portaudio, or sdl2
                but not pulseaudio.
                Alsa may need setup. 
                portaudio or sdl2 should not need setup

                Local files
                ###########
                First use try  $ mpg123 Dir-name/*
                (then input h) to see navigation keybindings
                eg q to quit, space to pause/unpause
                f to forward to next track,
                - to reduce volume, + to increase
                The doc TCE has many more options 

                streaming
                ##########
                I recommend you find http streams here
https://gist.github.com/Axel-Erfurt/3b1965ac233f65a4f968a649c7ae40bf
                We do not need curl as we have busybox wget, eg
$ mpg123 http://streaming.exclusive.radio/er/adele/icecast.audio
                
                I only found 1 https site - load curl then eg
$ mpg123 --network curl https://stream.nowyswiat.online/mp3
                
Change-log:     2016/02/07 v 1.23.0 (Juanito)
                2016/11/20 recompiled for updated pulseaudio
                2020/07/21 v 1.26.3 (Sashank999)
                2024/05/26 v 1.32.6 + sdl2 portaudio support (aus9) 
                2025/03/29 v 1.32.10 on 16x 
                2025/12/17 v 1.33.3 on 16x  
Current:        2025/12/20 v 1.33.3 split libmpg out ' > $P.tcz.info

readelf -d $P/usr/local/lib/$P/*.so | grep 'NEEDED'
# libasound.so.2] libasound portaudio
# libportaudio.so.2] portaudio
# libSDL2-2.0.so.0] sdl2
readelf -d $P/usr/local/bin/* | grep 'NEEDED'
# libmpg123.so.0] libmpg123
# libout123.so.0] this TCE
# libsyn123.so.0]there is a libsyn123 TCE but I am using inbuilt!

echo 'portaudio.tcz
sdl2.tcz
libmpg123.tcz ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

