#!/bin/bash
echo '                         __________'
echo '__________/ DXX-Rebirth /'
install_packages libsdl-mixer1.2 libsdl-image1.2 libphysfs1 libglu1-mesa || exit 1

mkdir ~/.d2x-rebirth
cd ~/.d2x-rebirth
# rename existing files to lower case
for i in $( ls | grep [A-Z] ); do mv -f $i `echo $i | tr 'A-Z' 'a-z'`; done
if [ ! -f descent2.hog -a ! -f d2demo.hog ]; then
    echo No game content detected, downloading shareware version
    wget -q --show-progress https://web.archive.org/web/20221208193117if_/https://www.dxx-rebirth.com/download/dxx/content/descent2-pc-demo.zip || error "failed to download game content!"
    unzip descent2-pc-demo.zip
    rm descent2-pc-demo.zip
fi
wget -N -q --show-progress https://web.archive.org/web/20221208193320if_/https://www.dxx-rebirth.com/download/dxx/res/d2xr-sc55-music.dxa || error "failed to download ogg soundtrack!"
wget -N -q --show-progress https://github.com/dimag0g/dxx-rebirth/releases/download/v0.61-switch/d2x-rebirth-rpi || error "failed to download the executable!"
chmod a+x d2x-rebirth-rpi

echo "ResolutionX=640
ResolutionY=480
WindowMode=1" > descent.cfg

echo "[Desktop Entry]
Name=Descent 2
Comment=DXX-Rebirth source port of Descent 2: Counterstrike from 1996...
Exec=$HOME/.d2x-rebirth/d2x-rebirth-rpi -hogdir $HOME/.d2x-rebirth
Icon=$(dirname "$0")/icon-64.png
Terminal=false
Type=Application
Categories=Game;ActionGame;
StartupNotify=false
" > ~/.local/share/applications/d2x-rebirth.desktop || error "Failed to create menu entry!"
exit 0
