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

export CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe" 
export CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe \
-fno-exceptions -fno-rtti"

P=cups
V=2.3.3
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc wget squashfs-tools zsync gnutls-dev libusb-dev avahi-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://github.com/apple/cups/releases/download/v$V/$SRC-source.tar.gz" $USER
tar xvf $SRC*gz
LOC=/usr/local
cd $SRC
sed 's|444|644|g' -i Makedefs.in

./configure --prefix=$LOC --sysconfdir=$LOC/etc --localstatedir=/var --disable-static \
--with-cups-user=tc --with-cups-group=staff --disable-systemd --disable-acl 
make -j5 # takes almost 2 minutes 
make install DESTDIR=/tmp/$P   
cd /tmp

# dev
#####
mkdir -p $P-dev/usr/local
mv $P/usr/local/include $P-dev/usr/local
# does not need license as cups loads first

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

# no locale keep smaller
rm -rf $P/usr/local/share/locale

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

# main
######
rm -rf $P/usr/local/share/man
rm -rf $P/usr/local/share/doc/cups/de
rm -rf $P/usr/local/share/doc/cups/es
rm -rf $P/usr/local/share/doc/cups/ja
rm -rf $P/usr/local/share/doc/cups/pt_BR
rm -rf $P/usr/local/share/doc/cups/ru

# dir created by cups start, perm change for ssl
rm -rf $P/var
chown root:root -R $P/usr/local/etc/cups/ssl

# 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

mkdir -p $P/usr/local/share/cups/files
mv $P/usr/local/etc/cups/cupsd.conf $P/usr/local/share/cups/files 
sed 's|localhost|127.0.0.1|' -i $P/usr/local/share/cups/files/cupsd.conf
# default left as is

# cups init
#############
# remove systemv stuff and start afresh
rm -rf $P/etc
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 ^^

# TCZ them
#######  
LIST="$P $P-dev 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
        zsyncmake $Z.tcz
done

ls -hal

echo 'Title:          cups-dev.tcz
Description:    cups devs 
Version:        2.3.3
Author:         Apple Inc
Original-site:  https://github.com/apple/cups
Copying-policy: accompanied
Size:           40K
Extension_by:   aus9
Tags:           printing
Comments:       dev files

Change-log:     2020/09/26 Original v 2.3.3 on 12x 
Current:        2020/09/26    ' > $P-dev.tcz.info

echo 'Title:          cups.tcz
Description:    printing tool 
Version:        2.3.3
Author:         Apple Inc
Original-site:  https://github.com/apple/cups
Copying-policy: accompanied
Size:           5.1M
Extension_by:   aus9
Tags:           printing
Comments:       No longer conflicts between the Cups libusb backend 
                and the usblp kernel driver. Built with libusb for 
                hopefully better usb support.
               
                optional: gutenprint or hplip TCEs

                howto:
                $ sudo /usr/local/etc/init.d/cups [start|stop|restart|status]
                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
                Do not click on "Find New Printers", first attempt click "add printer"
                To persist across boots, add the following to your backup (remove leading "/"):
                /usr/local/etc/cups/ppd/HP_OfficeJet_G85.ppd [for example]
                /usr/local/etc/cups/printers.conf
                /usr/local/etc/cups/cupsd.conf (only if you changed it)
                /etc/shadow (if you want to save to root password
 
Change-log:     2020/09/26 Original v 2.3.3 on 12x 
Current:        2020/09/26    ' > $P.tcz.info

echo 'Title:          libcups.tcz
Description:    cups library files 
Version:        2.3.3
Author:         Apple Inc
Original-site:  https://github.com/apple/cups
Copying-policy: accompanied
Size:           236K
Extension_by:   aus9
Tags:           printing
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:     2020/09/26 Original v 2.3.3 on 12x 
Current:        2020/09/26    ' > libcups.tcz.info

echo 'cups.tcz
gnutls-dev.tcz 
libusb-dev.tcz 
avahi-dev.tcz' > $P-dev.tcz.dep
echo 'libcups.tcz
dbus.tcz
cups-filters.tcz' > cups.tcz.dep
echo 'avahi.tcz
gnutls.tcz
libusb.tcz
' > libcups.tcz.dep

# cups-filters built after this so not a build dep

