#!/bin/sh

filename="$1"
sedfilename="$(echo "$filename" | sed "s|\/|\\\/|g")"
self="$(basename "$filename" .hdf)"
tmppath="/tmp/rom"
uaetmp="$tmppath/$self.uae"
template_path="/etc/libretro/system/amiga_templates"
hardfile="hardfile=read-write,32,1,2,512,"
[ -f "/bin/puae" ] && puae_core="$(grep "^exec" /bin/puae | awk '{print $3}')"
[ -f "/bin/uae4arm" ] && uae4arm_core="$(grep "^exec" /bin/uae4arm | awk '{print $3}')"

shift
while [ $# -gt 0 ]; do
  case "$1" in
    --template) template_file="$template_path/$2"; shift ;;
    --chipset) chipset="$2"; shift ;;
    --cpu) cpu="$(echo "$2" | sed "s|\/|\\\/|")"; shift ;;
    --res) res="$2"; shift ;;
    --mem) mem="$2"; shift ;;
    --framerate) framerate="$2"; shift ;;
    --fastcopper) fastcopper="$2"; shift ;;
    --aga) aga=1 ;;
    --lowocs) lowocs=1 ;;
    --uae4arm) uae4arm=1 ;;
    *) args="$args $1" ;;
  esac
  shift
done

[ -d "$tmppath" ] && rm -rf "$tmppath"
mkdir -p "$tmppath"

# Check if aga, lowocs or uae4arm template has been specified
if [ -z "$template_file" ]; then
  for t in aga lowocs uae4arm; do
    [ "$(eval echo "\$$t")" = "1" ] && template_file="$template_path/$t.uae"
  done
fi
[ ! -f "$template_file" ] && [ "$uae4arm" = "1" ] && template_file="$template_path/uae4arm.uae" # Use uae4arm template if template isnt found
[ -z "$template_file" ] || [ ! -f "$template_file" ] && template_file="$template_path/ocs.uae" # Use ocs template if none is specified or if template isnt found
cp "$template_file" "$uaetmp" # Copy and rename the template file in /var/cache/retroarch
[ -z "$(tail -c 1 "$uaetmp")" ] || echo >> "$uaetmp" # Add an empty line at the end if needed

# Modify/add required lines for WHDLoad.hdf and GAME.hdf
if [ "$(grep "^$hardfile.*WHDLoad.hdf" "$uaetmp")" ]; then
  sed -i "s/^$hardfile.*<PATH TO GAME>\/WHDLoad.hdf/$hardfile$(dirname "$sedfilename")/WHDLoad.hdf/" "$uaetmp"
else
  echo "$hardfile$(dirname "$filename")/WHDLoad.hdf" >> "$uaetmp"
fi
if [ "$(grep "^$hardfile.*<PATH TO GAME>/GAME.hdf" "$uaetmp")" ]; then
  sed -i "s/^$hardfile.*<PATH TO GAME>\/GAME.hdf/$hardfile$sedfilename/" "$uaetmp"
else
  echo "$hardfile$filename" >> "$uaetmp"
fi

# Edit the template options if specified
[ -z "$chipset" ] || sed -i "s/^chipset=.*/chipset=$chipset/" "$uaetmp"
[ -z "$cpu" ] || sed -i "s/^cpu_type=.*/cpu_type=$cpu/" "$uaetmp"
if [ ! -z "$res" ]; then
  [ "$res" = "min" ] && sed -i "s/^gfx_width=.*/gfx_width=320/;s/^gfx_height=.*/gfx_height=256/" "$uaetmp"
  [ "$res" = "max" ] && sed -i "s/^gfx_width=.*/gfx_width=724/;s/^gfx_height=.*/gfx_height=566/" "$uaetmp"
fi
[ -z "$mem" ] || sed -i "s/^fastmem_size=.*/fastmem_size=$mem/" "$uaetmp"
[ -z "$framerate" ] || sed -i "s/^gfx_framerate=.*/gfx_framerate=$framerate/" "$uaetmp"
[ -z "$fastcopper" ] || sed -i "s/^fast_copper=.*/fast_copper=$fastcopper/" "$uaetmp"

set "$uaetmp" "$args"

[ "$uae4arm" = "1" ] || exec retroarch-clover "$puae_core" "$@" --ra-nosaves
[ "$uae4arm" = "1" ] && exec retroarch-clover "$uae4arm_core" "$@" --ra-nosaves
