#!/bin/sh
source /etc/preinit
script_init

state_path="/var/saves/$modname"
state_file="$state_path/menu"

[ -z "$1" ] && exit 1
[ -f "$state_file" ] && prev_code="$(cat "$state_file")" || prev_code="000"
[ "$1" == "$prev_code" ] && exit 1

echo "Switching to menu $1"
gameStorage="$(findGameStorage)"
targetpath="$gameStorage/$1"
if ! containsGames "$targetpath"; then
  [ "$1" == "000" ] || exit 1
  targetpath="$gameStorage"
  containsGames "$targetpath" || exit 1
fi

mkdir -p "$state_path"
echo "$1" > "$state_file"

echo "New directory: $targetpath"
uistop
overmount_games "$1"
sleep 1
touch "/var/startmcp.flag"
