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

export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=xscreensaver
V=6.06
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc bc gtk3-dev intltool libglade-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-2022 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. 
 ' > COPYING
 
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://www.jwz.org/$P/$SRC.tar.gz \
http://tinycorelinux.net/11.x/x86_64/tcz/src/$P/$P.png"  $USER
tar xvf $SRC.tar.gz
cd $SRC
# gtk2->3 Warning: GTK was found, but gtk+-3.0 was not
# --without-gl -> --without-opengl is not supported
./configure --prefix=/usr/local --enable-nls --without-pam --without-gles \
--without-systemd --without-elogind 
#############   
# 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/lib/X11/app-defaults/ # claims v 6.05 no 6.06
make -j5 # about 1minute
make install-strip 
# make install-strip DESTDIR=/tmp/$P still fails 2023
cd /tmp

# locale fails
#########
# mkdir -p $P-locale/usr/local/share/
# mv /usr/local/share/locale $P-locale/usr/local/share
# for now Juanito allows 11x locales

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

# delete libEGL savers unable to stop at compile as above
#####################
LISTEGL="antinspect antmaze antspotlight atlantis atunnel \
beats blinkbox blocktube boing bouncingcow boxed bubble3d \
cage carousel chompytower circuit cityflow companioncube \
covid19 crackberg crumbler cube21 cubenetic cubestack cubestorm \
cubetwist cubicgrid dangerball deepstars discoball dymaxionmap \
endgame energystream engine esper etruscanvenus flipflop \
flipscreen3d fliptext flurry flyingtoasters gears geodesic \
geodesicgears gflux gibson glblur glcells gleidescope glhanoi \
glknots glmatrix glplanet glschool glslideshow glsnake gltext \
gravitywell handsy headroom hexstrut hextrail hilbert hopalong \
hydrostat hypnowheel hypertorus jigglypuff jigsaw juggler3d \
kaleidocycle kaleidescope klein lament lavalite lockward mapscroller \
maze3d menger mirrorblob moebius moebiusgears molecule morph3d nakagin \
noof peepers photopile pinion pipes polyhedra polytopes projectiveplane \
providence pulsar quasicrystal queens raverhoop razzledazzle romanboy \
rubik rubikblocks sballs sierpinski3d skytentacles sonar sphereeversion \
spheremonics splitflap splodesic sproingies squirtorus stairs starwars \
stonerview superquadrics surfaces tangram timetunnel topblock tronbit \
unicrud unknownpleasures vigilance voronoi winduprobot  "
for Z in $LISTEGL
do
	rm -rf  /usr/local/share/$P/config/$Z.xml 
        rm -rf  /usr/local/libexec/$P/$Z 
done

# testing shows glitches for the following so delete
########################
A=/usr/local/share/$P/config/
B=/usr/local/libexec/$P
cd $A
rm -rf blitspin* glitchpeg* rdbomb* ripples* rotzoomer* vidwhacker* webcollage*
cd $B
rm -rf blitspin glitchpeg rdbomb ripples rotzoomer vidwhacker webcollage*
cd /tmp

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

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

mkdir -p $P/usr/local/share/doc/$P
mv COPYING $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
# remove readme to reduce unpacked size 10kb, only useful for creations IMHO
rm -rf $P/usr/local/share/xscreensaver/config/README

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

mkdir -p mkdir -p $P/usr/local/share/pixmaps
mv $P.png $P/usr/local/share/pixmaps

APP1=$P/usr/local/share/applications/$P.desktop
APP2=$P/usr/local/share/applications/$P-settings.desktop
sed 's|XScreenSaver|xscreensaver|g' -i $APP1
sed 's|XScreenSaver|xscreensaver|g' -i $APP1
sed 's|Screensaver;Security|System;Utility|' -i $APP1
sed 's|Screensaver;Security|System;Utility|' -i $APP2
echo 'X-FullPathIcon=/usr/local/share/pixmaps/xscreensaver.png'  >> $APP1
echo 'X-FullPathIcon=/usr/local/share/pixmaps/xscreensaver.png'  >> $APP2

# error early test -> To prevent the kernel from randomly unlocking, your screen via the out-of-memory killer,
# "xscreensaver-auth" must be setuid root.
# SUID
######
chmod a+s $P/usr/local/libexec/xscreensaver/xscreensaver-auth

# TCZ them
###########
LIST2="$P "
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

cat >> $P.tcz.info <<'EOF'
Title:	          xscreensaver.tcz
Description:    screensavers and lock
Version:        6.06
Author:         Jamie Zawinski 
Original-site:  http://www.jwz.org/xscreensaver
Copying-policy: accompanied
Size:           4.3M
Extension-by:   aus9
Tags            screensavers screen lock 
Comments:       Online manual
                https://www.jwz.org/xscreensaver/man1.html
                
                System config is /usr/lib/X11/app-defaults/XScreenSaver
                is over ridden by home config, when you make a preference change.
                Most greyed out screensavers are opengl ones which have 
                not been compiled or glitchy screensavers I have removed.
                Ignore any terminal output about missing libGLU.so.1
                as its a dep of deleted opengl savers please.
                                    
                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 (but can be changed) for the screen to fade 
                to darkness. Before darkness you can click an input device 
                to stop that process.
                    
                1) Mode 1 has no "login" password so no lock so you 
                untick the lock box please. Run 
                $ xscreensaver-demo
                                
                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 it with
                echo 'xscreensaver -nosplash &' > ~/.X.d/xscreensaver
                                    
                2) Mode 2 -> After setting screensaver preferences, 
                run $ passwd  
                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 input box appears.
                
                3) Faster mode 2 lock -> run the command
                $ lockx

                SpeedMine & VFeedback about 10 seconds to display
                                
Change-log:     2014/01/29 v 5.26 (dentonlt)
                2020/07/26 v 5.44 (aus9)
                2020/07/31 fix desktop file 
Current:        2023/04/10 v 6.0.6           
EOF

readelf -d $P/usr/local/bin/$P-demo | grep 'NEEDED' # showing only extras
#[libSM.so.6] libSM libXt
#[libICE.so.6] libICE "
#[libXinerama.so.1] libXinerama gtk3
#[libXrandr.so.2] libXrandr     "
#[libXrender.so.1]libXrenderlibXrandr gtk3
#[libgtk-3.so.0]                       "
#[libgdk-3.so.0]                       "
#[libz.so.1]              TCB
#[libpangocairo-1.0.so.0] pango gtk3
#[libpango-1.0.so.0]              "
#[libharfbuzz.so.0]        harfbuzz
#[libatk-1.0.so.0]        atk gtk3
#[libcairo-gobject.so.2] cairo "
#[libcairo.so.2]          "
#[libgio-2.0.so.0]   glib2 atk gtk3
#[libgmodule-2.0.so.0]          "
#[libxml2.so.2]            libxml2
#[libgdk_pixbuf-2.0.so.0] gdk-pixbuf2 gtk3
#[libgobject-2.0.so.0] glib2           "
#[libglib-2.0.so.0]                    "
#[libXt.so.6]               libXt
#[libX11.so.6] libX11         "
#[libXext.so.6] libXext cairo gtk3
#[libXi.so.6] libXi            "
#[libpthread.so.0] TCB
#[libc.so.6] TCB

echo 'libXt.tcz
gtk3.tcz
harfbuzz.tcz
libxml2.tcz 
libglade.tcz' > $P.tcz.dep

submitqc --libs

