From f40ad7dd6ae7d7257e3e6b6ef27a608fcd7c390a Mon Sep 17 00:00:00 2001
From: Daniel Gibbs <me@danielgibbs.co.uk>
Date: Wed, 20 May 2020 12:45:58 +0100
Subject: [PATCH] fix(update): ensure that tmpdir is cleared when update is
 found (#2890)

---
 lgsm/functions/update_factorio.sh          | 5 +++++
 lgsm/functions/update_minecraft.sh         | 5 +++++
 lgsm/functions/update_minecraft_bedrock.sh | 7 ++++++-
 lgsm/functions/update_mta.sh               | 6 +++++-
 lgsm/functions/update_mumble.sh            | 4 ++++
 lgsm/functions/update_steamcmd.sh          | 4 ++--
 lgsm/functions/update_ts3.sh               | 5 +++++
 7 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/lgsm/functions/update_factorio.sh b/lgsm/functions/update_factorio.sh
index 8110e882f..f7436d58d 100644
--- a/lgsm/functions/update_factorio.sh
+++ b/lgsm/functions/update_factorio.sh
@@ -20,6 +20,7 @@ fn_update_factorio_dl(){
 		fn_print_fail_eol_nl
 		fn_script_log_fatal "Copying to ${serverfiles}"
 		core_exit.sh
+		fn_clear_tmp
 	fi
 }
 
@@ -78,6 +79,7 @@ fn_update_factorio_compare(){
 		if [ -n "${branch}" ]; then
 			echo -e "* Branch: ${branch}"
 		fi
+		echo -en "\n"
 		fn_script_log_info "Update available"
 		fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
 		fn_script_log_info "Remote build: ${remotebuild} ${factorioarch}"
@@ -118,6 +120,7 @@ fn_update_factorio_compare(){
 		if [ -v "${branch}" ]; then
 			echo -e "* Branch: ${branch}"
 		fi
+		echo -en "\n"
 		fn_script_log_info "No update available"
 		fn_script_log_info "Local build: ${localbuild} ${factorioarch}"
 		fn_script_log_info "Remote build: ${remotebuild} ${factorioarch}"
@@ -160,9 +163,11 @@ if [ "${installer}" == "1" ]; then
 	fn_update_factorio_remotebuild
 	fn_update_factorio_dl
 else
+	fn_print_dots "Checking for update"
 	fn_print_dots "Checking for update: ${remotelocation}"
 	fn_script_log_info "Checking for update: ${remotelocation}"
 	fn_update_factorio_localbuild
 	fn_update_factorio_remotebuild
 	fn_update_factorio_compare
 fi
+core_exit.sh
diff --git a/lgsm/functions/update_minecraft.sh b/lgsm/functions/update_minecraft.sh
index b9db53ec0..8a81e0ad6 100644
--- a/lgsm/functions/update_minecraft.sh
+++ b/lgsm/functions/update_minecraft.sh
@@ -26,6 +26,7 @@ fn_update_minecraft_dl(){
 	else
 		fn_print_fail_eol_nl
 		fn_script_log_fatal "Copying to ${serverfiles}"
+		fn_clear_tmp
 		core_exit.sh
 	fi
 }
@@ -137,6 +138,7 @@ fn_update_minecraft_compare(){
 		echo -e "Update available"
 		echo -e "* Local build: ${red}${localbuild}${default}"
 		echo -e "* Remote build: ${green}${remotebuild}${default}"
+		echo -en "\n"
 		if [ -n "${branch}" ]; then
 			echo -e "* Branch: ${branch}"
 		fi
@@ -177,6 +179,7 @@ fn_update_minecraft_compare(){
 		if [ -n "${branch}" ]; then
 			echo -e "* Branch: ${branch}"
 		fi
+		echo -en "\n"
 		fn_script_log_info "No update available"
 		fn_script_log_info "Local build: ${localbuild}"
 		fn_script_log_info "Remote build: ${remotebuild}"
@@ -208,9 +211,11 @@ if [ "${installer}" == "1" ]; then
 	fn_update_minecraft_remotebuild
 	fn_update_minecraft_dl
 else
+	fn_print_dots "Checking for update"
 	fn_print_dots "Checking for update: ${remotelocation}"
 	fn_script_log_info "Checking for update: ${remotelocation}"
 	fn_update_minecraft_localbuild
 	fn_update_minecraft_remotebuild
 	fn_update_minecraft_compare
 fi
+core_exit.sh
diff --git a/lgsm/functions/update_minecraft_bedrock.sh b/lgsm/functions/update_minecraft_bedrock.sh
index 564fb5c7b..e439adcb5 100644
--- a/lgsm/functions/update_minecraft_bedrock.sh
+++ b/lgsm/functions/update_minecraft_bedrock.sh
@@ -24,6 +24,7 @@ fn_update_minecraft_dl(){
 	else
 		fn_print_fail_eol_nl
 		fn_script_log_fatal "Extracting to ${serverfiles}"
+		fn_clear_tmp
 		core_exit.sh
 	fi
 }
@@ -66,7 +67,7 @@ fn_update_minecraft_localbuild(){
 		fn_script_log_error "Missing local build info"
 		fn_script_log_error "Set localbuild to 0"
 	else
-		fn_print_ok "Checking local build: ${remotelocation}: checking local build"
+		fn_print_ok "Checking local build: ${remotelocation}"
 		fn_script_log_pass "Checking local build"
 	fi
 }
@@ -106,6 +107,7 @@ fn_update_minecraft_compare(){
 		echo -e "Update available"
 		echo -e "* Local build: ${red}${localbuild}${default}"
 		echo -e "* Remote build: ${green}${remotebuild}${default}"
+		echo -en "\n"
 		fn_script_log_info "Update available"
 		fn_script_log_info "Local build: ${localbuild}"
 		fn_script_log_info "Remote build: ${remotebuild}"
@@ -140,6 +142,7 @@ fn_update_minecraft_compare(){
 		echo -e "No update available"
 		echo -e "* Local build: ${green}${localbuild}${default}"
 		echo -e "* Remote build: ${green}${remotebuild}${default}"
+		echo -en "\n"
 		fn_script_log_info "No update available"
 		fn_script_log_info "Local build: ${localbuild}"
 		fn_script_log_info "Remote build: ${remotebuild}"
@@ -168,9 +171,11 @@ if [ "${installer}" == "1" ]; then
 	fn_update_minecraft_remotebuild
 	fn_update_minecraft_dl
 else
+	fn_print_dots "Checking for update"
 	fn_print_dots "Checking for update: ${remotelocation}"
 	fn_script_log_info "Checking for update: ${remotelocation}"
 	fn_update_minecraft_localbuild
 	fn_update_minecraft_remotebuild
 	fn_update_minecraft_compare
 fi
+core_exit.sh
diff --git a/lgsm/functions/update_mta.sh b/lgsm/functions/update_mta.sh
index 0fe79ebff..c8fad55ea 100644
--- a/lgsm/functions/update_mta.sh
+++ b/lgsm/functions/update_mta.sh
@@ -13,11 +13,11 @@ fn_update_mta_dl(){
 	echo -e "copying to ${serverfiles}...\c"
 	cp -R "${tmpdir}/multitheftauto_linux_x64/multitheftauto_linux_x64/"* "${serverfiles}"
 	local exitcode=$?
+	fn_clear_tmp
 	if [ "${exitcode}" == "0" ]; then
 		fn_print_ok_eol_nl
 		fn_script_log_pass "Copying to ${serverfiles}"
 		chmod u+x "${serverfiles}/mta-server64"
-		fn_clear_tmp
 	else
 		fn_print_fail_eol_nl
 		fn_script_log_fatal "Copying to ${serverfiles}"
@@ -138,6 +138,7 @@ fn_update_mta_compare(){
 		echo -e "Update ${mtaupdatestatus}:"
 		echo -e "* Local build: ${red}${localbuild}${default}"
 		echo -e "* Remote build: ${green}${remotebuild}${default}"
+		echo -en "\n"
 		fn_script_log_info "Update available"
 		fn_script_log_info "Local build: ${localbuild}"
 		fn_script_log_info "Remote build: ${remotebuild}"
@@ -172,6 +173,7 @@ fn_update_mta_compare(){
 		echo -e "No update available"
 		echo -e "* Local build: ${green}${localbuild}${default}"
 		echo -e "* Remote build: ${green}${remotebuild}${default}"
+		echo -en "\n"
 		fn_script_log_info "No update available"
 		fn_script_log_info "Local build: ${localbuild}"
 		fn_script_log_info "Remote build: ${remotebuild}"
@@ -200,9 +202,11 @@ if [ "${installer}" == "1" ]; then
 	fn_update_mta_remotebuild
 	fn_update_mta_dl
 else
+	fn_print_dots "Checking for update"
 	fn_print_dots "Checking for update: ${remotelocation}"
 	fn_script_log_info "Checking for update: ${remotelocation}"
 	fn_update_mta_localbuild
 	fn_update_mta_remotebuild
 	fn_update_mta_compare
 fi
+core_exit.sh
diff --git a/lgsm/functions/update_mumble.sh b/lgsm/functions/update_mumble.sh
index 652db5b89..9fe9e2596 100644
--- a/lgsm/functions/update_mumble.sh
+++ b/lgsm/functions/update_mumble.sh
@@ -19,6 +19,7 @@ fn_update_mumble_dl(){
 	else
 		fn_print_fail_eol_nl
 		fn_script_log_fatal "Copying to ${serverfiles}"
+		fn_clear_tmp
 		core_exit.sh
 	fi
 }
@@ -74,6 +75,7 @@ fn_update_mumble_compare(){
 		echo -e "Update available"
 		echo -e "* Local build: ${red}${localbuild} ${mumblearch}${default}"
 		echo -e "* Remote build: ${green}${remotebuild} ${mumblearch}${default}"
+		echo -en "\n"
 		fn_script_log_info "Update available"
 		fn_script_log_info "Local build: ${localbuild} ${mumblearch}"
 		fn_script_log_info "Remote build: ${remotebuild} ${mumblearch}"
@@ -108,6 +110,7 @@ fn_update_mumble_compare(){
 		echo -e "No update available"
 		echo -e "* Local build: ${green}${localbuild} ${mumblearch}${default}"
 		echo -e "* Remote build: ${green}${remotebuild} ${mumblearch}${default}"
+		echo -en "\n"
 		fn_script_log_info "No update available"
 		fn_script_log_info "Local build: ${localbuild} ${mumblearch}"
 		fn_script_log_info "Remote build: ${remotebuild} ${mumblearch}"
@@ -145,3 +148,4 @@ else
 	fn_update_mumble_remotebuild
 	fn_update_mumble_compare
 fi
+core_exit.sh
diff --git a/lgsm/functions/update_steamcmd.sh b/lgsm/functions/update_steamcmd.sh
index 5000b5e3d..b22569d1a 100644
--- a/lgsm/functions/update_steamcmd.sh
+++ b/lgsm/functions/update_steamcmd.sh
@@ -130,11 +130,10 @@ fn_update_steamcmd_compare(){
 			command_stop.sh
 			exitbypass=1
 			fn_update_steamcmd_dl
-			date +%s > "${lockdir}/lastupdate.lock"
 			exitbypass=1
 			command_start.sh
 		fi
-
+		date +%s > "${lockdir}/lastupdate.lock"
 		alert="update"
 		alert.sh
 	else
@@ -247,3 +246,4 @@ else
 	fn_update_steamcmd_remotebuild
 	fn_update_steamcmd_compare
 fi
+core_exit.sh
diff --git a/lgsm/functions/update_ts3.sh b/lgsm/functions/update_ts3.sh
index fbabd3d83..87c8a86da 100644
--- a/lgsm/functions/update_ts3.sh
+++ b/lgsm/functions/update_ts3.sh
@@ -24,6 +24,7 @@ fn_update_ts3_dl(){
 	else
 		fn_print_fail_eol_nl
 		fn_script_log_fatal "Copying to ${serverfiles}"
+		fn_clear_tmp
 		core_exit.sh
 	fi
 }
@@ -161,6 +162,7 @@ fn_update_ts3_compare(){
 			exitbypass=1
 			command_start.sh
 		fi
+		date +%s > "${lockdir}/lastupdate.lock"
 		alert="update"
 		alert.sh
 	else
@@ -169,6 +171,7 @@ fn_update_ts3_compare(){
 		echo -e "No update available"
 		echo -e "* Local build: ${green}${localbuild}${default}"
 		echo -e "* Remote build: ${green}${remotebuild}${default}"
+		echo -en "\n"
 		fn_script_log_info "No update available"
 		fn_script_log_info "Local build: ${localbuild}"
 		fn_script_log_info "Remote build: ${remotebuild}"
@@ -209,9 +212,11 @@ if [ "${installer}" == "1" ]; then
 	fn_update_ts3_remotebuild
 	fn_update_ts3_dl
 else
+	fn_print_dots "Checking for update"
 	fn_print_dots "Checking for update: ${remotelocation}"
 	fn_script_log_info "Checking for update: ${remotelocation}"
 	fn_update_ts3_localbuild
 	fn_update_ts3_remotebuild
 	fn_update_ts3_compare
 fi
+core_exit.sh