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

CFLAGS="-march=i486 -mtune=i686 -Os -pipe   " 
CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fno-exceptions -fno-rtti  "
echo $CFLAGS && echo $CXXFLAGS

P=mpg123
V=1.31.3
SRC=$P-$V

USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools wget SDL2-dev \
pulseaudio-dev  "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /usr/local/lib
rm -rf *.la
rm -rf gcc/i486-pc-linux-gnu/12.2.0/plugin/libcp1plugin.la
rm -rf gcc/i486-pc-linux-gnu/12.2.0/plugin/libcc1plugin.la
rm -rf gcc/i486-pc-linux-gnu/12.2.0/liblto_plugin.la
rm -rf mpg123/output_sdl.la
rm -rf mpg123/output_pulse.la
rm -rf mpg123/output_oss.la
rm -rf mpg123/output_openal.la
rm -rf mpg123/output_dummy.la
rm -rf mpg123/output_alsa.la
cd /tmp
rm -rf /usr/lib/*.la
find / -name *.la

URL=https://sourceforge.net/projects/$P/files/$P
su -c "/usr/local/bin/wget -nc --no-check-certificate \
$URL/$V/$SRC.tar.bz2"  $USER
tar jxvf $SRC*bz2
cd $SRC
./configure --prefix=/usr/local --enable-modules=yes --enable-debug=no \
--with-cpu=i486 --with-audio=alsa,pulse,sdl
# Install path ............ /usr/local
#  CPU Optimization ........ i486
#  Compiler Optimization ... 2
#  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
#  File offsets ............ default
#  LFS alias symbols ....... enabled (32)
#  LFS alias type .......... long
#  Use yasm (for AVX only) . disabled
#       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 .... enabled
#  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 pulse sdl dummy
#  Detected audio support .. alsa pulse sdl dummy
#  Runtime order ........... alsa pulse sdl
# WARNING: You selected the i486 decoder. This is not recommened for regular use.
# It was designed for actual i486 CPUs a long time ago. Generic C code is likely
# to perform better nowadays. Also, the decoder misses features like volume
# scaling and has clipping issues. Continue if you are into software archeology
# only.
#  CPPFLAGS=' -DOPT_I486 -DREAL_IS_FLOAT -DNEWOLD_WRITE_SAMPLE '
#  CFLAGS='-O2 -fomit-frame-pointer -funroll-all-loops -finline-functions -ffast-math  -g -O2'
#  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 -j4 # 15 secs
make install-strip DESTDIR=/tmp/$P 
cd /tmp

# dev
####  la files no longer needed to run mpg123
mkdir -p $P-dev/usr/local/lib
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib 
mkdir -p $P-dev/usr/local/lib
mv $P/usr/local/lib/*.la $P-dev/usr/local/lib/ 
chmod 664 $P-dev/usr/local/lib/*.la
mv $P/usr/local/include $P-dev/usr/local 

# main
#######
rm -rf $P/usr/local/share/man
mkdir -p $P/usr/local/share/doc/$P
cp $SRC/COPYING  $P/usr/local/share/doc/$P/	

# TCZ them
###########
LIST2="$P $P-dev "
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:          mpg123-dev.tcz
Description:    dev files
Version:        1.31.3
Author:         Thomas Orgis & Nicholas J. Humfrey
                Original Michael Hipp        
Original-site:  https://sourceforge.net/projects/mpg123
Copying-policy: accompanied see main TCE
Size:           72K
Extension_by:   aus9
Tags:           mp3 player decoder alsa pulseaudio sdl
Comments:       Originally in the run TCE, now that TCE
                runs without la files

Changelog:      2023/09/02 spilt from main TCE v 1.31.3
Current:        2023/09/02 ' > $P-dev.tcz.info
echo 'mpg123.tcz
SDL2-dev.tcz
pulseaudio-dev.tcz ' > $P-dev.tcz.dep

echo 'Title:          mpg123.tcz
Description:    mp3 player & decoder
Version:        1.31.3
Author:         Thomas Orgis & Nicholas J. Humfrey
                Original Michael Hipp        
Original-site:  https://sourceforge.net/projects/mpg123
Copying-policy: accompanied
Size:           488K
Extension_by:   aus9
Tags:           mp3 player decoder alsa pulseaudio sdl
Comments:       Default alsa, setup alsa first
                $ mpg123 file.mp3
                Or pulseaudio, setup first
                $ mpg123 -o pulse file.mp3
                Or SDL2, load it no setup needed
                $ mpg123 -o sdl file.mp3
                
                NO audio dependencies set.
                
Changelog:      2008/11/05 First version (i486 compatible) (Jason W)
                2008/12/04 needs *.la & libltdl.so files 
                2009/02/18 Fixed broken symlink found by mikshaw.
                2009/03/28 v 1.7.0
                2009/03/30 needs *.la files (Curaga)
                2009/06/19 v 1.8.1 and ALSA output module
                2010/03/06 v 1.10.1 and put in /usr/local
                2013/09/17 v 1.15.4 (juanito)
Current:        2023/08/27 v 1.31.3 on 14x no oss support, no *.la files (aus9)' > $P.tcz.info

readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' #TCBS 
readelf -d $P/usr/local/lib/$P/output_alsa.so | grep 'NEEDED'
# [libasound.so.2] libasound
readelf -d $P/usr/local/lib/$P/output_pulse.so | grep 'NEEDED'
#[libpulse-simple.so.0][libpulse.so.0]pulseaudio
readelf -d $P/usr/local/lib/$P/output_sdl.so | grep 'NEEDED' 
#[libSDL2-2.0.so.0]SDL2
readelf -d $P/usr/local/bin/* | grep 'NEEDED'
#[libmpg123.so.0][libout123.so.0][libsyn123.so.0]this TCE


# no main dep set...libltdl is not appear?....its a dep of pulseaudio tho

# test setup alsa  $ mpg123 file.mp3 plays as expected
# test setup pulse $ mpg123 -o pulse file.mp3....plays but pulseaudio setup shows some gremlins
# test setup SDL2  $ mpg123 -o sdl file.mp3



