#!/bin/sh

M=${1%.map}
shift

sz=`grep '^size ' "$M.mapinfo" 2>/dev/null || true`
if [ -n "$sz" ]; then
	minimap_override="-minimap + -minmax ${sz#size }"
else
	minimap_override=
fi

mkdir -p ~/.xonotic-map-compiler-autobuild/ # to store map compile times in
tstart=`date +%s`

lasttime=`cat ~/.xonotic-map-compiler-autobuild/"${M##*/}".buildtime 2>/dev/null || true`
if [ -n "$lasttime" ] ; then
	echo "Done in approximately $((($lasttime+59)/60)) minutes."
	if [ -n "$IRCSPAM" ]; then
		echo "Done in approximately $((($lasttime+59)/60)) minutes." | $IRCSPAM >/dev/null 2>&1 || true
	fi
fi

misc/tools/xonotic-map-compiler "$M" `grep ^- "$M.map.options" | cut -d '#' -f 1` $minimap_override "$@"
status=$?

tend=`date +%s`
tdelta=$(($tend - $tstart)) # compute time
if [ $status -eq 0 ] ; then
	echo "$tdelta" > ~/.xonotic-map-compiler-autobuild/"${M##*/}".buildtime # save time in ~/.xonotic-map-compiler-autobuild/mapname
fi

exit $status
