#
# Copyright (C) 2016 Dream Property GmbH
#

source librecovery

URI="https://dreamboxupdate.com/opendreambox/2.6/stable/images/dream${MACHINE}/dreambox-rescue-image-dream${MACHINE}-20200724.bootimg"

usage()
{
	echo "Usage: ${0} [-fhqtv] [<URI>]"
	exit "${1}"
}

FORCE="false"
while getopts fhqtv opt; do
	case "${opt}" in
		f)
			FORCE=":"
			;;
	esac
	std_opt "${opt}"
done

shift $((${OPTIND} - 1))
[ "$#" -le 1 ] || usage 1
URI="${1:-${URI}}"

${FORCE} || ! is_gpt || abort "Do not run this command when switched to GPT!"
${FORCE} || ! is_initrd || abort "Do not run this command from rescue mode!"

create_workspace
create_keyring_for_uri "${URI}"

BASE_URI="$(dirname ${URI})"
FILENAME="$(basename ${URI})"

fetch_signed "${BASE_URI}" "${FILENAME}"
info "Writing rescue image to flash"
xtrap flash-rescue "${FILENAME}"
info "Done."
