#!/bin/sh
#
# defon.sh
# Extract FON upgrade archives
#
# by Stefan Tomanek <stefan@pico.ruhr.de>
# http://stefans.datenbruch.de/lafonera/

FONFILE=$1

if ! [ -f "$FONFILE" ]; then
    echo "File '$FONFILE' not found." >&2
    exit 1
fi

HEADER="$(head -c 4 "$FONFILE")"
if [ "$HEADER" == "FON3" ]; then
    echo "Upgrade contains a new firmware" >&2
elif [ "$HEADER" == "FON4" ]; then
    echo "Upgrade contains a hotfix archive" >&2
fi

OFFSET="$(head -c 7 "$FONFILE" | tail -c 3)"
BEGIN=$((7+$OFFSET+1))
tail -c +$BEGIN "$FONFILE"
