#!/bin/bash
source config-file

install_pwd=$(pwd)

if [ $first_install = "y" ]; then
echo "Create minetest user"
useradd minetest
mkdir -p /home/minetest/.minetest

echo "Allow sources repo and install dependecies for build"
perl -pi.orig -0e 's/^(deb .*\n)# (deb-src)/$1$2/mg' /etc/apt/sources.list
apt update
apt-get build-dep minetest -y

fi

echo "Compile libspatialindex"
rm -rf /tmp/spatialindex
git clone https://github.com/libspatialindex/libspatialindex /tmp/spatialindex
cd /tmp/spatialindex
cmake . -DCMAKE_INSTALL_PREFIX=/usr
make -j$(nproc)
make install

echo "Compile minetest version $mt_version"
rm -rf /tmp/minetest
git clone --depth 1 --branch "$mt_version" https://github.com/minetest/minetest.git /tmp/minetest
cp -n /tmp/minetest/minetest.conf.example /home/minetest/.minetest/minetest.conf
cd /tmp/minetest
cmake . \
       -DBUILD_CLIENT=0 \
       -DBUILD_SERVER=1 \
       -DCMAKE_INSTALL_PREFIX=/usr \
       -DCUSTOM_BINDIR=/usr/bin \
       -DCUSTOM_DOCDIR="/usr/share/doc/minetest" \
       -DCUSTOM_SHAREDIR="/usr/share/minetest" \
       -DENABLE_CURL=1 \
       -DENABLE_LEVELDB=1 \
       -DENABLE_LUAJIT=1 \
       -DENABLE_REDIS=1 \
       -DENABLE_SOUND=0 \
       -DENABLE_SYSTEM_GMP=1 \
       -DENABLE_POSTGRESQL=1 \
       -DPOSTGRESQL_CONFIG_EXECUTABLE=/usr/bin/pg_config \
       -DPOSTGRESQL_LIBRARY=/usr/lib/libpq.so \
       -DRUN_IN_PLACE=0
make -j$(nproc)
echo "Stop minetestsrv service(upgrade case)"
systemctl stop minetestsrv
make install

echo "Clone minetest_game $mt_version"
mkdir -p /home/minetest/.minetest/games/
rm -rf /home/minetest/.minetest/games/minetest
git clone --depth 1 --branch "$mt_version" https://github.com/minetest/minetest_game.git /home/minetest/.minetest/games/minetest

echo "Fix privileges on /home/minetest"
chown minetest:minetest /home/minetest/ -cR

if [ $first_install = "y" ]; then

echo "Configuring firstinstall config"
echo "Creating minetestsrv systemd service"
echo "[Unit]" > /etc/systemd/system/minetestsrv.service
echo "Description=MinetestSRV" >> /etc/systemd/system/minetestsrv.service
echo "After=postgresql.service" >> /etc/systemd/system/minetestsrv.service
echo "[Service]" >> /etc/systemd/system/minetestsrv.service
echo "Type=simple" >> /etc/systemd/system/minetestsrv.service
echo "User=minetest" >> /etc/systemd/system/minetestsrv.service
echo "Group=minetest" >> /etc/systemd/system/minetestsrv.service
echo "ExecStart=/usr/bin/minetestserver" >> /etc/systemd/system/minetestsrv.service
echo "Restart=always" >> /etc/systemd/system/minetestsrv.service
echo "RestartSec=3" >> /etc/systemd/system/minetestsrv.service
echo "[Install]" >> /etc/systemd/system/minetestsrv.service
echo "WantedBy=multi-user.target" >> /etc/systemd/system/minetestsrv.service
systemctl daemon-reload

apt install postgresql postgresql-contrib -y
pg_ctlcluster 12 main start

sudo -u postgres createuser minetest
sudo -u postgres createdb minetest
sudo -u postgres createdb minetest_players
sudo -u postgres createdb minetest_auth
sudo -u postgres psql -U postgres -d postgres -c "alter user postgres with password '$psql_postgres_user_password';"
sudo -u postgres psql -U postgres -d postgres -c "alter user minetest with password '$psql_minetest_user_password';"

sudo -u postgres psql -U postgres -d postgres -c "grant all privileges on database "minetest" to minetest;"
sudo -u postgres psql -U postgres -d postgres -c "grant all privileges on database "minetest_players" to minetest;"
sudo -u postgres psql -U postgres -d postgres -c "grant all privileges on database "minetest_auth" to minetest;"

mkdir -p /home/minetest/.minetest/worlds/world/
echo "gameid = minetest" > /home/minetest/.minetest/worlds/world/world.mt
echo "backend = postgresql" >> /home/minetest/.minetest/worlds/world/world.mt
echo "creative_mode = false" >> /home/minetest/.minetest/worlds/world/world.mt
echo "enable_damage = true" >> /home/minetest/.minetest/worlds/world/world.mt
echo "player_backend = postgresql" >> /home/minetest/.minetest/worlds/world/world.mt
echo "auth_backend = postgresql" >> /home/minetest/.minetest/worlds/world/world.mt
echo "pgsql_connection = host=127.0.0.1 user=minetest password=$psql_minetest_user_password dbname=minetest" >> /home/minetest/.minetest/worlds/world/world.mt
echo "pgsql_player_connection = host=127.0.0.1 user=minetest password=$psql_minetest_user_password dbname=minetest_players" >> /home/minetest/.minetest/worlds/world/world.mt
echo "pgsql_auth_connection = host=127.0.0.1 user=minetest password=$psql_minetest_user_password dbname=minetest_auth" >> /home/minetest/.minetest/worlds/world/world.mt

cd "$install_pwd"
cat mods-list >> /home/minetest/.minetest/worlds/world/world.mt

cp -r mods /home/minetest/.minetest/mods

echo "disable_fire = true" >> /home/minetest/.minetest/minetest.conf
echo "enable_tnt = false" >> /home/minetest/.minetest/minetest.conf
echo "server_name = "$mt_server_name"" >> /home/minetest/.minetest/minetest.conf
echo "server_description = "$mt_server_description"" >> /home/minetest/.minetest/minetest.conf
echo "server_address = "$mt_server_address"" >> /home/minetest/.minetest/minetest.conf
echo "server_url = "$mt_server_url"" >> /home/minetest/.minetest/minetest.conf
echo "server_announce = "$mt_server_announce"" >> /home/minetest/.minetest/minetest.conf
echo "ipv6_server = "$mt_ipv6_server"" >> /home/minetest/.minetest/minetest.conf
echo "default_game = minetest" >> /home/minetest/.minetest/minetest.conf
echo "motd = "$mt_motd"" >> /home/minetest/.minetest/minetest.conf
echo "max_users = "$mt_max_users"" >> /home/minetest/.minetest/minetest.conf
echo "name = "$mt_admin"" >> /home/minetest/.minetest/minetest.conf
echo "language = "$mt_language"" >> /home/minetest/.minetest/minetest.conf
echo "port = "$mt_port"" >> /home/minetest/.minetest/minetest.conf
echo "enable_rollback_recording = "$mt_enable_rollback_recording"" >> /home/minetest/.minetest/minetest.conf
echo "default_privs = "$mt_default_privs"" >> /home/minetest/.minetest/minetest.conf
echo "mg_name = "$mt_mg_name"" >> /home/minetest/.minetest/minetest.conf

echo "Fix privileges on /home/minetest"
chown minetest:minetest /home/minetest/ -cR

if [ $ufw_enable = "y" ]; then
apt install ufw -y
ufw allow proto tcp to any port $ufw_ssh_port_for_accept
ufw allow proto udp to any port $mt_port
ufw enable
fi

fi
systemctl start minetestsrv
systemctl enable minetestsrv
