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

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

BALL=neverball
PUTT=Neverballutt
P=nevergames
V=1.7.0-alpha.3
SRC=$BALL-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc sdl2-dev sdl2_image-dev  sdl2_mixer-dev  sdl2_ttf-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/Neverball/$P/archive/refs/tags/$V.tar.gz    " $USER 
tar xvf $V*gz
cd $SRC
sed 's|ENABLE_FETCH := curl|ENABLE_FETCH := 0|' -i Makefile
make PREFIX=/usr/local LOCALEDIR=/usr/local/share/locale DATADIR=/usr/local/share/$P # 2m 20S
# NO make install command
cd /tmp

# doc  
#######
mkdir -p $P-doc/usr/local/share/doc/$P 
cp $SRC/doc/manual.txt $P-doc/usr/local/share/doc/$P/
cp $SRC/doc/authors.txt $P-doc/usr/local/share/doc/$P/
chmod 644 $P-doc/usr/local/share/doc/$P/* 
mkdir -p $P-doc/usr/local/share/man/man6 
cp $SRC/dist/*.6 $P-doc/usr/local/share/man/man6
gzip  $P-doc/usr/local/share/man/man6/*.6 
chmod 644 $P-doc/usr/local/share/man/man6/*.6 

# locales
#########
mkdir -p $P-locale/usr/local/share
cp -a $SRC/locale $P-locale/usr/local/share/
# leave lang/*txt in main  they are small and path usr loc share $P pathway = non-std

##################  main TCE
# COPYING
##########
mkdir -p $P/usr/local/share/doc/$P 
echo 'GPL v 2' > $P/usr/local/share/doc/$P/COPYING

# bin
#####
## mapc is used to make maps apparently
mkdir -p $P/usr/local/bin 
cp $SRC/$BALL  $P/usr/local/bin
cp $SRC/$PUTT $P/usr/local/bin
strip --strip-unneeded $P/usr/local/bin/*
chmod 755 $P/usr/local/bin/*

# pixmaps
######### already 644
mkdir -p $P/usr/local/share/pixmaps 
cp $SRC/dist/neverball_48.png $P/usr/local/share/pixmaps/$BALL.png
cp $SRC/dist/Neverballutt_48.png $P/usr/local/share/pixmaps/$PUTT.png

# desktops
########## already 644
mkdir -p $P/usr/local/share/applications 
cp $SRC/dist/*.desktop $P/usr/local/share/applications/
echo 'X-FullPathIcon=/usr/local/share/pixmaps/neverball.png'  >> $P/usr/local/share/applications/$BALL.desktop
echo 'X-FullPathIcon=/usr/local/share/pixmaps/Neverballutt.png'  >> $P/usr/local/share/applications/$PUTT.desktop

# tce.installed  script 
################
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
# apparently we need read-write dir for local user as per
# https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=neverball-git
P=nevergames
USER=`cat /etc/sysconfig/tcuser`
LOOP=/tmp/tcloop/$P/usr/local/share/$P/files
rm -rf /usr/local/share/$P && mkdir -p /usr/local/share/$P
cp -a $LOOP/* /usr/local/share/$P/
chown $USER:root -R /usr/local/share/$P
EOF
chown root:staff  -R $P/usr/local/tce.installed
chmod 775  -R $P/usr/local/tce.installed

########
# run files   
#########
mkdir -p $P/usr/local/share/$P/files
cp -a $SRC/data/*      $P/usr/local/share/$P/files
# submit finds heaps of perms lets fix dirs first then basic files and see where to go next 
find $P -type d              | xargs chmod 755
find $P -type f               | xargs chmod 644
find $P -name *.wings | xargs chmod 755   # this command is not working
sleep 5
find $P/usr/local/share/$P/files  -name *.wings | xargs chmod 755  # for reasons I know not its working

# TCZ them
###########
LIST2="$P-doc $P-locale $P"
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:          nevergames-doc.tcz
Description:    man pages and manual 
Version:        1.7.0-alpha.3
Author:         see nevergames-doc authors.txt
Original-site:  https://github.com/Neverball/neverball
Copying-policy: GPL v 2
 Size:          12K
Extension_by:   aus9 @linuxquestions.org
Tags:           game 
Comments:       man pages plus txt manual for neverball
                Manual for neverputt found.

Change-log:     2025/01/14 v 1.7.0-alpha.3 on 15x
Current:        2025/01/14    ' > $P-doc.tcz.info
echo 'man-db.tcz  ' > $P-doc.tcz.dep

echo 'Title:          nevergames-locale.tcz
Description:    language files
Version:        1.7.0-alpha.3
Author:         see nevergames-doc authors.txt
Original-site:  https://github.com/Neverball/neverball
Copying-policy: GPL v 2
 Size:          484K
Extension_by:   aus9 @linuxquestions.org
Tags:           game 
Comments:       not tested by me
                
Change-log:     2025/01/14 v 1.7.0-alpha.3 on 15x
Current:        2025/01/14    ' > $P-locale.tcz.info
echo 'nevergames.tcz  ' > $P-locale.tcz.dep

echo 'Title:          nevergames.tcz
Description:    neverball or neverputt games
Version:        1.7.0-alpha.3
Author:         see nevergames-doc authors.txt
Original-site:  https://github.com/Neverball/neverball
Copying-policy: GPL v 2
 Size:          83M
Extension_by:   aus9 @linuxquestions.org
Tags:           game xorg wayland 
Comments:       see nevergames-doc please
                Both games play on Xorg-3d, sway, labwc or weston
                Fails to run on Xorg 2d on my AMD APU 
                If Xorg 3d -  needs setup first please.
                Sound does not need setup as it uses sdl2  
                Both games look excellent. Accepts  keyboard,
                mouse, trackball or gamepad but gamepad not tested. 
                Some settings mouse only.  Games have different modes.  

                Optional dep for gamepad=input-joystick-KERNEL            

                game 1=neverball
                Tilt the floor to roll a ball through an obstacle course 
                within the given time. If the ball falls or time expires, 
                a ball is lost. Collect coins to unlock the exit and earn 
                extra balls. Red coins are  worth 5. Blue coins are worth 
                10. A ball is awarded for 100 coins. 

                game 2=Neverputt  a mini golf game.
                IMHO this is much harder.

Change-log:     2025/01/14 v 1.7.0-alpha.3 on 15x
Current:        2025/01/14    ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED'  #	 remove nevergames duplicates as its a dep
# libSDL2_ttf-2.0.so.0] sdl2_ttf
# [libvorbisfile.so.3] libvorbis
# libSDL2-2.0.so.0] sdl2 sdl2_ttf
# libGL.so.1] libGL
# [libjpeg.so.62] libjpeg-turbo
# [libpng16.so.16] libpng sdl2_ttf

echo 'sdl2_ttf.tcz
libvorbis.tcz
libGL.tcz
libjpeg-turbo.tcz  ' >  $P.tcz.dep

submitqc --libs
rm -rf *.zsync

