#!/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 "
# attempted test on Xorg 2d not 3d results in 
# libEGL warning: MESA-LOADER: failed to open swrast: /usr/local/lib/dri/swrast_dri.so: cannot open shared object file
# locale fails to build for me so gettext-dev not loaded + --enable-nls not used
# attempt delete c h files before compile borks make so post install deletions for savers

P=xscreensaver
P2=$P-extras
V=6.09
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc bc gtk3-dev intltool imagemagick "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done
# xorg-server and others load libEGL-dev glu-dev libGL-dev

cd /tmp 
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://www.jwz.org/$P/$SRC.tar.gz \
http://tinycorelinux.net/14.x/x86_64/tcz/src/xscreensaver/COPYING "  $USER
tar xvf $SRC.tar.gz
chown root:root COPYING # already 644

cd $SRC
# fix perl pathway
sed 's|usr|usr/local|' -i hacks/check-configs.pl
sed 's|usr|usr/local|' -i hacks/webcollage  vidwhacker
sed 's|usr|usr/local|' -i hacks/vidwhacker xscreensaver-getimage-file
sed 's|usr|usr/local|' -i hacks/xscreensaver-getimage-file
sed 's|usr|usr/local|' -i hacks/xscreensaver-getimage-video
sed 's|usr|usr/local|' -i hacks/xscreensaver-text

# attempted configure for kb stuff not used ......checking whether /proc/interrupts contains keyboard data... no
# --enable-root-passwd	  Allow the root password to unlock, if not using PAM.
./configure --prefix=/usr/local --without-pam --without-systemd --without-elogind \
--with-gtk --with-xinerama-ext --with-shadow --with-randr-ext --with-xkb-ext \
--with-fontdir=/usr/local/share/fonts/xscreensaver --disable-nls --with-xft \
--with-app-defaults=/usr/local/share/X11/app-defaults --enable-root-passwd 

# locale make error 
###############
# config.status: creating po/POTFILES..config.status: creating po/Makefile..config.status: executing po/stamp-it commands
# conftest.c:60:6: warning: conflicting types for built-in function 'gettext'; expected 'char *(const char *)' [-Wbuiltin-declaration-mismatch]
# make[1]: Entering directory '/tmp/xscreensaver-6.09/po'
# make[1]: Nothing to be done for 'default'.
############# 
# Warning: Your system seems to have PAM but PAM is not being used. That is probably not going to work out well....maybe
# User programs: /usr/local/bin/
# Screen savers: /usr/local/libexec/xscreensaver/
# Configuration: /usr/local/share/xscreensaver/config/
# Extra Fonts:   /usr/local/share/fonts/xscreensaver/ # none installed
# App Defaults:  /usr/local/share/X11/app-defaults 
make -j4 # 40 seconds
# found https://forum.tinycorelinux.net/index.php/topic4350.msg28682.html#msg28682    # modified to install-strip
make install_prefix=/tmp/$P install-strip 
cd /tmp

# for reasons I know not /tmp is now populated with a lot of crud clean up YMMV
rm -rf *.c *.o

# doc
#################################
mkdir -p $P-doc/usr/local/share/
mv $P/usr/local/share/man $P-doc/usr/local/share/man/
rm -rf $P-doc/usr/local/share/man/man6/xscreensaver-systemd*
rm -rf $P-doc/usr/local/share/man/man6/sonar*
# compress 
gzip $P-doc/usr/local/share/man/man1/*
gzip $P-doc/usr/local/share/man/man6/*

# main
##################################################
# remove dbus service as we do not need dbus running IMHO
rm -rf $P/usr/local/share/$P/$P.service

# create a lock command
echo '#!/bin/sh
# WARNING password must be setup first
xscreensaver-command -lock' > $P/usr/local/bin/lock
chmod 755 $P/usr/local/bin/lock

# create a blank command
echo '#!/bin/sh
# safe for method 1 users as it does not involve lock
xscreensaver-command -activate' > $P/usr/local/bin/blank
chmod 755 $P/usr/local/bin/blank

mkdir -p $P/usr/local/share/doc/$P
cp COPYING $P/usr/local/share/doc/$P/

# remove readme to reduce unpacked size 10kb only useful for creations IMHO
rm -rf $P/usr/local/share/$P/config/README

cd $P/usr/local/share/pixmaps
convert $P.png -resize 48x48 $P.png && cd /tmp

APP1=$P/usr/local/share/applications/$P.desktop
APP2=$P/usr/local/share/applications/$P-settings.desktop
rm -rf $APP1 $APP2
# as I have been unable to compile locales supply cleaner desktops 
echo '[Desktop Entry]
Exec=xscreensaver --no-splash
Icon=xscreensaver
Name=xscreensaver
Comment=xscreensaver daemon: screen saver and locker
Type=Application
Categories=Applications;Settings;DesktopSettings;System;System Tools
Terminal=false
X-FullPathIcon=/usr/local/share/pixmaps/xscreensaver.png' > $APP1

echo '[Desktop Entry]
Exec=xscreensaver-settings
Icon=xscreensaver
Name=xscreensaver Settings
Comment=Change screensaver properties
Type=Application
Categories=Applications;Settings;DesktopSettings;System;System Tools
Terminal=false
X-FullPathIcon=/usr/local/share/pixmaps/xscreensaver.png' > $APP2
chmod 644 $APP1 $APP2

# extra TCE
#############
mkdir -p $P2/usr/local/libexec/$P
mv $P/usr/local/libexec/$P/*  $P2/usr/local/libexec/$P/
# move all and quicker to move some back....DITTO for config
mv $P2/usr/local/libexec/$P/bouboule      $P/usr/local/libexec/$P/
mv $P2/usr/local/libexec/$P/bumps         $P/usr/local/libexec/$P/
mv $P2/usr/local/libexec/$P/lcdscrub    $P/usr/local/libexec/$P/
mv $P2/usr/local/libexec/$P/xanalogtv     $P/usr/local/libexec/$P/
# next group not savers but exes so no configs
mv $P2/usr/local/libexec/$P/xscreensaver-auth            $P/usr/local/libexec/$P/
mv $P2/usr/local/libexec/$P/xscreensaver-getimage        $P/usr/local/libexec/$P/
mv $P2/usr/local/libexec/$P/xscreensaver-getimage-file   $P/usr/local/libexec/$P/
mv $P2/usr/local/libexec/$P/xscreensaver-getimage-video  $P/usr/local/libexec/$P/
mv $P2/usr/local/libexec/$P/xscreensaver-gfx             $P/usr/local/libexec/$P/
mv $P2/usr/local/libexec/$P/xscreensaver-text            $P/usr/local/libexec/$P/
mv $P2/usr/local/libexec/$P/xscreensaver-gl-visual       $P/usr/local/libexec/$P/
#################################################################################################
# build makes -rwsr-xr-x 1 root root 293752 Dec 24 08:53 xscreensaver-auth -> submit changes but I change later
chmod 755 $P/usr/local/share/xscreensaver/files/xscreensaver-auth
# using strings command against some exes
# NOTES $P-auth is launched by the xscreensaver daemon to authenticate the user by prompting for a password.
# Do not run this directly........
# NOTES $P-getimage It is used by those xscreensaver demos that operate on images
# NOTES $P-getimage-file and video and perl scripts
# NOTES $P-gfx launched by the xscreensaver daemon to manage the graphical display modes as subprocesses.
# Do not run this directly
# NOTES $P-gl-visual... This program prints out the ID of the best X visual for GL programs to use
#############################################################################################################3
mkdir -p $P2/usr/local/share/xscreensaver/config/
mv $P/usr/local/share/xscreensaver/config/* $P2/usr/local/share/xscreensaver/config/
# now move 4 back
mv $P2/usr/local/share/xscreensaver/config/bouboule.xml    $P/usr/local/share/xscreensaver/config/
mv $P2/usr/local/share/xscreensaver/config/bumps.xml       $P/usr/local/share/xscreensaver/config/
mv $P2/usr/local/share/xscreensaver/config/lcdscrub.xml  $P/usr/local/share/xscreensaver/config/
mv $P2/usr/local/share/xscreensaver/config/xanalogtv.xml   $P/usr/local/share/xscreensaver/config/

# --with-setuid-hacks Install the "sonar" demo as setuid root, which is needed in order to ping other hosts---NOPE
rm -rf $P2/usr/local/libexec/xscreensaver/sonar
rm -rf $P2/usr/local/share/xscreensaver/config/sonar.xml

# tce.installed  
################
mkdir -p $P/usr/local/share/$P/files
mv $P/usr/local/libexec/$P/$P-auth $P/usr/local/share/$P/files

mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
P=xscreensaver
# make *auth suid 
cp /usr/local/share/$P/files/$P-auth /usr/local/libexec/$P/
chmod a+s /usr/local/libexec/$P/$P-auth
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# TCZ them
###########
LIST2="$P $P-doc $P2"
for Z in $LIST2
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:	          xscreensaver-doc.tcz
Description:    lots of man pages 
Version:        6.09
Author:         Jamie Zawinski 
Original-site:  http://www.jwz.org/xscreensaver
Copying-policy: accompanied
Size:           388K
Extension-by:   aus9 @linuxquestions.org
Tags:           screensavers screen lock xorg
Comments:       "run" exes are man1 
                screensavers are man6

                I recommend you read $ man lcdscrub 
                man page pathways may not reflect my build pathways

Change-log:     2024/12/25 v 6.09 on 15x 
Current:        2024/12/25 ' > $P-doc.tcz.info
echo 'man-db.tcz ' > $P-doc.tcz.dep

echo 'Title:	          xscreensaver-extras.tcz
Description:    balance of savers 
Version:        6.09
Author:         Jamie Zawinski 
Original-site:  http://www.jwz.org/xscreensaver
Copying-policy: accompanied
Size:           26M
Extension-by:   aus9 @linuxquestions.org
Tags:           screensavers screen lock xorg
Comments:       Main TCE has 4 savers with the rest here
     
Change-log:     2024/12/25 v 6.09 on 15x
Current:        2024/12/25  ' > $P2.tcz.info
echo 'xscreensaver.tcz' >  $P2.tcz.dep

echo "Title:	          xscreensaver.tcz
Description:    screensavers and locker 
Version:        6.09
Author:         Jamie Zawinski 
Original-site:  http://www.jwz.org/xscreensaver
Copying-policy: accompanied
Size:           700K
Extension-by:   aus9 @linuxquestions.org
Tags:           screensavers screen lock xorg
Comments:       saver=TCE=xscreensaver & kb=keyboard
                (UPGRADERS) Please read all saver TCES as you may now prefer TCE=yscreensaver
                The X.d file has changed significantly for autostart as below
                This TCE has 4 savers, with balance in TCE-extras. 
                Health WARNING some savers may cause issues due to immersion, flashing etc.
                lcdscrub saver.... claims to repair burn-ins on LCD monitors
                This TCE is not designed to run on wayland desktops.  
                
                TCE fails on Xorg 2d with error missing /usr/local/lib/dri/swrast_dri.so
                So reboot into Xorg-7.7-3d and its not in TCE dep file. Default config is 
                read only /usr/local/share/X11/app-defaults/XScreenSaver unless you have 
                ~/.xscreensaver (read-write) which we will create shortly.

                Method 1 users must NOT use pulldown File -> lock nor run $ lock
                -> either will require a hardware reset as lock refers to password lock
                So if settings lock box is UNTICKED but you run $ lock....it really locks!

                $ blank is safe for method 1 users $ lock for method 2 users 
                blank and lock allows for desktop kb shortcuts for those that use them!
                eg my jwm line is <Key mask="A" key="b">exec:blank</Key>
                $ lock first blanks (may fade first) then locks 
                If you notice your screen is starting to fade, a mouse click, mouse 
                move or kb input should stop the fade/saver. I call that an INPUT-EVENT

                Method 1 = savers and no locker
                #########
                Run $ xscreensaver-settings 
                Popup asks do you want to launch the daemon now? Click launch button
                -> another popup appears - do nothing and that popup will disappear shortly. 
                The default setting for the "lock box" is already unticked.
                
                If daemon fails, $ blank will fail. You can check the daemon
                status by pulldown File, if kill daemon is in bold, its working.
                If start daemon is in bold.....start it!
                                                
                Some savers are blanked as they have not been installed or are in 
                TCE-extras and some are slow to load. Each saver has their own settings 
                button bottom RH corner. Advanced TAB includes fade, power & theme stuff.

                Test timer for fadeout/saver by clicking the minus sign to show
                Blank 1 minute. 
                Now exit the app and make no kb or mouse clicks for 1 minute. Once saver starts,
                - make an INPUT-EVENT. Leave the timer set 1 minute until you have completed all tests,
                as below.
                                        
                If you like TCE autostarted then add either this TCE or TCE-extras 
                (which has a dep of this TCE) to boot list and run
                $ echo 'sleep 10 && xscreensaver --no-splash &' > ~/.X.d/xscreensaver
                exit to prompt -> startx and check if kill daemon is in bold.
                If not, increase sleep time and try again. On my AMD APU 8s is too short
               
                If after testing you are unhappy with changes you can use pulldown
                File and kill the daemon, then uninstall the TCE and choose other options
                Experienced members can manually edit their home config but please test
                by pulldown File -> restart daemon.

                I have a tower so power management not tested. Please see TCE=acpid
                and $ man xscreensaver from TCE-doc section = LAPTOP LIDS WITHOUT SYSTEMD
                
                Mode 2 password 
                ###### READ MODE 1 FIRST PLEASE
                run $ passwd (to create password for local user) 
                I have compiled support for root password but not tested.

                Consider persistence of your password by backing up 
                etc/passwd & etc/shadow 
                Otherwise you need to repeat password creation over reboot.
                
                run $ xscreensaver-settings and complete all of mode 1 but TICK "the lock box"
                To speed up testing run $ lockx
                Method 2 users will, on kb or mouse input, see a login popup. Input password 
                within 30 seconds to unlock. The login popup wil respawn if password not accepted
                Logging is available read the man pages.
               
Change-log:     2014/01/29 v 5.26 (dentonlt)
                2020/07/26 v 5.44 (aus9)
                2020/07/31 fix desktop file 
                2023/04/10 v 6.0.6 (called 6.06 in 6.09 changelog) on 14x
Current:        2024/12/25 v 6.09 on 15x  split TCEs X.d file changes " > $P.tcz.info

readelf -d $P/usr/local/libexec/xscreensaver/xscreensaver-gl-visual  | grep 'NEEDED'
# libEGL.so.1]libEGL xorg-server.....Xorg 3d mentioned in info for boot list
# libGLU.so.1]glu
# libGL.so.1]libGL glu.....the balance named below
# rootfs has libutil.so.1 libcrypt.so.1
#[libSM.so.6] libSM libXt
#[libICE.so.6] libICE libXt
#libXft.so.2]libXft pango gtk3
#[libXt.so.6] libXt
#[libX11.so.6] libX11 cairo pango gtk3
#[libXext.so.6]libXext cairo gtk3
readelf -d $P/usr/local/bin/$P-demo | grep 'NEEDED' 
#[libXinerama.so.1] libXinerama gtk3
#[libXrandr.so.2] libXrandr gtk3
#[libXrender.so.1]libXrender libXrandr gtk3
#[libgtk-3.so.0] [libgdk-3.so.0]       gtk3
#[libpangocairo-1.0.so.0][libpango-1.0.so.0]pango gtk3
#[libharfbuzz.so.0]harfbuzz...in apps gtk3
#[libatk-1.0.so.0] at-spi2-core             gtk3
#[libcairo-gobject.so.2][libcairo.so.2]cairo pango gtk3
#[libgio-2.0.so.0][libgmodule-2.0.so.0]glib2 atk gtk3
#[libxml2.so.2] libxml2------in apps for gtk3
#[libgdk_pixbuf-2.0.so.0] gdk-pixbuf2 gtk3
#[libgobject-2.0.so.0]libglib-2.0.so.0]glib2 cairo pango gtk3
#[libXi.so.6] libXi libXrandr gtk3
readelf -d $P/usr/local/bin/$P | grep 'NEEDED' # no extras
readelf -d $P/usr/local/bin/$P-command | grep 'NEEDED' # no extras
readelf -d $P/usr/local/bin/$P-settings| grep 'NEEDED' # no extras
readelf -d $P/usr/local/share/xscreensaver/files/xscreensaver-auth # no extras
######## libexec contains huge list of savers but also some run exes
###############################################################S
cd $P/usr/local/libexec/$P
readelf -d * | grep 'NEEDED' > /tmp/readelf # 2662 lines many duplicates - show extras
#libjpeg.so.62]libjpeg-turbo....in apps gtk3
#libXxf86vm.so.1]libXxf86vm libGL Xorg-7.7-bin ( boot 3d)
# submitqc catches more depends on than my attempt use apps to check total tab confirms reduced list becomes
# REMEMBER I expect member to boot into xorg 3d for missing deps 

cd /tmp
echo 'libXt.tcz
perl5.tcz
glu.tcz
gtk3.tcz' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync *.bak

# test wbar