#!/bin/bash

GAME="Team Fortress 2"
GAME_ID=440
HL2_SYSTEM=$(find $HOME/.steam/root/SteamApps -name "$GAME" -type d)
HL2_MOD=tf
STEAM=$HOME/.steam/root/ubuntu12_32
LOG=/tmp/$(echo $GAME|tr ' ' _).log

[ -d "$HL2_SYSTEM"/$HL2_MOD ] || exit

cd "$HL2_SYSTEM"/$HL2_MOD

declare -a DEMOS=($(ls *.dem|sed s/.dem$// 2>/dev/null|sort))

DEMO=$(dialog --stdout --single-quoted --title "$GAME Benchmark" --menu "\nSelect demo..." 0 0 0 \
	$(echo ${DEMOS[@]}|tr ' ' '\n'|nl)
     )

[ $? -eq 0 ] || exit

cd "$HL2_SYSTEM"

echo $GAME_ID >steam_appid.txt

rm -f $LOG

# direct start does not always work, but usually it works when indirect start worked once - needs to be verified

# variant without overlay

LD_LIBRARY_PATH=$STEAM:bin __GL_THREADED_OPTIMIZATIONS=1 ./hl2_linux -game $HL2_MOD +con_logfile $LOG +cl_showfps 1 +timedemoquit ${DEMOS[$((DEMO-1))]}

# variant with overlay

#LD_LIBRARY_PATH=$STEAM:bin LD_PRELOAD=$STEAM/gameoverlayrenderer.so __GL_THREADED_OPTIMIZATIONS=1 ./hl2_linux -game $HL2_MOD +con_logfile $LOG +cl_showfps 1 +timedemoquit ${DEMOS[$((DEMO-1))]}

rm -f steam_appid.txt

RESULT=$(grep frames $LOG)
dialog --stdout --single-quoted --title "$GAME Benchmark" --msgbox "\nResult: $RESULT\n\n" 0 0
