#!/bin/sh
# Wrapper for canoe
# You can add extra command line arguments to all games at once
# using "cfg_snes_extra_args" variable in p0000_config

source /etc/preinit
script_init

options=""

while [ $# -gt 0 ] ; do
  case "$1" in
  -rom)
    filename="$(readlink -f "$2")"
    filebase="$(basename "$filename")"
    extension="${filebase##*.}"
    tmppath="$temppath/rom"
    if [ "$extension" == "7z" ]; then
      rm -rf "$tmppath"
      mkdir -p "$tmppath"
      cd "$tmppath"
      tiny7zx x "$filename"
      filename="$tmppath/$(ls)"
      filename_str=${filename// /_}
      mv "$filename" "$filename_str"
      filename=$filename_str
    fi
    options="-rom $filename"
    shift
    ;;
  *) options="$options $1" ;;
  esac
  shift
done

args="$options $cfg_snes_extra_args"
exec /usr/bin/clover-canoe-shvc $args
