#!/usr/bin/env bash

{ # this ensures the entire script is downloaded #
# this will exit the script if any error is encountered . . . 
# . . . it usually also exits the ssh connection so not using this for now
# set -e

# Not sure why nvm has all these functions but I don't mind them
my_has() {
  type "$1" > /dev/null 2>&1
}

my_distro_check() {
  if [ -f "/usr/bin/rocknix-config" ]; then
    return 0  # True
  else 
    return 1  # False
  fi
}

my_echo() {
  command printf %s\\n "$*" 2>/dev/null
}

#
# Unsets the various functions defined
# during the execution of the install script
#
my_reset() {
  unset -f my_has my_echo my_distro_check my_grep
}

my_echo "=> STARTING SAMPLE GAME INSTALL SCRIPT"

if [ -z "${BASH_VERSION}" ] || [ -n "${ZSH_VERSION}" ]; then
  my_echo >&2 'Error: the install instructions explicitly say to pipe the install script to `bash`; please follow them'
  exit 1
fi

my_grep() {
  GREP_OPTIONS='' command grep "$@"
}

# check to see if I'm running on a knulli device
if my_distro_check; then
  my_echo "=> This is a compatible device, /etc/issue says so"
else
  my_echo "=> This NOT is a compatible device, EXITING!!"
  exit 1
fi

my_warning() {
    my_echo "=> NOTE! You need to run the 'Update Gamelists' Rocknix option to get the game to show up. "
    my_echo "=> NOTE! This script assumes your roms are stored in /roms on the root device. "
    my_echo "=> NOTE! We need to do more testing on a device we a secondary SD card to get this to work in those situations. "
    my_echo "=> NOTE! This script will also delete and replace any existing games in /roms/jsgames that have matching names. "
}


my_warning

cd ~
my_echo "=> Cleaning up old files"
if [ -d "$HOME/newmygames.zip" ]; then
  my_echo "=> File ~/newmygames.zip exists. Deleting..."
  rm -rf ~/newmygames.zip
fi

if [ -d "$HOME/jsgames-main" ]; then
  my_echo "=> File ~/jsgames-main exists. Deleting..."
  rm -rf ~/jsgames-main
fi

my_echo "=> Downloading jsgames"
source ~/.bash_profile
curl -o newmygames.zip -L https://github.com/monteslu/jsgames/archive/refs/heads/main.zip
unzip newmygames.zip



if my_distro_check; then
  my_echo "=> This is a compatible device so I'm copying the sample games"

  if [ -d "/roms/jsgames" ]; then
    my_echo "=> Folder /roms/jsgames exists, no need to create it."
  else 
    my_echo "=> Folder /roms/jsgames does not exist! That could be a problem."
    mkdir /roms/jsgames
  fi

  source ~/.bash_profile
  nvm use 22
  cd ~/jsgames-main
  for dir in *; do
    if [ -d "/roms/jsgames/$dir" ]; then
        my_echo "=> Deleting existing $dir game from /roms/jsgames"
        rm -rf /roms/jsgames/$dir
    else
        my_echo "=> Copying $dir game to /roms/jsgames"
    fi

  done
  cd ~
  mv jsgames-main/* /roms/jsgames/
  rm -r jsgames-main

  cd /roms/jsgames/
  for dir in *; do
    if [ "$dir" = "installers" ]; then
      continue
    fi
    if [ -f "/roms/jsgames//$dir/package.json" ]; then
        my_echo "=> package.json exists! Need to npm install on $dir" 
        cd /roms/jsgames/$dir
        npm install --omit=dev
    else
        my_echo "=> Note: No package.json on $dir so skipping npm install"
    fi
  done

  my_echo "=> INSTALL SUCCESSFUL!"
  cd ~
else
  my_echo "=> my_distro_check says this is NOT is a compatible device, so I'm not moving files around!"
  my_echo "=> INSTALL NOT SUCCESSFUL!"
fi

my_warning
my_reset

} # this ensures the entire script is downloaded #


