#!/bin/bash
# LGSM fn_steaminstall function
# Author: Daniel Gibbs
# Website: http://danielgibbs.co.uk
# Version: 281214

fn_steaminstallcommand(){
echo "0x402" > .fn_steaminstall.tmp
while [ "$(grep -c 0x402 .fn_steaminstall.tmp)" -ge "1" ]; do
	counter=$((counter+1))
	if [ "${counter}" -le "10" ]; then
		if [ "${counter}" -ge "2" ]; then
			fn_printwarningnl "SteamCMD did not complete the download, retrying: Attempt ${counter}"
		fi
		./steamcmd.sh +login "${steamuser}" "${steampass}" +force_install_dir "${filesdir}" +app_update "${appid}" +quit |tee .fn_steaminstall.tmp
	elif [ "${counter}" -ge "11" ]; then
		fn_printfailurenl "SteamCMD did not complete the download, too many retrys"
		break
	fi
done
rm -f .fn_steaminstall.tmp
}

echo ""
echo "Installing ${gamename} Server"
echo "================================="
sleep 1
mkdir -pv "${filesdir}"
cd "${rootdir}/steamcmd"
fn_steaminstallcommand
if [ -z "${autoinstall}" ]; then
	echo ""
	echo "================================="
	while true; do
	read -p "Was the install successful? [y/N]" yn
		case $yn in
			[Yy]* ) break;;
			[Nn]* ) fn_retryinstall;;
			* ) echo "Please answer yes or no.";;
		esac
	done
fi