#!/bin/bash

#	Factorio Server Installation Script
#	Author: Maximilian Schubert

# Directories
source_dir=/home/factorio/install
if [ ! -d $source_dir ]
then
	printf "Factorio Source Directory not found!\n"
	exit 1
fi
install_dir=/home/factorio/server
if [ ! -d $install_dir ]
then
	printf "Factorio Install Directory not found!\n"
	exit 1
fi

# Variables
latency=0
mode=$1
version=$2
map=$3
install_warning=0
install_error=0

# Colors
cyan='\033[1;34m'
green='\033[1;32m'
yellow='\033[1;33m'
red='\033[1;31m'
nocolor='\033[0m'

case "$mode" in
install)
cd $install_dir

# Installation
printf "+------------------------------------------------\n"
printf "|   ${green}Factorio Server Installation${nocolor}\n"
printf "+------------------------------------------------\n"

# Create Server Directory
printf "|--> ${cyan}Creating Directory ... ${nocolor}"
mkdir $version 2>/dev/null
error=$?
if [ $error != 0 ]
then
	if [ $error == 1 ]
	then
		printf "		${yellow}[Warning]${nocolor}\n"
		((install_warning++))
	else
		printf "		${red}[Failed]${nocolor}\n"
		((install_error++))
	fi
else
	printf "		${green}[Done]${nocolor}\n"
fi
cd $version

# Check Server Directory
printf "|--> ${cyan}Checking Server Directory ... ${nocolor}"
if [ -d factorio ]
then
	printf "	${yellow}[Warning]${nocolor}\n"
	((install_warning++))
else
	printf "	${green}[Done]${nocolor}\n"

# Extract Files
	printf "|--> ${cyan}Extracting Files ... ${nocolor}"
	file=$(ls $source_dir/versions/$version)
	tar -xf $source_dir/versions/$version/$file 2>/dev/null
	error=$?
	if [ $error != 0 ]
	then
		printf "		${red}[Failed]${nocolor}\n"
		((install_error++))
	else
		printf "		${green}[Done]${nocolor}\n"
	fi

# Copy Base Configuration
	printf "|--> ${cyan}Copying Base Configuration ... ${nocolor}"
	cp -R $source_dir/base/* $install_dir/$version/factorio/ 2>/dev/null
	error=$?
	if [ $error != 0 ]
	then
		printf "	${red}[Failed]${nocolor}\n"
		((install_error++))
	else
		printf "	${green}[Done]${nocolor}\n"
	fi
# Create Maps-Link
	printf "|--> ${cyan}Create Maps-Link ... ${nocolor}"
	mkdir factorio/saves 2>/dev/null
	ln -s factorio/saves maps 2>/dev/null
	error=$?
	if [ $error != 0 ]
	then
		if [ $error == 1 ]
		then
			printf "		${yellow}[Warning]${nocolor}\n"
			((install_warning++))
		else
			printf "		${red}[Failed]${nocolor}\n"
			((install_error++))
		fi
	else
		printf "		${green}[Done]${nocolor}\n"
	fi
fi

# Create or Copy Map
if [ $map ]
then
	if [ -e $source_dir/maps/$map.zip ]
	then
		printf "|--> ${cyan}Copying Map ... ${nocolor}"
		if [ -e factorio/saves/$map.zip ]
		then
			printf "			${yellow}[Warning]${nocolor}\n"
			((install_warning++))
		else
			if [ ! -d factorio/saves ]
			then
				mkdir factorio/saves
			fi
			cp $source_dir/maps/$map.zip factorio/saves/$map.zip
			error=$?
			if [ $error != 0 ]
			then
				printf "			${red}[Failed]${nocolor}\n"
				((install_error++))
			else
				printf "			${green}[Done]${nocolor}\n"
			fi
		fi
	else
		printf "|--> ${cyan}Generate New Map ... ${nocolor}"
		if [ -e factorio/saves/$map.zip ]
		then
			printf "		${yellow}[Warning]${nocolor}\n"
			((install_warning++))
		else
			factorio/bin/x64/factorio --create $map 1>/dev/null 2>/dev/null
			error=$?
			if [ $error != 0 ]
			then
				printf "		${red}[Failed]${nocolor}\n"
				((install_error++))
			else
				printf "		${green}[Done]${nocolor}\n"
			fi
		fi
	fi
fi

# Installation Summary
printf "\n+------------------------------------------------\n"
printf "|   ${nocolor}Installation Summary${nocolor}\n"
printf "+------------------------------------------------\n"
printf "|--> ${yellow}Warnings: ${nocolor}$install_warning\n"
printf "|--> ${red}Errors: ${nocolor}$install_error\n"
;;
start)
# Start Factorio Server
printf "+------------------------------------------------\n"
printf "|   ${green}Starting Factorio Server${nocolor}\n"
printf "+------------------------------------------------\n"
cd $install_dir/$version
if [ $latency == 0 ]
then
	factorio/bin/x64/factorio --start-server $map
else
	factorio/bin/x64/factorio --latency $latency --start-server $map
fi
printf "+------------------------------------------------\n"
printf "|   ${red}Stopping Factorio Server${nocolor}\n"
printf "+------------------------------------------------\n"
;;
uninstall)
# Uninstall Factorio Server
printf "+------------------------------------------------\n"
printf "|   ${red}Uninstalling Factorio Server${nocolor}\n"
printf "+------------------------------------------------\n"
if [ -d $install_dir/$version ]
then
	printf "|--> ${green}$version${nocolor}\n"
	rm -rf $install_dir/$version
else
	printf "|--> ${red}Server not installed${nocolor}\n"
fi
;;
status)
# Factorio Server Status
printf "+------------------------------------------------\n"
printf "|   ${cyan}Installed Factorio Servers${nocolor}\n"
printf "+------------------------------------------------\n"
if [ "$(ls $install_dir)" ]
then
for versions in $(ls $install_dir)
do
	printf "|-- $versions\n"
	for maps in $(ls $install_dir/$versions/maps)
	do
		size=$(($(stat -c%s "$install_dir/$versions/maps/$maps")/1024))
		printf "  |-- ${maps%.zip} ($size KB)\n"
	done
	printf "\n"
done
else
	printf "|--> ${red}No Server installed${nocolor}\n"
fi
;;
*)
# Usage
printf "+------------------------------------------------\n"
printf "|   ${cyan}Factorio Installer Usage${nocolor}\n"
printf "+------------------------------------------------\n"
printf "Usage:	factorio-server (install|uninstall|start) <version> <map>\n"
printf "	factorio-server status\n"
exit 1
;;
esac
exit 0
