#!/bin/sh
# HVR-12x0-14x0-17x0_1_25_25271_WHQL.zip
# f26a8 (992936) = firmware offset

SUM=`md5sum HVR-12x0-14x0-17x0_1_25_25271_WHQL.zip`
if [ "$SUM" != "7fe395c1a398f079d98e416ee7be3777  HVR-12x0-14x0-17x0_1_25_25271_WHQL.zip" ]
then
	echo "Invalid zip archive, either corrupt or incorrect version"
	exit
fi

echo "Extracting hcw85bda.sys from the windows zip file"
unzip -o HVR-12x0-14x0-17x0_1_25_25271_WHQL.zip Driver85/hcw85bda.sys
if [ $? -ne 0 ]
then
	echo "Failed to extract file, aborting"
	exit
fi

echo "Extracting firmware as dvb-fe-xc5000-1.1.fw from hcw85bda.sys"
dd if=Driver85/hcw85bda.sys of=dvb-fe-xc5000-1.1.fw bs=1 skip=992936 count=12332 >/dev/null 2>&1
if [ $? -ne 0 ]
then
	echo "Failed to extract firmware from file, aborting"
	exit
fi

SUM=`md5sum dvb-fe-xc5000-1.1.fw`
if [ "$SUM" != "9ded26647453de88b3eb9bd64b9f8c2d  dvb-fe-xc5000-1.1.fw" ]
then
	echo "Error extracting firmware, failed md5 check"
	exit
else
	echo "Firmware extracted successfully"
fi

echo "Removing Driver85/hcw85bda.sys temporary file"
rm -f Driver85/hcw85bda.sys

echo "Removing Driver85 temporary dir"
rmdir Driver85

echo "Now manually copy dvb-fe-xc5000-1.1.fw into your firmware dir"
echo "  E.g. sudo cp dvb-fe-xc5000-1.1.fw /lib/firmware/`uname -r`"

