FactorioScripts/start_server.sh

56 lines
2.1 KiB
Bash
Raw Normal View History

2023-08-13 15:45:11 -04:00
#!/usr/bin/env bash
2023-08-14 12:21:54 -04:00
# If config.sh exists, fetch variables from it
if [ -f "config.sh" ]; then
source config.sh
echo "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 "WARNING: config.sh file does not exist, falling back on default configuration."
source config.sh.example
echo "Loaded configuration from config.example.sh"
else
# Handle the scenario when both files are missing
echo "FATAL: Configuration files are missing! Cannot start the Factorio server."
exit 1
fi
fi
# Retrieves the script's directory and cd's into it.
script_dir="$(cd "$(dirname "$0")" && pwd)"
cd ${script_dir}
2023-08-13 15:45:11 -04:00
2023-08-14 12:05:57 -04:00
# 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.
2023-08-14 12:21:54 -04:00
echo "Updating scripts..."
2023-08-14 12:05:57 -04:00
git pull
2023-08-14 12:21:54 -04:00
echo "Scripts updated!"
2023-08-14 12:25:18 -04:00
echo "Server must be restarted for script updates to take effect."
2023-08-14 12:05:57 -04:00
else
echo "WARNING: This directory is not a Git repository. Script updates will not be automatically fetched."
fi
2023-08-13 15:45:11 -04:00
# This handles automatically updating the Factorio binary.
2023-08-14 12:21:54 -04:00
echo "Fetching Factorio binary..."
2023-08-13 15:45:11 -04:00
wget https://factorio.com/get-download/stable/headless/linux64 -O factorio_headless.tar.xz
2023-08-14 12:21:54 -04:00
echo "Extracting Factorio binary..."
2023-08-13 15:45:11 -04:00
tar -xf factorio_headless.tar.xz --strip-components=1 --overwrite
2023-08-14 12:21:54 -04:00
echo "Factorio binary updated!"
2023-08-13 15:45:11 -04:00
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.
2023-08-14 12:05:57 -04:00
byobu attach -t factorio