#!/bin/bash

# omarchy:summary=Remove a game from ~/Games/vibes
# omarchy:args=[game]
# omarchy:examples=omarchy games vibes remove snake

VIBES="$HOME/Games/vibes"

if [[ -z $1 ]]; then
  games=()
  for page in "$VIBES"/*/index.html; do
    [[ -f $page ]] || continue
    game=${page%/index.html}
    games+=("${game##*/}")
  done

  if (( ${#games[@]} > 0 )); then
    GAME_NAME=$(omarchy-menu-select "$(omarchy-i18n games.vibesRemoveTitle)" "${games[@]}" -- --width 520 --maxheight 520)
  else
    echo "No games of your own to remove."
    exit 1
  fi
else
  GAME_NAME="$1"
fi

# Ensure a game was picked, and that the name cannot climb out of ~/Games/vibes
# on its way into the rm below.
if [[ ! $GAME_NAME =~ ^[a-z0-9][a-z0-9-]*$ ]]; then
  exit 1
fi

if [[ ! -d $VIBES/$GAME_NAME ]]; then
  if [[ -d $OMARCHY_PATH/default/games/vibes/$GAME_NAME ]]; then
    echo "Error: $GAME_NAME comes with Armbian and cannot be removed."
  else
    echo "Error: No game called $GAME_NAME in ~/Games/vibes."
  fi
  exit 1
fi

rm -rf "${VIBES:?}/$GAME_NAME"
omarchy-notification-send "$(omarchy-i18n games.vibesRemoved)" "$GAME_NAME" || true
echo "Removed $GAME_NAME"
