#!/bin/bash
LATEST_JSON=$(curl -L --silent "https://api.github.com/repos/RimworldTogether/Rimworld-Together/releases/latest")
RELEASES=$(curl -L --silent "https://api.github.com/repos/RimworldTogether/Rimworld-Together/releases")
MATCH=$([[ "$(uname -m)" == "x86_64" ]] && echo "Linux-x64 " || echo "Linux-ARM-x64")
if [ -z "${VERSION}" ] || [ "${VERSION}" == "latest" ]; then
DOWNLOAD_URL=$(echo ${LATEST_JSON} | jq .assets | jq -r .[].browser_download_url | grep -i ${MATCH})
else
VERSION_CHECK=$(echo ${RELEASES} | jq -r --arg VERSION "${VERSION}" '.[] | select(.tag_name==$VERSION) | .tag_name')
if [ "${VERSION}" == "${VERSION_CHECK}" ]; then
DOWNLOAD_URL=$(echo ${RELEASES} | jq -r --arg VERSION "${VERSION}" '.[] | select(.tag_name==$VERSION) | .assets[].browser_download_url' | grep -i ${MATCH})
else
echo -e "defaulting to latest release"
DOWNLOAD_URL=$(echo ${LATEST_JSON} | jq .assets | jq -r .[].browser_download_url)
fi
fi
mkdir -p /mnt/server
cd /mnt/server
curl -sSL -o server.zip ${DOWNLOAD_URL}
unzip -o server.zip
rm server.zip
chmod +x GameServer
if [ -f "/mnt/server/Core/ServerConfig.json" ]; then
echo "Config file exits"
else
mkdir -p /mnt/server/Core
echo "{\"IP\":\"0.0.0.0\",\"Port\":$SERVER_PORT,\"MaxPlayers\":$MAX_PLAYERS,\"verboseLogs\":$VERBOSE}" | jq . > /mnt/server/Core/ServerConfig.json
fi
echo "-----------------------------------------"
echo "Installation completed..."
echo "-----------------------------------------"