#!/bin/sh

# Uncomment the following line to get debug info
#set -x

SELF="$(basename ${0})"
INSTANT_PLAY_FILE="/mnt/instant_play"
RESUME_PLAY_FILE="/mnt/resume_play"
LAST_OPK_FILE="/mnt/last_opk"

usage() {
    >&2 echo "Usage: ${SELF} load"
    >&2 echo "       ${SELF} save application args..."
    exit 1
}

# Check number of arguments
if [ ${#} -lt 1 ]; then
    usage
fi

case ${1} in
load)
	if [ ${#} -ne 1 ]; then
	    usage
	fi

	# Umount any remaining OPK, if any
	umount /opk >/dev/null 2>&1

	# Mount last OPK, if any
	if [ -r "${LAST_OPK_FILE}" ]; then
	    last_opk=$(cat "${LAST_OPK_FILE}")
	    mount -t squashfs "${last_opk}" /opk
	fi

	# Remove unnecessary files
	rm -f "${RESUME_PLAY_FILE}"

	# Launch Previous Game if any
	if [ -f "${INSTANT_PLAY_FILE}" ]; then
	    keymap resume
	    echo -n "Found Instant Play file, restarting previous game with command: "
	    cat "${INSTANT_PLAY_FILE}"
	    mv "${INSTANT_PLAY_FILE}" "${RESUME_PLAY_FILE}"
	    source "${RESUME_PLAY_FILE}"
	    rm -f "${RESUME_PLAY_FILE}"
	    keymap default
	    termfix_all
	fi

	# Unmount last OPK, if any
    if [ -r "${LAST_OPK_FILE}" ]; then
		umount /opk
		rm "${LAST_OPK_FILE}"
    fi
	;;

save)
	if [ ${#} -lt 2 ]; then
	    usage
	fi
	shift

	# Write quick load file args
	echo -n "" > "${INSTANT_PLAY_FILE}"
	# First arg is prog name, forcing real path
	printf "'" >> "${INSTANT_PLAY_FILE}"
    bin_name=$(printf %s "$1" | sed "s/'/'\\\\''/g")
	bin_path="$(pwd)"/"$(basename "$bin_name")"
	echo -n "$bin_path" >> "${INSTANT_PLAY_FILE}"
    shift
    printf "' " >> "${INSTANT_PLAY_FILE}"

	while :
	do
        printf "'" >> "${INSTANT_PLAY_FILE}"
        printf %s "$1" | sed "s/'/'\\\\''/g" >> "${INSTANT_PLAY_FILE}"
        shift
        case $# in 0) break; esac
        printf "' " >> "${INSTANT_PLAY_FILE}"
	done
	printf "'\n" >> "${INSTANT_PLAY_FILE}"

	# Add the magic sauce to launch the process in background,
	# record the PID into a file, wait for the process to
	# terminate and erase the recorded PID
	cat << EOF >> "${INSTANT_PLAY_FILE}"
&
pid record \$!
wait \$!
pid erase
EOF

	# Now terminate gracefully
	exec powerdown now
	;;

*)
	usage
	;;
esac
exit 0

