RetroArch/retroarch-zip
2012-11-08 16:31:56 -06:00

86 lines
1.9 KiB
Bash
Executable File

#!/bin/sh
###
## Wrapper script that enables loading of compressed roms for RetroArch.
## Author: Themaister
## License: Do whatever you want with it.
#
die()
{
echo "Error: $1"
rm "${TMPROM}"
rm -r "${TMPDIR}"
exit 1
}
if [ -z "$1" ]; then
die "Need at least one argument"
fi
COMP_FILE="$1"
TMPROM="`mktemp`"
TMPDIR="`mktemp -d`"
shift
extract_zip()
{
which unzip >/dev/null 2>&1 || die "Cannot find unzip"
unzip "$1" -d "${TMPDIR}" >/dev/null 2>&1 || die "Couldn't extract from archive"
}
extract_7z()
{
which 7z >/dev/null 2>&1 || die "Cannot find 7z"
7z x "$1" -o"${TMPDIR}" >/dev/null 2>&1 || die "Couldn't extract from archive"
}
extract_rar()
{
which unrar >/dev/null 2>&1 || die "Cannot find unrar"
unrar x "$1" "${TMPDIR}" >/dev/null 2>&1 || die "Couldn't extract from archive"
}
case "${COMP_FILE}" in
*.zip )
extract_zip "${COMP_FILE}"
EXTENSION=".zip"
;;
*.7z )
extract_7z "${COMP_FILE}"
EXTENSION=".7z"
;;
*.rar )
extract_rar "${COMP_FILE}"
EXTENSION=".rar"
;;
* )
die "Couldn't find valid zipped rom."
;;
esac
# pick the largest non-directory in the root of the unpacked archive
FILENAME="`ls -Sp \"${TMPDIR}\" | grep -v /$ | head -n1`"
if [ -z "${FILENAME}" ]; then
die "Cannot find any files in root of archive"
fi
mv "${TMPDIR}/${FILENAME}" "${TMPROM}"
SAVENAME="`basename \"${COMP_FILE}\" ${EXTENSION}`"
SAVESTATE="`basename \"${COMP_FILE}\" ${EXTENSION}`"
DIRNAME="`dirname \"${COMP_FILE}\"`"
SAVENAME="${SAVENAME}.srm"
SAVESTATE="${SAVESTATE}.state"
EXE="`which retroarch`"
if [ -z "${EXE}" ]; then
die "Cannot find RetroArch in path."
fi
echo "Temp ROM: ${TMPROM}"
echo "Savename: ${DIRNAME}/${SAVENAME}"
echo "Savestate: ${DIRNAME}/${SAVESTATE}"
"${EXE}" "${TMPROM}" -s "${DIRNAME}/${SAVENAME}" -S "${DIRNAME}/${SAVESTATE}" "$@"
rm "${TMPROM}"
rm -r "${TMPDIR}"