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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=cdrkit
V=1.1.11
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc cmake libcap-dev bzip2-dev "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

URL=http://deb.debian.org/debian/pool/main/c/$P
cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
$URL/cdrkit_$V.orig.tar.gz  \
$URL/cdrkit_1.1.11-3.5.debian.tar.xz " $USER 
tar xvf *gz
tar xvf *xz # creates debian dir
cd /tmp/$SRC

PATCH1=fix-implicit-function-declaration.patch
PATCH2=fix_libcap_detection.patch
PATCH3=fix_typo.patch
PATCH4=gcc10.patch
PATCH5=update_geteltorito.patch

patch -Np1 < ../debian/patches/$PATCH1
patch -Np1 < ../debian/patches/$PATCH2
patch -Np1 < ../debian/patches/$PATCH3
patch -Np1 < ../debian/patches/$PATCH4
patch -Np1 < ../debian/patches/$PATCH5
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -Wno-dev
make -j5 # 10s
make -j5 install/strip DESTDIR=/tmp/$P
cd /tmp

# doc
#####
mkdir -p $P-doc/usr/local/share/doc/$P
mv $P/usr/local/share/man $P-doc/usr/local/share/
cp $SRC/doc/READMEs/* $P-doc/usr/local/share/doc/$P
cp $SRC/ABOUT $P-doc/usr/local/share/doc/$P

# main
######
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2' > $P/usr/local/share/doc/$P/COPYING

# move sbin to bin
mv $P/usr/local/sbin/netscsid $P/usr/local/bin
rm -rf $P/usr/local/sbin

mkdir $P/usr/local/share/$P
# move all that need symlinks here
cd $P/usr/local
mv bin/genisoimage share/$P
mv bin/icedax      share/$P
mv bin/readom      share/$P
mv bin/wodim       share/$P
cd /tmp

# install script
################
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
DIR1=/tmp/tcloop/cdrkit/usr/local/share/cdrkit 
DIR2=/usr/local/bin
cd $DIR1
cp * $DIR2/
cd $DIR2
ln -s genisoimage mkisofs
ln -s icedax cdda2wav
ln -s readom recordcd

# before linking set SUID
chmod u+s wodim
ln -s wodim cdrecord
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# TCZ them
###########
LIST2="$P $P-doc  "
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:          cdrkit-doc.tcz
Description:    man pages and readmes
Version:        1.1.11-3.5
Author:         see doc TCE/ABOUT        
Original-site:  https://packages.debian.org/source/bookworm/cdrkit
Copying-policy: GPL v2
Size:           128K           
Extension_by:   aus9 @ linuxquestions.org
Tags:           audio burn isofs
Comments:       info   
                
Change-log:     2024/05/08 v 1.11.1-3.5
Current:        2024/05/08  ' > $P-doc.tcz.info
echo 'man-db.tcz            ' > $P-doc.tcz.dep    

echo 'Title:          cdrkit.tcz
Description:    command line burning tools
Version:        1.1.11-3.5
Author:         see doc TCE/ABOUT        
Original-site:  https://packages.debian.org/source/bookworm/cdrkit
Copying-policy: GPL v2
Size:           1.1M           
Extension_by:   aus9 @ linuxquestions.org
Tags:           audio burn isofs
Comments:       This is a debian fork of and CONFLICTS with
                cdrtools as discussed in doc TCE ABOUT

                genisoimage (mkisofs) Generate ISO images
                icedax(cdda2wav) InCrEDible Audio eXtractor
                librols: lib Remains Of LibSchily
                libusal: lib Unified/Universal Scsi Access Layer
                netscsid: net SCSI Daemon
                readom(recordcd) read optical media 
                wodim(cdrecord) writes optical media tool
                see install script  
                
Change-log:     2024/05/08 v 1.11.1-3.5
Current:        2024/05/08  ' > $P.tcz.info

readelf -d $P/usr/local/sbin/netscsid | grep 'NEEDED' # only TCB
readelf -d $P/usr/local/bin/* | grep 'NEEDED' # not all are elfs
#libbz2.so.1.0]bzip2-lib
#libcap.so.2]libcap

echo 'bzip2-lib.tcz
libcap.tcz ' > $P.tcz.dep


# on pass 1 submitqc reverses my SUID so change to install script
submitqc --libs
rm -rf *.zsync

