#!/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

apt install --assume-yes fuse3

echo "Installing Wasted ${wasted_version}"

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

# Install Wasted
wasted_dir="/opt/Wasted-${wasted_version}"
mkdir -p ${wasted_dir}
curl --location "https://codeberg.org/wastedgames/wasted-releases/raw/branch/main/${wasted_version}/wasted-64bit.AppImage" > "${wasted_dir}"/wasted-64bit.AppImage
chmod +x "${wasted_dir}"/wasted-64bit.AppImage

# Configuration
mkdir -p /etc/wasted
chown -R ${systemuser}: /etc/wasted

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

[Service]
ExecStart="${wasted_dir}"/wasted-64bit.AppImage --server-config=/etc/wasted/wasted.xml --lan-server=onFOSS
Restart=on-failure
User=${systemuser}

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
#systemctl enable --now wasted.service

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