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

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

apt-get -y install build-essential libsdl2-dev libloadpng4-dev libflac++-dev luajit-5.1-dev liblua5.1-dev libminizip-dev liblz4-dev libpng++-dev libtbb-dev ninja-build xvfb

# Install Cortex Command
mkdir -p ${TMPDIR:-/tmp}/cortexcommand-build
cd ${TMPDIR:-/tmp}/cortexcommand-build
if [ -d cortexcommand ]; then
  rm -rf cortexcommand
fi
git clone --depth=1 https://github.com/cortex-command-community/Cortex-Command-Community-Project-Source.git
git clone --depth=1 https://github.com/cortex-command-community/Cortex-Command-Community-Project-Data.git
cd Cortex-Command-Community-Project-Source

virtualenv mesonenv
pip install meson
source mesonenv/bin/activate
meson setup --prefix=/opt/cortexcommand --buildtype=debug build
ninja -C build
ninja install -C build
deactivate
cd ..
rm -rf ${TMPDIR:-/tmp}/cortexcommand-build

# Patch startup script to get rid of weird directories
cd /opt/cortexcommand/bin
patch -l <<EOF
--- CortexCommand   2023-04-27 21:46:03.667870056 -0400
+++ CortexCommand       2023-04-27 21:46:32.457638889 -0400
@@ -10,7 +10,7 @@

 link_user_files() {
     local user_files=("LogConsole.txt" "LogLoading.txt" "LogLoadingWarning.txt" "AbortScreen.bmp" "AbortScreen.png" "Settings.ini")
-    local user_directories=("Metagames.rte" "Scenes.rte" "_ScreenShots")
+    local user_directories=("_ScreenShots")

     if ! [[ -d "\${user_data}" ]]; then
         mkdir -p "\${user_data}"
@@ -20,16 +20,6 @@
         ln -s "\${user_data}/\$file" \$tmp_dir
     done

-    if ! [[ -d "\${user_data}/Metagames.rte" ]]; then
-        mkdir -p "\${user_data}/Metagames.rte"
-        echo -e "DataModule\n\tModuleName = Metagame Saves" > "\${user_data}/Metagames.rte/Index.ini"
-    fi
-
-    if ! [[ -d "\${user_data}/Scenes.rte" ]]; then
-        mkdir -p "\${user_data}/Scenes.rte"
-        echo -e "DataModule\n\tModuleName = Saves" > "\${user_data}/Scenes.rte/Index.ini"
-    fi
-
     for directory in \${user_directories[@]}; do
         if ! [[ -d "\${user_data}/\${directory}" ]]; then
             mkdir -p "\${user_data}/\${directory}"
EOF

sudo -u ${systemuser} mkdir -p "${systemuserhome}/.local/share/Cortex Command"
echo -e "ServerSleepWhenIdle = 1\nServerSimSleepWhenIdle = 1" | sudo -u ${systemuser} tee "${systemuserhome}/.local/share/Cortex Command/Settings.ini"

# Create SystemD unit
cat > /etc/systemd/system/cortexcommand.service <<EOF
[Unit]
Description=Cortex Command server
After=network.target cortexcommand-fakex.service
BindsTo=cortexcommand-fakex.service

[Service]
Environment="DISPLAY=:100"
ExecStart=/opt/cortexcommand/bin/CortexCommand -server
Restart=on-failure
User=${systemuser}

[Install]
WantedBy=multi-user.target
EOF

cat > /etc/systemd/system/cortexcommand-fakex.service <<EOF
[Unit]
Description=Fake X server for Cortex Command server
BindsTo=cortexcommand.service

[Service]
ExecStart=/usr/bin/Xvfb :100
Restart=on-failure
User=${systemuser}

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable --now cortexcommand.service

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