#!/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 flac-dev lame-dev libvorbis-dev python3.9 "
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:       Manually load lame-dev due to 
                circular dependency

Change-log:     2013/11/10 v 1.0.25 (Juanito)
                2024/06/21 v 1.0.28 on 15x contains libsndfile.a (aus9)
                2025/12/17 v 1.2.2 on 16x - no libsndfile.a
Current:        2025/12/20 remove circular dependency' > $P-dev.tcz.info
echo 'libsndfile.tcz
flac-dev.tcz 
libvorbis-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
                2025/12/17 v 1.2.2 on 16x increase audio support
Current:        2025/12/20 remove a circular dependency in dev ' > $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)
                2025/12/17 v 1.2.2 on 16x increase audio support
Current:        2025/12/20 remove a circular dependency in dev  ' > $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 
#libopus.so.0] opus
#libogg.so.0] libogg 
#libmpg123.so.0] libmpg123
#libmp3lame.so.0] lame

echo 'flac.tcz
libvorbis.tcz
opus.tcz
libmpg123.tcz 
lame.tcz' > $P.tcz.dep

submitqc --libs
rm -rf *zsync



