#!/bin/bash

# Checks for newer scripts version and downloads them

sdtdCommandUpdatescripts() {
	local LOCAL=$(cat /usr/local/lib/7dtd/VERSION | grep "Version" | cut -d\  -f2)
	local REMOTE=$(wget -qO- http://illy.bz/fi/7dtd/VERSION | grep "Version" | cut -d\  -f2)
	
	local LOCAL_BUILD=$(getLocalEngineVersion)
	local LOCAL_SUPPORTED_BUILD=$(cat /usr/local/lib/7dtd/VERSION | grep "DediBuild" | cut -d\  -f2)
	local REMOTE_SUPPORTED_BUILD=$(wget -qO- http://illy.bz/fi/7dtd/VERSION | grep "DediBuild" | cut -d\  -f2)
	
	local FORCED
	if [ "$1" = "--force" ]; then
		FORCED=yes
	else
		FORCED=no
	fi
	if [ "$FORCED" = "yes" -o $REMOTE -gt $LOCAL ]; then
		echo "A newer version of the scripts is available."
		echo "Local:     v.$LOCAL (supported dedi build: $LOCAL_SUPPORTED_BUILD)"
		echo "Available: v.$REMOTE (supported dedi build: $REMOTE_SUPPORTED_BUILD)"
		echo
		echo "Please check the release notes before continuing:"
		echo "  https://7dtd.illy.bz/wiki/Release%20Notes"
		echo
		
		if [ "$LOCAL_BUILD" != "$REMOTE_SUPPORTED_BUILD" ]; then
			echo "NOTE: The newer scripts are made for a more recent build ($REMOTE_SUPPORTED_BUILD) of the dedicated server than you are running ($LOCAL_BUILD)!"
			echo "You will have to update the engine after updating to those new scripts!"
			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
		
		wget -q http://illy.bz/fi/7dtd/management_scripts.tar.gz -O /tmp/management_scripts.tar.gz
		rm -f /usr/local/lib/7dtd/VERSION /usr/local/lib/7dtd/*.sh /usr/local/lib/7dtd/commands/* /usr/local/bin/7dtd.sh
		tar --touch --no-overwrite-dir --exclude=etc --exclude=home -xzf /tmp/management_scripts.tar.gz -C /

		chown root.root /etc/init.d/7dtd.sh
		chown root.root /etc/bash_completion.d/7dtd
		chown root.root /usr/local/bin/7dtd.sh
		chown root.root /usr/local/lib/7dtd -R
		chmod 0755 /etc/init.d/7dtd.sh
		chmod 0755 /etc/bash_completion.d/7dtd
		chmod 0755 /usr/local/bin/7dtd.sh
		chmod 0755 /usr/local/lib/7dtd -R
		
		if [ -e $SDTD_BASE/engine/7DaysToDie_Data/Managed ]; then
			cp /usr/local/lib/7dtd/server-fixes/* $SDTD_BASE/engine/ -R
			chown $SDTD_USER.$SDTD_GROUP -R $SDTD_BASE/engine/
		fi

		echo "Update done."
		echo
		echo "Note: This updated only script files. If the global config file"
		echo "/etc/7dtd.conf contains changes for the newer version or there"
		echo "were new files added to the user folder /home/sdtd those changes"
		echo "have not been applied!"

		if [ "$LOCAL_BUILD" != "$REMOTE_SUPPORTED_BUILD" ]; then
			echo
			echo "Please do now update the engine by running '7dtd.sh updateengine'"
		fi
	else
		echo "Scripts are already at the newest version (v.$LOCAL)."
	fi
}

sdtdCommandUpdatescriptsHelp() {
	echo "Usage: $(basename $0) updatescripts [--force]"
	echo
	echo "Check for a newer version of the management scripts. 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 scripts"
	echo "even if there is no new version available."
}

sdtdCommandUpdatescriptsDescription() {
	echo "Update these scripts"
}

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

