#!/bin/bash

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2025-present ArchR (https://github.com/archr-linux/Arch-R)

. /etc/profile

if [ -n "${1}" ]; then
  # Make sure sdl2text dir exists
  if [ ! -d "/storage/.config/sdl2text" ]; then
    mkdir -p "/storage/.config/sdl2text"
  fi

  # Check if guide exists for this game and output to temp path
  GAME_GUIDE_CHECK="${1%.*}.txt"

  if [ -f "${GAME_GUIDE_CHECK}" ]; then
cat <<EOF >/storage/.config/sdl2text/game_guide.path
${GAME_GUIDE_CHECK}
EOF
 else
    rm -r "/storage/.config/sdl2text/game_guide.path"
  fi
else
  # Show game guide if exists
  if [ -f "/storage/.config/sdl2text/game_guide.path" ]; then
    GAME_GUIDE_PATH="$(cat /storage/.config/sdl2text/game_guide.path)"
    /usr/bin/sdl2text  "${GAME_GUIDE_PATH}"
  else
    exit 1
  fi
fi
