FactorioScripts/start_server.sh

70 lines
2.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# Define color variables
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
RESET='\033[0m'
# Retrieves the script's directory and cd's into it.
script_dir="$(cd "$(dirname "$0")" && pwd)"
cd ${script_dir}
# Check if the current directory is inside a Git repository.
if [ -d .git ] || git rev-parse --is-inside-work-tree > /dev/null 2>&1 ; then
# Pull updated scripts from CoastalCommits.
echo -e "${GREEN}Updating scripts..."
git pull
echo -e "${GREEN}Scripts updated!"
echo -e "${GREEN}Server must be restarted for script updates to take effect."
else
echo -e "${RED}WARNING: This directory is not a Git repository. Script updates will not be automatically fetched."
fi
# If config.sh exists, fetch variables from it
if [ -f "config.sh" ]; then
source config.sh
configpath="config.sh"
echo -e "${GREEN}Loaded configuration from config.sh"
else
# If config.sh doesn't exist, check if config.sh.example exists
if [ -f "config.sh.example" ]; then
# Fetch variables from config.sh.example
echo -e "${RED}WARNING: config.sh file does not exist, falling back on default configuration."
source config.sh.example
configpath="config.sh.example"
echo -e "${YELLOW}Loaded configuration from config.sh.example"
else
# Handle the scenario when both files are missing
echo -e "${RED}FATAL: Configuration files are missing! Cannot start the Factorio server."
exit 1
fi
fi
# This handles automatically updating the Factorio binary.
echo -e "${GREEN}Fetching Factorio binary..."
wget "https://factorio.com/get-download/${version}/headless/linux64" -O factorio_headless.tar.xz
if [ $? -ne 0 ]; then
echo -e "${RED}FATAL: There is not a headless linux64 build available for the version you've selected in your ${YELLOW}${configpath} ${RED}file."
echo -e "${RED}See this website for a list of all Factorio versions: https://factorio.com/download/archive/"
rm -f factorio_headless.tar.xz
exit 1
fi
echo -e "${GREEN}Extracting Factorio binary..."
tar -xf factorio_headless.tar.xz --strip-components=1 --overwrite
echo -e "${GREEN}Factorio binary updated!"
rm -f factorio_headless.tar.xz
# This checks if the ./saves/$save_name.zip save file exists or not...
if [ ! -f "./saves/${save_name}.zip" ]; then
# ...if not, create the save file
./bin/x64/factorio ${extra_params_create} --create "./saves/${save_name}.zip"
fi
# This creates a byobu session that starts the server.
byobu new-session -d -s factorio "./bin/x64/factorio ${extra_params_start_server} --start-server ./saves/${save_name}.zip"
# This attaches the current terminal to the byobu session.
# Comment this out if you don't want this functionality.
byobu attach -t factorio