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

# lame-dev loads libsndfile-dev watch out for that
export CFLAGS="-mtune=generic -Os -pipe" 
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti "

P=libsndfile
V=1.2.2
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc lame-dev mpg123-dev opus-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://github.com/$P/$P/releases/download/$V/$SRC.tar.xz 
tar xvf $SRC*xz
cd $SRC 
./configure --prefix=/usr/local --enable-static=no  --enable-alsa --enable-mpeg
# libsndfile version : ...........1.2.2
# Host CPU : .....................x86_64
# Host Vendor : ..................pc
# Host OS : ......................linux-gnu
# CFLAGS : -mtune=generic -Os -pipe -Wall -Wextra -Wpointer-arith -Wcast-align -Wcast-qual -Wshadow -Wwrite-strings \
#        -Wundef -Wuninitialized -Winit-self -Wvla -Wbad-function-cast -Wnested-externs -Wstrict-prototypes \
#        -Wmissing-prototypes -Wmissing-declarations -Waggregate-return 
# CXXFLAGS : -mtune=generic -Os -pipe -fno-exceptions -fno-rtti  -Wall -Wextra -Wpointer-arith -Wcast-align -Wcast-qual \
#         -Wshadow -Wwrite-strings -Wundef -Wuninitialized -Winit-self -Wctor-dtor-privacy -Wnon-virtual-dtor \
#          -Woverloaded-virtual -Wreorder -Wsign-promo
# CPPFLAGS : ............................ 
# LDFLAGS : ............................. 
# Experimental code : ............no
# Using ALSA in example programs :yes
# External FLAC/Ogg/Vorbis/Opus...yes
# External MPEG Lame/MPG123 : ... yes
# Building Octave interface : ... no
#    Tools :
# C Compiler Vendor is : ..... gnu (14.2.0)
# CXX Compiler Vendor is :.... gnu (14.2.0)
# Sanitizer enabled : ........ no
# Installation directories :
# Library directory : .... /usr/local/lib
# Program directory : .... /usr/local/bin
# Pkgconfig directory : . /usr/local/lib/pkgconfig
# HTML docs directory : /usr/local/share/doc/libsndfile
make -j6 # 8 seconds
make install-strip DESTDIR=/tmp/$P
cd /tmp

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

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

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

# TCZ them
###########
LIST2="$P $P-dev $P-doc "
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:          libsndfile-dev.tcz
Description:    dev files
Version:        1.2.2
Author:         https://github.com/libsndfile/libsndfile/blob/master/AUTHORS
Original-site:  https://github.com/libsndfile/
Copying-policy: LGPL v 2.1
Size:           16K           
Extension_by:   aus9 @linuxquestions.org
Tags:           audio 
Comments:       Development
                lame-dev was built with libsndfile-dev
                so lame-dev needs to be loaded manually,
                if you need to recompile something

Change-log:     2013/11/10 v 1.0.25 (Juanito)
                2024/06/21 v 1.0.28 on 15x contains libsndfile.a (aus9)
Current:        2025/12/17 v 1.2.2 on 16x - no libsndfile.a' > $P-dev.tcz.info
echo 'mpg123-dev.tcz 
opus-dev.tcz    ' > $P-dev.tcz.dep  

echo 'Title:          libsndfile-doc.tcz
Description:    manpages and html
Version:        1.2.2
Author:         https://github.com/libsndfile/libsndfile/blob/master/AUTHORS
Original-site:  https://github.com/libsndfile/
Copying-policy: LGPL v 2.1
Size:           84K          
Extension_by:   aus9 @linuxquestions.org
Tags:           audio 
Comments:       help
                
Change-log:     2024/06/21 v 1.0.28 on 15x
Current:        2025/12/17 v 1.2.2 on 16x ' > $P-doc.tcz.info
echo 'man-db.tcz' > $P-doc.tcz.dep

echo 'Title:          libsndfile.tcz
Description:    read write routines for audio data
Version:        1.2.2
Author:         http://www.mega-nerd.com/libsndfile/AUTHORS
Original-site:  http://www.mega-nerd.com/libsndfile/
Copying-policy: LGPL v 2.1
Size:           292K          
Extension_by:   aus9 @linuxquestions.org
Tags:           audio  
Comments:       C routines for reading/writing files 
                containing sampled audio data

                v 1.2.2 (onwards) adds more sound support but can not
                have lame in the dep file. Load it manually please.
                Lame was built with libsndfile as a dep

                $ sndfile-convert -h
                eg $ sndfile-convert -vorbis wav.wav oga.oga
                $ file oga.oga
oga.oga: Ogg data, Vorbis audio, stereo, 48000 Hz, ~128000 bps, created by: Xiph.Org libVorbis I
                
Change-log:     2013/11/10 v 1.0.25 (Juanito)
                2024/06/21 v 1.0.28 on 15x (aus9)
Current:        2025/12/17 v 1.2.2 on 16x increase audio support  ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED' # ignore TCBS
#libsndfile.so.1] this TCE
#libFLAC.so.8] flac 
#libvorbis.so.0]#libvorbisenc.so.2] libvorbis mpg123
#libopus.so.0] opus
#libogg.so.0] libogg mpg123
#libmpg123.so.0] mpg123
#libmp3lame.so.0] lame

echo 'opus.tcz
mpg123.tcz 
flac.tcz
libvorbis.tcz' > $P.tcz.dep
# no submit as it fails to know of circular dep for lame 

#........depends-on.sh libsndfile-dev
# lame-dev.tcz libsamplerate-dev.tcz pulseaudio-dev.tcz sbc-dev.tcz speech-dispatcher-dev.tcz

# lame & mp3rtp expects libsndfile.so.1  lib files........................ OK
# libsamplerate expects nothing for lib and bin expects libsndfile.so.1.....OK
# sbc (my baby) bin or libs look only for libc.so.6.........................Ok
# libpulseaudio no lib files look for my TCE
# usr/local/lib/pulseadio lib files look for libsndfile.so.1...............OK
# speech-d*  no bin file look for my TCe
# speech-d* lib files nope...you made it a dep probably ldd hit.............OK

