#!/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"

# BUILD GOES INTO SYSTEM FILES

P=xscreensaver
V=5.44
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc compiletc squashfs-tools zsync wget bc intltool libglade-dev libXt-dev"
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp 
# licence from https://www.jwz.org/xscreensaver/man1.html

echo 'Copyright © 1991-2015 by Jamie Zawinski. 
Permission to use, copy, modify, distribute, and sell this software and its documentation
 for any purpose is hereby granted without fee, provided that the above copyright notice 
 appear in all copies and that both that copyright notice and this permission notice appear
 in supporting documentation. No representations are made about the suitability of this 
 software for any purpose. It is provided "as is" without express or implied warranty. 
 ' > LICENCE
 
su -c "/usr/local/bin/wget -nc --no-check-certificate \
http://www.jwz.org/$P/$SRC.tar.gz \
https://www.dropbox.com/s/u02tm00wni426v5/xscreensaver.png"  $USER
tar xvf $SRC.tar.gz
mkdir $P
cd $SRC
./configure --prefix=/usr/local --enable-locking --with-gtk \
--disable-root-passwd --with-pixbuf --without-gl --with-jpeg --with-setuid-hacks
# Note: The OpenGL 3D library is not being used.
# User programs will be installed in /usr/local/bin/
# Screen savers will be installed in /usr/local/libexec/xscreensaver/
# Configuration dialogs will be installed in /usr/local/share/xscreensaver/config/
# System-wide default settings will be installed in /usr/lib/X11/app-defaults/

make -j5 # about 1 Min 30
make install-strip 
# DESTDIR=/tmp/$P ignored....but stripping ok
cd /tmp

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

# no doc
#####
rm -rf mv /usr/local/share/man 

# main
######
mkdir -p $P/usr/lib/X11/app-defaults
mv /usr/lib/X11/app-defaults/XScreenSaver $P/usr/lib/X11/app-defaults
# ^ system wide config until user changes prefs

mkdir -p $P/usr/local/share/doc/$P
mv  LICENCE $P/usr/local/share/doc/$P/

mkdir -p $P/usr/local/bin
mv /usr/local/bin/$P* $P/usr/local/bin/
# create a lock command
echo '#!/bin/sh
xscreensaver-command -lock' > $P/usr/local/bin/lockx
chmod 755 $P/usr/local/bin/lockx

mkdir -p $P/usr/local/libexec
mv /usr/local/libexec/$P $P/usr/local/libexec/

mv /usr/local/share/$P $P/usr/local/share

mkdir -p $P/usr/local/share/applications
mv /usr/local/share/applications/$P-properties.desktop $P/usr/local/share/applications/$P.desktop

# old build used xpm which are not supported
mkdir -p mkdir -p $P/usr/local/share/pixmaps
mv $P.png $P/usr/local/share/pixmaps

APP=$P/usr/local/share/applications/$P.desktop
sed 's|Screensaver|xscreensaver|' -i $APP
sed 's|DesktopSettings;Security;X-XFCE;|System;Utility;|' -i $APP
echo 'X-FullPathIcon=/usr/local/share/pixmaps/xscreensaver.png'  >> $APP

# my testing shows glitches for the following so removed from TCE
# while faint ones = not available might appear if opengl compiled
A=$P/usr/local/share/$P/config/
B=/tmp/$P/usr/local/libexec/$P
cd $A
rm -rf glitchpeg* vidwhacker* webcollage*
cd $B
rm -rf glitchpeg* vidwhacker* webcollage*
cd /tmp

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

ls -hal

echo 'Title:          xscreensaver-locale.tcz
Description:    locales
Version:        5.44
Author:         Jamie Zawinski
Original-site:  http://www.jwz.org/xscreensaver
Copying-policy: accompanied
Size:           764K	
Extension_by:   aus9 
Tags:           screensaver screen lock
Comments:       locales

Change-log:     2020/10/08 Original v 5.44
Current:        2020/10/08  ' > $P-locale.tcz.info

echo "Title:	          xscreensaver.tcz
Description:    screensavers and lock
Version:        5.44
Author:         Jamie Zawinski 
Original-site:  http://www.jwz.org/xscreensaver
Copying-policy: accompanied
Size:           4.1M
Extension-by:   aus9
Tags:           screensavers screen lock 
Comments:       Screensavers, some support setuid. Can optionally 
                lock your screen if pw set. System config at 
                /usr/lib/X11/app-defaults/XScreenSaver
                is over ridden by home config.
                    
                Mode 1 without a password= screen savers 
                Mode 2 if password set allows screen, once locked,
                to open only with correct password. Please make changes 
                at mode 1 first, so you have a home config.
                
                It is normal for the screen to fade to darkness. Before 
                darkness you can click an input device to stop the process.
                    
                1) Mode 1 has no "login" password so no lock so you 
                untick the lock box please. Run 
                $ xscreensaver-demo
                Greyed out screensavers are either opengl ones which have 
                not been compiled or glitchy screensavers I removed.
                Popup will ask to start daemon -> select Yes. 
                Choose your preferences, file is saved to
                ~/.xscreensaver Click on preview to see effects. 
                Click any kb key or mouse to stop demo.
                                        
                If after test you like it, autostart with
                echo 'xscreensaver -nosplash &' > ~/.X.d/xscreensaver
                (Above equals start the daemon)
                Optional, add that line to an existing ~/.X.d file
                ->
                $ exittc (or GUI) and exit to prompt and run
                $ startx
                Do not move mouse or kb clicks for the time you set to test.
                If you have also set rotation of screens, you then need to wait
                even longer for at least one rotation to occur.
                    
                2) Mode 2 -> After setting screensaver preferences, 
                run $ sudo passwd tc
                to setup a password for local user. Consider persistence 
                of your password by backing up /etc/shadow
                Do not move mouse or kb clicks for the time you set to test.
                Click mouse or kb -> password dialog box appears.
                
                3) Faster mode 2 lock -> run the command
                $ lockx
                
                Some window managers have their own key combo files too

Change-log:     2020/10/08 Original v 5.44
Current:        2020/10/08" > $P.tcz.info 

echo 'xscreensaver.tcz' > $P-locale.tcz.dep
echo 'gtk2.tcz' > $P.tcz.dep

