#!/bin/bash

case `getconf LONG_BIT` in
	64) tar xzf flasher_amd64.tar.gz ;;
	 *) tar xzf flasher_i386.tar.gz	 ;;
esac

echo "Running Installer"
echo ""
echo "Please make sure your device is properly powered OFF prior to"
echo "connecting the USB cable."
echo "You can power your device off by holding down the power button"
echo "for 8+ seconds."
echo "Flashing your device will erase all data and reset the device"
echo "back to factory state."
echo "Please make sure you have backups of your data before proceeding"
echo "with flashing."
echo "Depending on amount of data to be securely erased, the erasing may"
echo "take as long as 30 minutes until the flashing starts."
echo ""
echo "Please, be patient."
echo ""
echo "Check first that device is powered OFF and then connect USB cable."
echo ""

echo "First erasing eMMC"
echo "Then flashing image and eMMC"
echo "Flashing started at "`date "+%H:%M:%S"`
echo ""
failed=false
./usr/bin/flasher -F img.bin -F emmc.bin -f --erase-mmc=secure || failed=true
echo ""
echo "Flashing finished at "`date "+%H:%M:%S"`
echo ""

if $failed
then
	echo "Flashing unsuccessful"
	echo "Press Enter to exit"
	read ip
	exit 1

else
	echo "Press Enter to exit"
	echo -n "Unplug the USB cable from the device to boot new flashed image ..."
fi
read ip
echo ""

exit 0
