#!/bin/bash

cd "${datadir}" || exit

exitcode=0

echo ""
echo "Checking that all the game servers listed in serverlist.csv have a shortname-icon.png file"
for shortname in $(tail -n +2 serverlist.csv | cut -d ',' -f1); do
	# check if $shortname-icon.png exists
	if [ ! -f "gameicons/${shortname}-icon.png" ]; then
		echo "ERROR: gameicons/${shortname}-icon.png does not exist"
		exitcode=1
	else
		echo "OK: gameicons/${shortname}-icon.png exists"
	fi
done

echo ""
echo "Checking if an unexpected gameicon exists"
shopt -s nullglob
for gameiconpath in gameicons/*; do
	gameicon="$(basename "${gameiconpath}")"
	# check if $gameicon is in serverlist.csv
	if ! grep -q -E "^${gameicon%-icon.png}," serverlist.csv; then
		echo "ERROR: gameicon ${gameicon} is not in serverlist.csv"
		exitcode=1
	else
		echo "OK: gameicon ${gameicon} is in serverlist.csv"
	fi
done

echo ""
echo "Checking that the number of gameicons matches the number of servers in serverlist.csv"
gameiconcount="$(find gameicons -mindepth 1 -maxdepth 1 -type f | wc -l)"
serverlistcount="$(tail -n +2 serverlist.csv | wc -l)"
if [ "${gameiconcount}" -ne "${serverlistcount}" ]; then
	echo "ERROR: game icons (${gameiconcount}) does not match serverlist.csv ($serverlistcount)"
	exitcode=1
else
	echo "OK: gameiconcount ($gameiconcount) matches serverlistcount ($serverlistcount)"
fi

exit "${exitcode}"
