#!/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 hcw85enc.rom from the windows zip file"
unzip -jo HVR-12x0-14x0-17x0_1_25_25271_WHQL.zip Driver85/hcw85enc.rom
if [ $? -ne 0 ]
then
	echo "Failed to extract file, aborting"
	exit
fi
mv hcw85enc.rom v4l-cx23885-enc.fw

echo "Extracting hcw85mlC.rom from the windows zip file"
unzip -jo HVR-12x0-14x0-17x0_1_25_25271_WHQL.zip Driver85/hcw85mlC.rom
if [ $? -ne 0 ]
then
	echo "Failed to extract file, aborting"
	exit
fi
mv hcw85mlC.rom v4l-cx23885-avcore-01.fw

SUM=`md5sum v4l-cx23885-enc.fw`
if [ "$SUM" != "1cb3c48a6684126f5e503a434f2d636b  v4l-cx23885-enc.fw" ]
then
	echo "Error extracting firmware, failed md5 check"
	exit
else
	echo "Firmware extracted successfully"
fi

SUM=`md5sum v4l-cx23885-avcore-01.fw`
if [ "$SUM" != "b3704908fd058485f3ef136941b2e513  v4l-cx23885-avcore-01.fw" ]
then
	echo "Error extracting firmware, failed md5 check"
	exit
else
	echo "Firmware extracted successfully"
fi

##
# TDA10048 firmware offset a3060 (667744) = firmware offset, length = 24878

echo "Extracting hcw85bda.sys from the windows zip file"
unzip -jo 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-tda10048-1.0.fw from hcw85bda.sys"
dd if=hcw85bda.sys of=dvb-fe-tda10048-1.0.fw bs=1 skip=667744 count=24878 >/dev/null 2>&1
if [ $? -ne 0 ]
then
	echo "Failed to extract firmware from file, aborting"
	exit
fi

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

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

echo "Now manually copy firmware into your firmware dir"
echo "  E.g. sudo cp v4l-cx23885-enc.fw /lib/firmware/`uname -r`"
echo "  E.g. sudo cp v4l-cx23885-avcore-01.fw /lib/firmware/`uname -r`"
echo "  E.g. sudo cp dvb-fe-tda10048-1.0.fw /lib/firmware/`uname -r`"

