#!/bin/sh

set -eu

IS_LOG="${AUTOPKGTEST_ARTIFACTS}/is_savegames.log"

NOSOUND=1
export NOSOUND

is > "${IS_LOG}" &
#IS_PID=$!

# click begin new game
xdotool search --sync --all --onlyvisible --name Ironseed \
  windowfocus --sync %1 \
  set_window --name "is_startup" \
  key --clearmodifiers Escape \
  mousemove --window %1 200 350 click --clearmodifiers 1

# screenshot
xwd -silent -root -out "${AUTOPKGTEST_ARTIFACTS}/savegames_1.xwd"
  
# complete crewgen and save in slot1
xdotool search --sync --all --onlyvisible --name Ironseed \
  windowfocus --sync %1 \
  set_window --name "is_crewgen" \
  mousemove --window %1 580 350 click --clearmodifiers 1 \
  sleep 3 \
  key --clearmodifiers --delay 500 Return \
  Return Return Return Return Return Return Return Return \
  1 Return T Return

# screenshot
xwd -silent -root -out "${AUTOPKGTEST_ARTIFACTS}/savegames_2.xwd"

# start game and save in slot2, then exit the game
xdotool search --sync --all --onlyvisible --name Ironseed \
  windowfocus --sync %1 \
  key --clearmodifiers --delay 500 Escape \
  b e 2 Return X Return \
  alt+x y

# screenshot
xwd -silent -root -out "${AUTOPKGTEST_ARTIFACTS}/savegames_3.xwd"

# wait for game to finish and cleanup
if grep -vi '^ironseed' "${IS_LOG}"
then
	echo >&2 "Unrecognized game output"
	exit 21
fi

wait
