#!/bin/bash

cleanup() {
	# Cleanup
	echo -e "\nCleanup\n"
	rm -rf q3-server
	rm -rf quake3-latest-pk3s
	rm Linux.zip
	rm quake3-latest-pk3s.zip
}

trap cleanup EXIT

# Getting ioquake3
echo -e "\nGetting ioquake3 binary\n"
wget https://files.ioquake3.org/Linux.zip
unzip -q Linux.zip
mv ioquake3 ~/.q3a

# Getting pak0.pk3
echo -e "\nGetting pak0.pk3\n"
git clone https://github.com/nrempel/q3-server
mv q3-server/baseq3/pak0.pk3 ~/.q3a/baseq3/

# Getting patch data
echo -e "\nGetting patched pk3s\n"
wget https://files.ioquake3.org/quake3-latest-pk3s.zip
unzip -q quake3-latest-pk3s.zip
mv quake3-latest-pk3s/missionpack/* ~/.q3a/missionpack/
mv quake3-latest-pk3s/baseq3/* ~/.q3a/baseq3/

# Getting icon
echo -e "\nGetting icon\n"
wget https://cdn2.steamgriddb.com/icon/7dd6af0d70340195c48c002ebe5e4aac/32/256x256.png
mv 256x256.png ~/.q3a/

# Create desktop entry
echo -e "\nCreating desktop entry\n"
touch ~/.local/share/applications/ioquake3.desktop
echo "[Desktop Entry]
Type=Application
Name=Quake III Arena
Comment= Quake III Arena
Exec=/home/$(logname)/.q3a/ioquake3.x86_64
Terminal=false
Icon=/home/$(logname)/.q3a/256x256.png" > ~/.local/share/applications/ioquake3.desktop

cleanup
