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

# Export variables needed for compilation

case $(uname -m) in
	arm*)
		export CFLAGS="-flto=auto -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
		export CXXFLAGS="-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
	;;
	aarch64)
		export CFLAGS="-flto=auto -Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
		export CXXFLAGS="-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72"
	;;
esac
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig
echo $CFLAGS && echo $CXXFLAGS

P=cups
V=2.4.4
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc squashfs-tools wget openssl-dev libusb-dev avahi-dev "
for Z in $LIST    
    do 
    su -c "tce-load -i $Z" $USER
done

# list may have more than loaded
cd /usr/local/lib
rm -rf *.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/plugin/*.la
rm -rf gcc/aarch64-unknown-linux-gnu/12.2.0/*.la
rm -rf gprofng/*.la 
rm -rf python3.8/site-packages/*.la
rm -rf pkcs11/*.la
rm -rf cairo/*.la
rm -rf gtk-2.0/modules/*.la
rm -rf gtk-2.0/2.10.0/printbackends/*.la
rm -rf gtk-2.0/2.10.0/immodules/*.la
rm -rf gtk-2.0/2.10.0/engines/*.la
rm -rf imlib2/loaders/*.la
rm -rf imlib2/filters/*.la
rm -rf gdk-pixbuf-2.0/2.10.0/loaders/*.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcp1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/plugin/libcc1plugin.la
rm -rf gcc/armv7l-unknown-linux-gnueabihf/12.2.0/liblto_plugin.la
cd /tmp
rm -rf /usr/lib/*.la

su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/OpenPrinting/$P/releases/download/v$V/$SRC-source.tar.gz   " $USER
tar xvf $SRC*gz
cd $SRC
sed 's|444|644|g' -i Makedefs.in
./configure --prefix=/usr/local --sysconfdir=/usr/local/etc --localstatedir=/var --disable-static \
--with-cups-user=tc --with-cups-group=staff  --disable-acl --disable-dbus --without-systemd 
make -j4 # about 2M10s
make install DESTDIR=/tmp/$P # no rule to strip   
cd /tmp

# not all stripped do b4 splitting
##############################
strip --strip-unneeded $P/usr/local/lib/* # ignore dir warnings
# for reasons I know not....stripped already in ..local/lib/cups sub-dirs
# and ../bin and ../sbin elfs already stripped

# 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

# libcups
###########
LIB=/tmp/libcups/usr/local/lib
mkdir -p $LIB
mv $P/usr/local/lib/libcups* $LIB/
mkdir -p libcups/usr/local/share/doc/libcups
cp $SRC/LICENSE libcups/usr/local/share/doc/libcups

# created by cups-filters
rm -rf $P/usr/local/share/$P/banners 

# locale
######
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share
mkdir -p $P-locale/usr/local/share/$P/templates
mv $P/usr/local/share/$P/templates/da    $P-locale/usr/local/share/$P/templates
mv $P/usr/local/share/$P/templates/de    $P-locale/usr/local/share/$P/templates
mv $P/usr/local/share/$P/templates/es    $P-locale/usr/local/share/$P/templates
mv $P/usr/local/share/$P/templates/fr    $P-locale/usr/local/share/$P/templates
mv $P/usr/local/share/$P/templates/ja    $P-locale/usr/local/share/$P/templates
mv $P/usr/local/share/$P/templates/pt_BR $P-locale/usr/local/share/$P/templates
mv $P/usr/local/share/$P/templates/ru    $P-locale/usr/local/share/$P/templates
mkdir -p $P-locale/usr/local/share/doc/$P
mv $P/usr/local/share/doc/$P/da    $P-locale/usr/local/share/doc/$P
mv $P/usr/local/share/doc/$P/de    $P-locale/usr/local/share/doc/$P
mv $P/usr/local/share/doc/$P/es    $P-locale/usr/local/share/doc/$P
mv $P/usr/local/share/doc/$P/fr    $P-locale/usr/local/share/doc/$P
mv $P/usr/local/share/doc/$P/ja    $P-locale/usr/local/share/doc/$P
mv $P/usr/local/share/doc/$P/pt_BR $P-locale/usr/local/share/doc/$P
mv $P/usr/local/share/doc/$P/ru    $P-locale/usr/local/share/doc/$P

# main
######
rm -rf $P/usr/local/share/man
rm -rf $P/usr/share # we do not need icons or desktop-> info says launch web browser
# can not delete $P/usr/local/share/doc/cups/help/man-*, they are used by help clicking links

mkdir -p $P/usr/local/share/cups/files
mv $P/usr/local/etc/cups/cupsd.conf $P/usr/local/share/cups/files 

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
echo '#!/bin/sh
LOOP=/tmp/tcloop/cups/usr/local/share/cups/files/cupsd.conf
[ -f /usr/local/etc/cups/cupsd.conf ] || cp $LOOP /usr/local/etc/cups/    ' > $P/usr/local/tce.installed/$P
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# cups init
#############
mkdir -p $P/usr/local/etc/init.d
cat >> $P/usr/local/etc/init.d/$P <<'EOF'
#!/bin/sh
start() {
	if [ ! -f /var/run/dbus/cups_flag ]; then
		/usr/local/etc/init.d/dbus restart
		touch /var/run/dbus/cups_flag
	fi
	if ! pidof cupsd >/dev/null; then
		start-stop-daemon --start --exec /usr/local/sbin/cupsd 2>/dev/null
	fi
}

stop() {
	start-stop-daemon --stop --exec /usr/local/sbin/cupsd 2>/dev/null
	sleep 2
}

status() {
	if pidof cupsd >/dev/null; then
		echo -e "\ncups is running.\n"
		exit 0
	else
		echo -e "\ncups is not running.\n"
		exit 1
	fi	
}

case $1 in
	start) start
		;;
	stop) stop
		;;
	status) status
		;;
	restart) stop; start
		;;
	*) echo -e "\n$0 [start|stop|restart|status]\n"
		;;
esac 
EOF
chmod 755 $P/usr/local/etc/init.d/$P
# tabs and space not output ^^

# above build gave load errors but still ran ok 
# find: /tmp/tcloop/cups/usr/local/etc/cups/ssl: Permission denied
# fixes for me
##############
chmod 755 $P/usr/local/etc/cups/ssl
rm -rf  $P/var

# TCZ them
#######  
LIST="$P $P-dev $P-locale libcups "
for Z in $LIST
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:          cups-dev.tcz
Description:    cups devs 
Version:        2.4.4
Author:         OpenPrinting group
Original-site:  https://openprinting.github.io/cups/
Copying-policy: accompanied
Size:           56K
Extension_by:   aus9
Tags:           printing printer
Comments:       dev files

Change-log:     2015/08/12 v 2.0.4 (bmarkus)
                2017/11/04 v 2.2.4 12.x
Current:        2023/06/28 v 2.4.4 14.x    ' > $P-dev.tcz.info
echo 'cups.tcz
openssl-dev.tcz 
libusb-dev.tcz 
avahi-dev.tcz' > $P-dev.tcz.dep

echo 'Title:          cups-locale.tcz
Description:    languages 
Version:        2.4.4
Author:         OpenPrinting group
Original-site:  https://openprinting.github.io/cups/
Copying-policy: accompanied
Size:           1012K
Extension_by:   aus9
Tags:           printing printer
Comments:       Normal locales plus some doc type locales

Change-log:     2015/08/12 v 2.0.4 (bmarkus)
                2017/11/04 v 2.2.4 12.x
Current:        2023/06/28 v 2.4.4 14.x   ' > $P-locale.tcz.info
echo 'cups.tcz' > $P-locale.tcz.dep

echo 'Title:          cups.tcz
Description:    printing tool 
Version:        2.4.4
Author:         OpenPrinting group
Original-site:  https://openprinting.github.io/cups/
Copying-policy: accompanied
Size:           5.3M
Extension_by:   aus9
Tags:           printing printer
Comments:       Some printers/models support ipp anywhere
                https://www.pwg.org/ipp/everywhere.html
                Only some basic drivers are included in TCE
                Most drivers are in gutenprint or hplip 

                howto
                $ sudo /usr/local/etc/init.d/cups start
                the root password needs to be set as root owns /usr/local/etc/cups
                $ sudo passwd
                At some stage in the process below use root and root password 
                Ensure popups allowed in your web browser.

                http://localhost:631/admin will start the browser interface
                Click "add printer" (not find) and follow the prompts
                To persist across boots, add the following to your backup 
                (but remove leading "/" s). --- PPD are examples only
                No need to backup something.ppd.0
                /usr/local/etc/cups/ppd/HP_DeskJet_2600_series.ppd 
                /usr/local/etc/cups/ppd/Canon_MG3000_series.ppd
                /usr/local/etc/cups/printers.conf
                /usr/local/etc/cups/cupsd.conf (only if you changed it)
                
                Most likely these are already backed up but if not there, 
                add to backup please 
                /etc/group # contains lp group for printing
                /etc/gshadow
                /etc/passwd
                /etc/shadow 

                Cups root session password is more flexible. Unless you change
                config files, you do not need to use the same root password you
                originally used to setup the printer(s). As long as you input the
                current root password in the popup, configs should be edittable.
 
Change-log:     2015/08/12 v 2.0.4 (bmarkus)
                2017/11/04 v 2.2.4 12.x
Current:        2023/06/28 v 2.4.4 14.x           ' > $P.tcz.info

readelf -d $P/usr/local/sbin/cups* | grep 'NEEDED' # remove duplicates TCB
#[libcups.so.2] this TCE
#[libavahi-common.so.3] [libavahi-client.so.3]libavahi
readelf -d $P/usr/local/sbin/lp* | grep 'NEEDED' # remove duplicates TCB below
readelf -d $P/usr/local/bin/* | grep 'NEEDED' 
readelf -d $P/usr/local/lib/cups/backend/* | grep 'NEEDED'
[libusb-1.0.so.0]libusb
readelf -d $P/usr/local/lib/cups/cgi-bin/* | grep 'NEEDED'
readelf -d $P/usr/local/lib/cups/command/* | grep 'NEEDED'
readelf -d $P/usr/local/lib/cups/daemon/* | grep 'NEEDED'
readelf -d $P/usr/local/lib/cups/filter/* | grep 'NEEDED'
readelf -d $P/usr/local/lib/cups/monitor/* | grep 'NEEDED'
readelf -d $P/usr/local/lib/cups/notifier/* | grep 'NEEDED'

# cups-filters not yet re-built/upgraded
echo 'libcups.tcz
libavahi.tcz
libusb.tcz 
cups-filters.tcz          ' > cups.tcz.dep

echo 'Title:          libcups.tcz
Description:    cups library files 
Version:        2.4.4
Author:         OpenPrinting group
Original-site:  https://openprinting.github.io/cups/
Copying-policy: accompanied
Size:           248K
Extension_by:   aus9
Tags:           printing printer
Comments:       Howto print from a cups client to cups server
                create a file of the form:
                $ cat /usr/local/etc/cups/client.conf
                ServerName 192.168.1.200
                replace 192.168.1.200 with the ip address of your cups server
                and modify hosts as follows:
                $ cat /etc/hosts
                127.0.0.1 cups_client localhost localhost.local
                192.168.1.200 cups_server cups_server.local
                replace cups_client and cups_server with the hostnames from your client/server

Change-log:     2015/08/12 v 2.0.4 (bmarkus)
                2017/11/04 v 2.2.4 12.x
Current:        2023/06/28 v 2.4.4 14.x     ' > libcups.tcz.info

readelf -d lib$P/usr/local/lib/libcups* | grep 'NEEDED' # remove duplicates TCB
# dep gnutls replaced by openssl
#[libavahi-common.so.3][libavahi-client.so.3]libavahi
#[libssl.so.3] [libcrypto.so.3]openssl
#[libcups.so.2] this TCE

echo 'libavahi.tcz
openssl.tcz
libusb.tcz    ' > libcups.tcz.dep

# submitqc --libs  rm -rf *.zsync

