#!/bin/bash

# Downloads SteamCMD, downloads/updates the 7dtd engine

sdtdCommandUpdateengine() {
	local FORCED=no
	local CHECKONLY=no
	if [ "$1" = "--force" ]; then
		FORCED=yes
	fi

	if [ ! -e $SDTD_BASE/steamcmd ]; then
		mkdir $SDTD_BASE/steamcmd
		cd /tmp
		wget http://media.steampowered.com/installer/steamcmd_linux.tar.gz
		tar -xvzf steamcmd_linux.tar.gz -C $SDTD_BASE/steamcmd
		cd $SDTD_BASE/steamcmd
		./steamcmd.sh +quit
	fi

	if [ "$1" = "--check" -o "$2" = "--check" ]; then
		local LOCAL=$(getLocalEngineVersion)
		local REMOTE=$(getRemoteEngineVersion)
		if [ $REMOTE -gt $LOCAL ]; then
			echo "Newer engine version available."
		else
			echo "Engine up to date."
		fi
		echo "Local buildid:     $LOCAL"
		echo "Available buildid: $REMOTE"
		return
	fi

	for I in $(getInstanceList); do
		if [ $(isRunning $I) -eq 1 ]; then
			echo "At least one instance is still running (\"$I\")."
			echo "Before updating the engine please stop all instances!"
			return
		fi
	done

	local LOCAL=$(getLocalEngineVersion)
	local REMOTE=$(getRemoteEngineVersion)
	local LOCAL_SUPPORTED_BUILD=$(cat /usr/local/lib/7dtd/VERSION | grep "DediBuild" | cut -d\  -f2)

	if [ "$FORCED" = "yes" -o $REMOTE -gt $LOCAL ]; then
		echo "A newer version of the engine is available."
		echo "Local buildid:     $LOCAL"
		echo "Available buildid: $REMOTE"
		echo

		if [ "$LOCAL_SUPPORTED_BUILD" != "$REMOTE" ]; then
			echo "WARNING!!! The currently installed version of the scripts do not support the newer dedicated server build!"
			echo "If you continue you might not be able to start the server."
			echo "Please wait for an updated release of the scripts or continue at your own risk."
			echo
		fi
	
		while : ; do
			local CONTINUE
			read -p "Continue? (yn) " CONTINUE
			case $CONTINUE in
				y)
					echo "Updating..."
					break
					;;
				n)
					echo "Canceled"
					return
					;;
				*)
					echo "Wrong input"
			esac
		done

		cd $SDTD_BASE/steamcmd
		./steamcmd.sh +login $STEAM_USER $STEAM_PASS +force_install_dir $SDTD_BASE/engine "+app_update 294420" validate +quit

		cp $SDTD_BASE/engine/7DaysToDie_Data/Managed/Assembly-CSharp.dll $SDTD_BASE/engine/7DaysToDie_Data/Managed/Assembly-CSharp.dll_orig
		cp /usr/local/lib/7dtd/server-fixes/* $SDTD_BASE/engine/7DaysToDie_Data/Managed/

		chown $SDTD_USER.$SDTD_GROUP -R $SDTD_BASE/engine

		if [ "$LOCAL_SUPPORTED_BUILD" != "$REMOTE" ]; then
			echo
			echo "Also update the scripts as soon as there is a new release for this dedicated server build."
			echo
		fi
	else
		echo "Engine is already at the newest build (local: $LOCAL, remote: $REMOTE)."
	fi
}

sdtdCommandUpdateengineHelp() {
	echo "Usage: $(basename $0) updateengine [--force | --check]"
	echo
	echo "Check for a newer version of engine (aka game) files of 7dtd. If there is a newer"
	echo "version they can be updated by this command."
	echo
	echo "If --force is specified you are asked if you want to redownload the engine"
	echo "even if there is no new version available."
	echo
	echo "If --check is specified it will only output the current local and remote build ids"
	echo "and if an update is available."
}

sdtdCommandUpdateengineDescription() {
	echo "Update the 7dtd engine files"
}

sdtdCommandUpdateengineExpects() {
	case $1 in
		2)
			echo "--force --check"
			;;
	esac
}

