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

state_file="$installpath/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"
if [ "$1" == "000" ]; then
  targetpath="$rootfs$gamepath"
else
  targetpath="$rootfs$gamepath/$1"
fi

[ -d "$targetpath" ] || exit 1
echo "$1" > "$state_file"

echo "New directory: $targetpath"

if [ "$1" != "000" ]; then
  if [ -f "$rootfs$gamepath/title.fnt" ] && [ ! -f "$targetpath/title.fnt" ]; then
    ln -s "$rootfs$gamepath/title.fnt" "$targetpath/title.fnt"
  fi
  if [ -f "$rootfs$gamepath/copyright.fnt" ] && [ ! -f "$targetpath/copyright.fnt" ]; then
    ln -s "$rootfs$gamepath/copyright.fnt" "$targetpath/copyright.fnt"
  fi
fi

uistop
overmount_games

touch "/var/startmpc.flag"
