#!/bin/sh
#
# Copyright (C) 2016 Dream Property GmbH
#

source librecovery

OTP=$(readspi --otp -c 192 | base64 | tr -d '\n' | tr '+' '-' | tr '/' '_')
SHA=$(readspi -c ${FSBL_SIZE} | sha256sum | awk '{print $1}')

[ -n "${OTP}" ] || abort 'Failed to read OTP data'
[ -n "${SHA}" ] || abort 'Failed to hash FSBL'

URI="http://dreambox.de/verify/fsbl?m=${MID}&o=${OTP}&s=${SHA}"

xgetopts $@
create_workspace
create_keyring "$FSBL_KEY"

info "Downloading '${URI}'"
wget -q "${URI}" -O fsbl-flasher.tgz >&3 2>&4

create_directory fsbl-flasher
extract_tarball fsbl-flasher.tgz fsbl-flasher
verify fsbl-flasher/fsbl-flasher

info "Running fsbl-flasher"
chmod 755 fsbl-flasher/fsbl-flasher || abort "Failed to set execute permissions"
xtrap fsbl-flasher/fsbl-flasher || abort "Failed to execute fsbl-flasher"
info "Done."
