#!/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=varnish
V=7.5.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc pcre21042-dev python3.9-docutils python3.9-sphinx "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

ln -s /usr/local/include/ncursesw/curses.h /usr/local/include/
ln -s /usr/local/include/ncursesw/term.h /usr/local/include/

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://$P-cache.org/_downloads/$SRC.tgz   " $USER 
tar xvf $SRC*tgz
cd $SRC
./configure prefix=/usr/local --with-ncurses  
make -j5 # 30 seconds
make install-strip DESTDIR=/tmp/$P # no strip
cd /tmp

# dev
#####
rm -rf $P/usr/local/lib/*.la 
rm -rf $P/usr/local/lib/varnish/vmods/*.la
mkdir -p $P-dev/usr/local/lib/
mkdir -p $P-dev/usr/local/share
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
mv $P/usr/local/share/aclocal $P-dev/usr/local/share

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

# main
######
# fix python pathways
sed 's|usr|usr/local|' -i $P/usr/local/share/varnish/*.py

# already there mkdir -p $P/usr/local/share/doc/$P
cp $SRC/LICENSE $P/usr/local/share/doc/$P/

# 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 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:           varnish-dev.tcz
Description:     dev files
Version:         7.5.0
Author:          varnish team
Original-site:   https://varnish-cache.org
Copying-policy:  accompanied
Size:            176K
Extension_by:    aus9 @ linuxquestions.org
Tags:            server http cache 
Comments:        Development

Change-log:     2017/09/17 v 5.0.0 (chiguireitor)
Current:        2024/04/08 v 7.5.0 on 15x (aus9) ' > $P-dev.tcz.info
echo 'varnish.tcz
pcre21042-dev.tcz 
python3.9-docutils.tcz 
python3.9-sphinx.tcz ' > $P-dev.tcz.dep

echo 'Title:           varnish-doc.tcz
Description:     man pages
Version:         7.5.0
Author:          varnish team
Original-site:   https://varnish-cache.org
Copying-policy:  accompanied
Size:            180K
Extension_by:    aus9 @ linuxquestions.org
Tags:            server http cache 
Comments:        man pages 
                 see https://varnish-cache.org/docs/index.html
                 for manuals tutorials etc

Change-log:     2017/09/17 v 5.0.0 (chiguireitor)
Current:        2024/04/08 v 7.5.0 on 15x (aus9) ' > $P-doc.tcz.info
echo 'man-db.tcz ' > $P-doc.tcz.dep

echo 'Title:           varnish.tcz
Description:     caching HTTP reverse proxy.
Version:         7.5.0
Author:          varnish team
Original-site:   https://varnish-cache.org
Copying-policy:  accompanied
Size:            1.2M
Extension_by:    aus9 @ linuxquestions.org
Tags:            server http cache  
Comments:        Untested by me. Has had vulnerabily reports
                 so watch https://varnish-cache.org/security/index.html
                 and consider subscribing at above link
                 see varnish-doc please

                 Please read /usr/local/share/doc/varnish/builtin.vcl
                 see https://varnish-cache.org/docs/index.html
                 for manuals tutorials etc

Change-log:     2017/09/17 v 5.0.0 (chiguireitor)
Current:        2024/04/08 v 7.5.0 remove pre dep on 15x (aus9) ' > $P.tcz.info


readelf -d $P/usr/local/bin/* | grep 'NEEDED' # ignore dupes and TCBS
#libvarnishapi.so.3] the TCE
#libpcre2-8.so.0] pcre21042.tcz
#libreadline.so.8] readline
#libncursesw.so.6] ncursesw -> readline
readelf -d $P/usr/local/lib/*.so | grep 'NEEDED' # dupes only
readelf -d $P/usr/local/lib/varnish/vmods/*.so | grep 'NEEDED' # they are all TCBs hmm

echo 'pcre21042.tcz
readline.tcz ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

# sudo varnishd -d
# Debug: Version: varnish-7.5.0 revision eef25264e5ca5f96a77129308edb83ccf84cb1b1
# Debug: Platform: Linux,6.6.8-tinycore64,x86_64,-jnone,-sdefault,-sdefault,-hcritbit
# 200 317     
#-----------------------------
#Varnish Cache CLI 1.0
#-----------------------------
#Linux,6.6.8-tinycore64,x86_64,-jnone,-sdefault,-sdefault,-hcritbit
# varnish-7.5.0 revision eef25264e5ca5f96a77129308edb83ccf84cb1b1

#Type 'help' for command list.
#Type 'quit' to close CLI session.
#Type 'start' to launch worker process.

# I input start
# 300 13      
#No VCL loaded
# input ctrl + c to abort



