#!/bin/bash
# Dependencies: obs-cli,scummvm, ...

# Scumm vars
scummvm="/usr/games/scummvm"
subs="--subtitles"
fullScreen="--no-fullscreen"
debugLevel="--debuglevel=3"
debugFlag="--debugflags=RESOURCE"
outFile="/tmp/.debugScummvm.txt"
detect="--auto-detect"
gameDir="--path=$(dirname $0)/game/Monkey_Island_2-LeChucks_Revenge-Spanish-Scummvm/MONKEY2/"
saveDir="--savepath=$(dirname $0)/game/Monkey_Island_2-LeChucks_Revenge-Spanish-Scummvm/SAVES"
extraDir="--extrapath=$(dirname $0)/game/Monkey_Island_2-LeChucks_Revenge-Spanish-Scummvm/EXTRAS"

# Obs-cli vars
obsCli="/usr/local/go/bin/obs-cli"
obsHost="--host localhost"
obsPort="--port 4444"
obsPasswd="--password lapassword"

# Script vars
skipAutoSwitch="$1" # --just-debug

function main() {
  startScummvmDebugRoom
  if [ "$skipAutoSwitch" != "--just-debug" ]; then obsSceneAutoSwitch ; fi
}

function startScummvmDebugRoom () {
  $scummvm $subs $fullScreen $debugLevel $debugFlag $detect $gameDir $saveDir $extraDir | tee $outFile | grep "ensureResourceLoaded(Room," &
}

function obsSceneAutoSwitch() {
  while :
  do
    room=$(cat $outFile | grep -i "ensureResourceLoaded(Room," | tail -1 | cut -d "," -f 2 | cut -d ")" -f 1)
    echo $room
    if [ "$room" == "$room" ]; then $obsCli $obsHost $obsPort $obsPasswd scene switch $room >/dev/null 2>&1 ; fi
    sleep 1
  done
}

main
