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

# load manually sdl2-dev
# error: exception handling disabled, use ‘-fexceptions’  same as zmusic
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe -fexceptions"
# ref https://github.com/UZDoom/UZDoom/wiki/Compilation#linux

P=uzdoom
V=4.14.3
SRC=UZDoom-4.14.3
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc git cmake ninja python3.9 bzip2-dev gtk3-dev libepoxy-dev imagemagick libvulkan-dev \
libvpx113-dev libwebp1-dev libXcomposite-dev libXcursor-dev libXinerama-dev openal125-dev sdl2-dev zmusic-dev  "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/UZDoom/UZDoom/archive/refs/tags/4.14.3.zip
unzip 4*zip
cd $SRC
mkdir build && cd build
# cmake -LAH
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_INSTALL_LIBDIR=/usr/local/lib -DCMAKE_BUILD_TYPE=Release  ../
cmake --build . --parallel 6 # 3m 10s
DESTDIR=/tmp/$P make install
cd /tmp

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

# main
######
mkdir -p $P/usr/local/share/doc/$P 
cp $SRC/LICENSE $P/usr/local/share/doc/$P/
cp $SRC/docs/licenses/*   $P/usr/local/share/doc/$P/
cp $SRC/wadsrc_widepix/static/license.md $P/usr/local/share/doc/$P/widepix-license.md

strip --strip-unneeded $P/usr/local/bin/$P

chmod 644 $P/usr/local/share/applications/org.zdoom.UZDoom.desktop \
$P/usr/local/share/icons/hicolor/scalable/apps/org.zdoom.UZDoom.svg \
$P/usr/local/share/metainfo/org.zdoom.UZDoom.metainfo.xml \
$P/usr/local/share/mime/packages/org.zdoom.UZDoom-mime.xml

mkdir -p $P/usr/local/share/pixmaps
cp $P/usr/local/share/icons/hicolor/scalable/apps/org.zdoom.UZDoom.svg $P/usr/local/share/pixmaps/
cd $P/usr/local/share/pixmaps/
convert org.zdoom.UZDoom.svg -scale 48x48 $P.png
rm -rf *.svg && cd /tmp

APP=$P/usr/local/share/applications
mv $APP/* $APP/$P.desktop
sed 's|Icon=org.zdoom.UZDoom|Icon=uzdoom|' -i $APP/$P.desktop
sed 's|StartupNotify=true|StartupNotify=false|' -i $APP/$P.desktop 
echo 'X-FullPathIcon=/usr/local/share/pixmaps/uzdoom.png' >> $APP/$P.desktop

# TCZ them
#######
LIST="$P $P-doc "
for Z in $LIST
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:          uzdoom-doc.tcz
Description:    html and txt docs
Version:        4.14.3
Author:         ZDoom,GZDoom,UZDoom Teams+ contributors
Original-site:  https://github.com/UZDoom/UZDoom
Copying-policy: accompanied
Size:           636K
Extension_by:   aus9 
Tags:           game 
Comments:       uzdoom replaces gzdoom in development
                
Change-log:     2026/01/02 v 4.14.3  on 16x
Current:        2026/01/02  '  > $P-doc.tcz.info
# no dep

echo 'Title:          uzdoom.tcz
Description:    doom like game engine to wad files
                freedoom WARNING--pixelated violence 
                Not suitable for children
Version:        4.14.3
Author:         ZDoom,GZDoom,UZDoom Teams+ contributors
Original-site:  https://github.com/UZDoom/UZDoom
Copying-policy: accompanied
Size:           21M
Extension_by:   aus9 
Tags:           game 
Comments:       Needs a minumum of Xorg-7.7-3d and best graphics load
                Xorg-7.7-3d-vulkan. Can also run on some wayland
                desktops, see the forum for comments on that.
                For graphics, I recommend you try out the settings for 
                "choose resolution presets" and click full screen if you 
                are in full screen otherwise click the window option. 
                I tested full screen on 1920x1080 on AMD APU -- YMMV

                If you want sound, test sound on TC works outside of uzdoom first.
                When you start freedoom wad, if you hear a welcome sound, congrats!
                If no game sound, please read info for openal125.  

                This TCE needs wad files to play. Be aware that you need 
                to buy certain wad files if they are closed source.
                Do that at steam for windows or linux. Keep your receipt and 
                create and inject wad under ~/.local/share/games/uzdoom/
                Or use root to create, if missing and inject under
                /usr/local/share/games/uzdoom
                freedoom wad game has been made a run dep

                For gamepad support load input-joystick-KERNEL,
                and you made need to run game as root.
                                
Change-log:     2026/01/02 v 4.14.3  on 16x
Current:        2026/01/02  '  > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED'
# libSDL2-2.0.so.0] sdl2
# libbz2.so.1.0] bzip2-lib
# libvpx.so.8] libvpx113
# libzmusic.so.1] zmusic
# libmvec.so.1] glibc_add_lib.tcz
echo 'sdl2.tcz
bzip2-lib.tcz
libvpx113.tcz
zmusic.tcz
glibc_add_lib.tcz
freedoom.tcz
openal.tcz ' > $P.tcz.dep

submitqc --libs
rm -rf *.zsync

