#!/bin/bash

#	Factorio Server Installation Script
#	Author: Maximilian Schubert
#	Version: 1.1

# 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 [ -e bin/x64/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
	mv factorio/* .
	rm -rf factorio
	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/ 2>/dev/null
	error=$?
	if [ $error != 0 ]
	then
		printf "	${red}[Failed]${nocolor}\n"
		((install_error++))
	else
		printf "	${green}[Done]${nocolor}\n"
	fi
fi
;;
start)
# Start Factorio Server
printf "+------------------------------------------------\n"
printf "|   ${green}Starting Factorio Server${nocolor}\n"
printf "+------------------------------------------------\n"
cd $install_dir/$version
if [ $latency == 0 ]
then
	bin/x64/factorio --start-server $map
	error=$?
else
	bin/x64/factorio --latency $latency --start-server $map
	error=$?
fi
printf "+------------------------------------------------\n"
printf "|   ${red}Stopping Factorio Server${nocolor}\n"
printf "+------------------------------------------------\n"
if [ $error != 0 ]
then
	((install_warning++))
fi
;;
uninstall)
# Uninstall Factorio Server
printf "+------------------------------------------------\n"
printf "|   ${red}Uninstalling Factorio Server${nocolor}\n"
printf "+------------------------------------------------\n"
if [ -d $install_dir/$version ]
then
	printf "|--> ${cyan}$version has been uninstalled.${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/saves)
	do
		size=$(($(stat -c%s "$install_dir/$versions/saves/$maps")/1024))
		printf "  |-- ${maps%.zip} ($size KB)\n"
	done
done
else
	printf "|--> ${red}No Server installed${nocolor}\n"
fi
;;
create)
# Create or Copy Map
printf "+------------------------------------------------\n"
printf "|   ${cyan}Create New Map${nocolor}\n"
printf "+------------------------------------------------\n"
cd $install_dir/$version
if [ $map ]
then
	if [ -e $source_dir/maps/$map.zip ]
	then
		printf "|--> ${cyan}Copying Map ... ${nocolor}"
		if [ -e saves/$map.zip ]
		then
			printf "			${yellow}[Warning]${nocolor}\n"
			((install_warning++))
		else
			if [ ! -d saves ]
			then
				mkdir saves
			fi
			cp $source_dir/saves/$map.zip 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 saves/$map.zip ]
		then
			printf "		${yellow}[Warning]${nocolor}\n"
			((install_warning++))
		else
			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
;;
delete)
cd $install_dir/$version
printf "+------------------------------------------------\n"
printf "|   ${cyan}Delete Map${nocolor}\n"
printf "+------------------------------------------------\n"
printf "|--> ${cyan}Delete Map ... ${nocolor}"
if [ -e saves/$map.zip ]
then
	rm -rf saves/$map.zip
	error=$?
	if [ $error != 0 ]
	then
		printf "		${red}[Failed]${nocolor}\n"
		((install_error++))
	else
		printf "		${green}[Done]${nocolor}\n"
	fi
else
	printf "		${yellow}[Warning]${nocolor}\n"
	((install_warning++))
fi
;;
*)
# Usage
printf "+------------------------------------------------\n"
printf "|   ${cyan}Factorio Installer Usage${nocolor}\n"
printf "+------------------------------------------------\n"
printf "Usage:	factorio-server install|uninstall <version>\n"
printf "			start|create|delete <version> <map>\n"
printf "			status\n"
exit 1
;;
esac

# Installation Summary
printf "\n+------------------------------------------------\n"
printf "|   ${nocolor}Summary${nocolor}\n"
printf "+------------------------------------------------\n"
printf "|--> ${yellow}Warnings: ${nocolor}$install_warning\n"
printf "|--> ${red}Errors: ${nocolor}$install_error\n"
exit 0
