#!/bin/sh
# Collection of scripts to deploy a server hosting several open-source games
# Copyright (C) 2022  Jarno van der Kolk
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

set -e

echo "Installing SuperTuxKart ${stk_version}"

apt-get install --assume-yes build-essential cmake libbluetooth-dev libsdl2-dev \
libcurl4-openssl-dev libenet-dev libfreetype6-dev libharfbuzz-dev \
libjpeg-dev libogg-dev libopenal-dev libpng-dev \
libssl-dev libvorbis-dev libmbedtls-dev pkg-config zlib1g-dev subversion

if [ -e /etc/systemd/system/supertuxkart.service ]; then
  systemctl stop supertuxkart
fi

if [ -z ${stk_version} ] || [ "${stk_version}" = "latest" ]; then
  stk_version=$(curl -s https://api.github.com/repos/supertuxkart/stk-code/releases/latest | jq -r '.["tag_name"]')
fi

# Install SuperTuxKart
stk_dir="/opt/SuperTuxKart-${stk_version}"
mkdir -p ${stk_dir}
curl --location "https://github.com/supertuxkart/stk-code/releases/download/${stk_version}/SuperTuxKart-${stk_version}-linux-x86_64.tar.xz" | tar --extract --xz --no-same-owner --strip-components=1 --directory=${stk_dir}

builddir=""${TMPDIR:-/tmp}/stk-build""
if [ -d "$builddir" ]; then
  rm -rf "$builddir"
fi
mkdir -p "$builddir"
curl --location "https://github.com/supertuxkart/stk-code/archive/refs/tags/${stk_version}.tar.gz" | tar --extract --gz --no-same-owner --directory=$builddir
svn co https://svn.code.sf.net/p/supertuxkart/code/stk-assets ${builddir}/stk-assets
patch -p1 -d "$builddir"/stk-code-${stk_version}/ < "$(dirname $0)"/../configs/stkranking.patch
mkdir -p "$builddir"/build
cd "$builddir"/build
cmake ../stk-code-${stk_version}/ -DCMAKE_INSTALL_PREFIX=/opt/SuperTuxKart-${stk_version}/ -DSERVER_ONLY=On
make -j 2
make install
rm -rf "$builddir"

ln -s ${stk_dir}/bin/supertuxkart /usr/games/supertuxkart

# Configuration
mkdir -p /etc/supertuxkart
cp "$(dirname $0)"/../configs/supertuxkart.xml /etc/supertuxkart/supertuxkart.xml
touch /etc/supertuxkart/stkservers.db
chown -R ${systemuser}: /etc/supertuxkart

# Create SystemD unit
cat > /etc/systemd/system/supertuxkart.service <<EOF
[Unit]
Description=SuperTuxKart server
After=network.target

[Service]
ExecStart=${stk_dir}/bin/supertuxkart --server-config=/etc/supertuxkart/supertuxkart.xml --lan-server=onFOSS
Restart=on-failure
User=${systemuser}

[Install]
WantedBy=multi-user.target
EOF

# SuperTuxKart scoring
cp "$(dirname $0)"/../services/supertuxkartscores.py ${stk_dir}

cat > /etc/nginx/gameserver.d/supertuxkart.conf <<EOF
location /dynamic/supertuxkartscore.json {
    proxy_pass http://localhost:9985/;
}
EOF

cat > /etc/systemd/system/supertuxkartscores.service <<EOF
[Unit]
Description=SuperTuxKart score service
After=network.target supertuxkart.service

[Service]
ExecStart=${stk_dir}/supertuxkartscores.py
Restart=on-failure
User=${systemuser}

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now supertuxkart.service supertuxkartscores.service

# Add firewall rules
firewall-cmd --zone=public --add-port=2757/udp --permanent
firewall-cmd --zone=public --add-port=2759/udp --permanent
