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

# based on arch build
# https://gitlab.archlinux.org/archlinux/packaging/packages/volume_key/-/blob/main/PKGBUILD?ref_type=heads

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

P=volume_key
V=0.3.12
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc compiletc automake cryptsetup-dev glib2-dev gpgme-dev   \
nspr-dev  nss-dev python3.9-dev swig util-linux-dev libtool-dev gettext-dev "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://releases.pagure.org/$P/$SRC.tar.xz  " $USER 
tar xvf $P*xz
cd $SRC
autoreconf -fiv
./configure --prefix=/usr/local --with-python3
make -j5 # seconds
make install-strip DESTDIR=/tmp/$P
cd /tmp

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

# locale 
######
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share 

# dev
#####
rm -rf  $P/usr/local/lib/*.la 
rm -rf $P/usr/local/lib/python3.9/site-packages/_volume_key.la
mkdir -p $P-dev/usr/local/
mv $P/usr/local/include $P-dev/usr/local/

# main
###### submit finds perm error fix py from 644 to 755 on pass 1
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2' > $P/usr/local/share/doc/$P/COPYING
chmod 644 $P/usr/local/lib/python3.9/site-packages/volume_key.py # pass 2

# TCZ them
###########
LIST2="$P $P-dev $P-doc $P-locale"
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:          volume_key-dev.tcz
Description:    dev files
Version:        0.3.12
Author:         Miloslav Trmac    
Original-site:  https://pagure.io/volume_key
Copying-policy: GPL v2   
Size:           8.0K
Extension_by:   aus9 @ linuxquestions.org  
Tags:           encryption crypto key            
Comments:       development
     
Change-log:     2017/11/02 v 0.3.9 (Juanito)
                2020/01/30 v 0.3.12
Current:        2024/04/07 rebuilt against python3.9, drop pcre dep on 15x (aus9)   ' > $P-dev.tcz.info
echo 'volume_key.tcz
cryptsetup-dev.tcz
glib2-dev.tcz
gpgme-dev.tcz
nspr-dev.tcz
nss-dev.tcz
python3.9-dev.tcz
swig.tcz
util-linux-dev.tcz' > $P-dev.tcz.dep

echo 'Title:          volume_key-doc.tcz
Description:    man page
Version:        0.3.12
Author:         Miloslav Trmac    
Original-site:  https://pagure.io/volume_key
Copying-policy: GPL v2   
Size:           4.0K
Extension_by:   aus9 @ linuxquestions.org  
Tags:           encryption crypto key            
Comments:       development
     
Change-log:     2024/04/07 first v
Current:        2024/04/07    ' > $P-doc.tcz.info
echo 'man-db.tcz ' > $P-doc.tcz.dep

echo 'Title:          volume_key-locale.tcz
Description:    locales
Version:        0.3.12
Author:         Miloslav Trmac    
Original-site:  https://pagure.io/volume_key
Copying-policy: GPL v2   
Size:           212K
Extension_by:   aus9 @ linuxquestions.org  
Tags:           encryption crypto key            
Comments:       untested by me
     
Change-log:     2024/04/07 v 0.3.12 first 
Current:        2024/04/07                   ' > $P-locale.tcz.info
echo 'volume_key.tcz' > $P.tcz.dep

echo 'Title:          volume_key.tcz
Description:    work with volume encryption secrets and escrow packets
Version:        0.3.12
Author:         Miloslav Trmac    
Original-site:  https://pagure.io/volume_key
Copying-policy: GPL v2   
Size:           88K
Extension_by:   aus9 @ linuxquestions.org  
Tags:           encryption crypto key            
Comments:       exe and library for manipulating storage volume 
                encryption keys and storing them separately 
                from volumes to handle forgotten passphrases
     
Change-log:     2017/11/02 v 0.3.9 (Juanito)
                2020/01/30 v 0.3.12
Current:        2024/04/07 rebuilt against python3.9 on 15x (aus9)   ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P| grep 'NEEDED' # ignore TCBS
#libvolume_key.so.1] this TCE
#libgpgme.so.11]gpgme
#libassuan.so.0] libassuan -> gpgme
#libgpg-error.so.0]libgpg-error -> libassuan -> gpgme
#libcryptsetup.so.4]cryptsetup
#libglib-2.0.so.0]glib2
#libnss3.so]libnssutil3.so]libsmime3.so]libssl3.so]libsoftokn3.so] nss
#libplds4.so]libplc4.so]libnspr4.so] nspr -> nss
readelf -d $P/usr/local/lib/lib$P.so.1.2.3.| grep 'NEEDED' # no extras

echo 'gpgme.tcz
cryptsetup.tcz 
glib2.tcz
nss.tcz    ' > $P.tcz.dep

submitqc --libs # on pass 1 but then fix up its mistake for py file remains 644
rm -rf *.zsync


