#!/bin/sh

# build_dhewm3.rpi4.sh
# 2019-12-30
# by Gernot Walzl

# Downloads, builds and installs Doom 3 on Raspbian.
# On the Raspberry Pi 4, the game renders roughly 20 fps with OpenGL.

SOURCE="dhewm3-master.zip"
DOWNLOAD="https://github.com/dhewm/dhewm3/archive/master.zip"
DEPENDS="build-essential cmake libjpeg8-dev libogg-dev libvorbis-dev libopenal-dev libsdl2-dev"
DEST="/usr/local/games/doom3"

# exit on error
set -e

# install dependencies
apt-get install $DEPENDS

# download
if [ ! -f "$SOURCE" ]; then
  wget -O "$SOURCE" "$DOWNLOAD"
fi

# extract
unzip "$SOURCE"
cd dhewm3-master
chown -R root:root .

# build
cmake neo
make

# install
mkdir -p "$DEST/base"
mkdir -p "$DEST/d3xp"
install -m 0755 base.so d3xp.so dhewm3 "$DEST"
install -m 0644 base/default.cfg "$DEST/base"
install -m 0644 neo/sys/linux/setup/image/doom3.png /usr/share/pixmaps

# launcher
cat > '/usr/local/games/dhewm3' <<EOF
#!/bin/sh
cd $DEST
./dhewm3 "\$@"
EOF
chmod +x '/usr/local/games/dhewm3'
cat > '/usr/share/applications/dhewm3.desktop' <<EOF
[Desktop Entry]
Type=Application
Name=dhewm3
Exec=dhewm3
Icon=doom3
Terminal=false
Categories=Game;
EOF

# required files
cat > "$DEST/base/CHECKSUMS.md5" <<EOF
71b8d37b2444d3d86a36fd61783844fe  pak000.pk4
4bc4f3ba04ec2b4f4837be40e840a3c1  pak001.pk4
fa84069e9642ad9aa4b49624150cc345  pak002.pk4
f22d8464997924e4913e467e7d62d5fe  pak003.pk4
38561a3c73f93f2e6fd31abf1d4e9102  pak004.pk4
2afd4ece27d36393b7538d55a345b90d  pak005.pk4
a6e7003fa9dcc75073dc02b56399b370  pak006.pk4
6319f086f930ec1618ab09b4c20c268c  pak007.pk4
28750b7841de9453eb335bad6841a2a5  pak008.pk4
EOF
cat > "$DEST/d3xp/CHECKSUMS.md5" <<EOF
a883fef0fd10aadeb73d34c462ff865d  pak000.pk4
06fc9be965e345587064056bf22236d2  pak001.pk4
EOF