#!/bin/sh -x

if [ "$#" -ne 1 ]; then
	echo "Usage: apply_stv.sh input/"
	exit 1
fi

inpath=$1/
BASEDIR=`dirname "$0"`/
OUTDIR=$BASEDIR
toolpath=$BASEDIR"tool/"
romtool=$toolpath"romtool"
#You literally can't name the variable XDELTA or it fucks it up.
#Incredible software design, thank you.
xdeltapath=$toolpath"xdelta3-3_0_11/xdelta3-3.0.11-i686"

# The below is just to figure out the name of the bin and cue, basically.
# If you're looking at this file, you probably can just ignore
# this part and the path setup, and skip to #perform

workpath=$BASEDIR"work/"
patchpath=$BASEDIR"patches/"
mkdir -p $workpath
outname=$OUTDIR"RSG_EX_STV"
outmultiname=$OUTDIR"file.13"
outbinname=$outname".bin"
outbinmultiname=$outmultiname

#perform
echo Creating baseline patch EPROM...
"$romtool" -e "$workpath""STVPRG.BIN" "$inpath""mpr20958.7" "$inpath""mpr20959.2" "$inpath""mpr20960.3" "$inpath""mpr20961.4" "$inpath""mpr20962.5"
"$romtool" -p "$workpath""EPROM.BIN" "$workpath""STVPRG.BIN"

echo Patching EPROM...
"$xdeltapath" -f -d -s "$workpath""EPROM.BIN" "$patchpath""EPROM.xdelta3" "$outbinname"

echo Creating multigame-compatible ROM file...
"$romtool" -i "$outbinmultiname" "$outbinname"

echo Done!
