#! /bin/bash
## vim: noet:sw=0:sts=0:ts=4

# (C) 2016-2017 Maximilian Wende <dasisdormax@mailbox.org>
#
# This file is licensed under the Apache License 2.0. For more information,
# see the LICENSE file or visit: http://www.apache.org/licenses/LICENSE-2.0




App::generateServerConfig () {
	disclaimer () { cat <<-EOF ; }
		// This file was generated by cs2-multiserver, licensed under the Apache License 2.0
		// See https://github.com/dasisdormax/cs2-multiserver for more information

	EOF

	gamemodenames () {
		GTN="unknown"
		GMN="unknown"
		case $GAMETYPE in
			0)	GTN="classic"
				(( GAMEMODE == 0 )) && GMN="casual"
				(( GAMEMODE == 1 )) && GMN="competitive"
				(( GAMEMODE == 2 )) && GMN="scrimcomp2v2"
				(( GAMEMODE == 3 )) && GMN="scrimcomp5v5";;
			1)	GTN="gungame"
				(( GAMEMODE == 0 )) && GMN="gungameprogressive"
				(( GAMEMODE == 1 )) && GMN="gungametrbomb"
				(( GAMEMODE == 2 )) && GMN="deathmatch";;
			2)	GTN="training"
				(( GAMEMODE == 0 )) && GMN="training";;
			3)	GTN="custom"
				(( GAMEMODE == 0 )) && GMN="custom";;
			4)	GTN="cooperative"
				(( GAMEMODE == 0 )) && GMN="cooperative"
				(( GAMEMODE == 1 )) && GMN="coopmission";;
			5)  GTN="skirmish"
				(( GAMEMODE == 0 )) && GMN="skirmish";;
		esac
	}

	local CSGO_DIR="$INSTANCE_DIR/game/csgo"
	local MAPCYCLE_TXT="$CSGO_DIR/mapcycle.txt"
	local AUTOEXEC_CFG="$CSGO_DIR/cfg/autoexec.cfg"
	local SERVER_CFG="$CSGO_DIR/cfg/server.cfg"
	local LAST_CFG="$CSGO_DIR/cfg/server_last.cfg"
	local GTN
	local GMN

	######## MAPCYCLE ########
	# The map pool (and usually its order as well) when using sourcemod

	rm -f "$MAPCYCLE_TXT"
	for map in ${MAPS[@]}; do
		echo "$map" >> "$MAPCYCLE_TXT"
	done


	######## AUTOEXEC ########
	# This is executed once when starting the server

	(	disclaimer
		cat <<-EOF
			// -------- BASIC STUFF --------

			log on
			sv_password "$PASS"
			rcon_password "$RCON_PASS"

			hostname "$TITLE"

			sv_tags "$TAGS"

			// sv_lan should NEVER be set, because it disables VAC protection and
			// prevents loading of a player's inventory
			sv_lan 0

			sv_pure "$SV_PURE"
			sv_cheats "$SV_CHEATS"

			exec banned_user.cfg // Read list of banned users
		EOF

		# Conditionals
		[[ $HOSTIP           ]] && echo "hostip \"$HOSTIP\""
		[[ $HOSTPORT         ]] && echo "hostport \"$HOSTPORT\""
		[[ $SV_SPONSOR_IMAGE ]] && echo "sv_server_graphic1 \"$SV_SPONSOR_IMAGE\""
		[[ $SV_HOST_IMAGE    ]] && echo "sv_server_graphic2 \"$SV_HOST_IMAGE\""
		[[ $TV_CAMERAMAN     ]] && echo "tv_allow_camera_man_steamid \"$TV_CAMERAMAN\""

		# GOTV specific settings ####
		if (( TV_ENABLE )); then cat <<-EOF


			// -------- GOTV --------
			tv_enable 1
			tv_advertise_watchable "${TV_ADVERTISE_WATCHABLE-1}"
			tv_autorecord "${TV_AUTORECORD-0}"

			tv_password "$TV_PASS"
			tv_title "$TV_TITLE"

			tv_transmitall "$TV_TRANSMITALL"
			tv_relayvoice "$TV_RELAYVOICE"

			tv_delaymapchange 1
			tv_deltacache 2

			mapoverview_allow_client_draw 1
		EOF
		else
			TV_ENABLE=
		fi

		# Additional commands, may be set through the gamemode script
		for item in "${AUTOEXEC_CUSTOM[@]}"; do
			echo "$item"
		done
	) > "$AUTOEXEC_CFG"


	#### SERVER ####
	# This file is executed on every map change

	(	disclaimer
		cat <<-EOF
			writeid // Update banned_user.cfg
			// You could add 'writeip' here, but banning ips is generally
			// not effective, with most people having dynamic ip addressesf
		EOF

		# Additional commands, may be set through the gamemode script
		for item in "${MAPCHANGE_CUSTOM[@]}"; do
			echo "$item"
		done
	) > "$SERVER_CFG"


	#### LAST_CFG ####
	# This file is executed AFTER the default gamemode settings have been
	# loaded and can be used to override some of their settings

	(	disclaimer

		[[ $BOT_QUOTA ]] && echo "bot_quota \"$BOT_QUOTA\""
		[[ $TV_DELAY ]] && echo "tv_delay \"$TV_DELAY\""

		# Additional commands, may be set through the gamemode script
		for item in "${GAMEMODE_CUSTOM[@]}"; do
			echo "$item"
		done
	) > "$LAST_CFG"
}
