#!/bin/sh

IVTVFWEXTRACT=/usr/bin/ivtvfwextract
PKGNAME="ivtv-firmware"

DIST_FWENC=ivtv-fw-enc.bin
DIST_FWDEC=ivtv-fw-dec.bin
DIST_AUDIO=HcwMakoA.ROM

FWENC=v4l-cx2341x-enc.fw
FWDEC=v4l-cx2341x-dec.fw
AUDIO=v4l-cx25840.fw

ME=$(basename $0)

MAINTAINER_NAME="Unknown Maintainer"
MAINTAINER_EMAIL=unknown@example.com

if [ -n "$DEBFULLNAME" ] ; then
    MAINTAINER_NAME="$DEBFULLNAME"
else
    echo "$ME: Maintainer name is unknown. Using \"$MAINTAINER_NAME\"." 1>&2
    echo "$ME: You should set \$DEBFULLNAME." 1>&2
fi

if [ -n "$DEBEMAIL" ] ; then
    MAINTAINER_EMAIL="$DEBEMAIL"
elif [ -n "$EMAIL" ] ; then
    MAINTAINER_EMAIL="$EMAIL"
else
    echo "$ME: Maintainer email is unknown. Using \"$MAINTAINER_EMAIL\"." 1>&2
    echo "$ME: You should set \$DEBEMAIL." 1>&2
fi
MAINTAINER="$MAINTAINER_NAME <$MAINTAINER_EMAIL>"

if [ ! -x "$IVTVFWEXTRACT" ] ; then
    echo "$ME: ivtvfwextract not found." 1>&2
    exit 1
fi

for ARG in $@ ; do
    #echo "$ME: Considering $ARG"
    case $ARG in
	--help)
	    echo "$0:" 1>&2
	    echo "  Usage:" 1>&2
	    echo "    $0 --help" 1>&2
	    echo "  or" 1>&2
	    echo "    $0 [ivtvfwextract-options] FILENAME" 1>&2
	    echo "  All options are passed directly to $IVTVFWEXTRACT" 1>&2
	    echo "  Only a single filename is allowed" 1>&2
	    echo "" 1>&2
	    exec $IVTVFWEXTRACT --help
	    ;;
	--*) ;; # skip other options, they are passed direct to ivtvfwextract
	*)
	    #echo "$ME: Found filename $ARG"
	    if [ -n "$FILENAME" ] ; then
		echo "$ME: More than one filename found on command line." 1>&2
		echo "$ME: $FILENAME $ARG" 1>&2
		exit 1
	    fi
	    FILENAME=$ARG
	    ;;
    esac
done

if [ -z "$FILENAME" ] ; then
    echo "$ME: A firmware archive is required as an argument." 1>&2
    echo "$ME: e.g. $ME pvr_1.18.21.22254_inf.zip." 1>&2
    echo "$ME: Use \"$ME --help\" for more information." 1>&2
    echo ""
    echo "$ME: For more information about suggested firmware versions" 1>&2
    echo "$ME: see http://www.hellion.org.uk/ivtv/index.html." 1>&2
    exit 1
fi

echo "$ME: Extracting firmware from $FILENAME"
BASENAME=$(basename "$FILENAME")

case "$BASENAME" in
    pvr48wdm_*.exe)
	echo "$ME: Firmware is self-extracting .exe variety"
	VERSION=$(echo "$BASENAME" | sed -e 's/pvr48wdm_\(.*\)\.exe/\1/')
	;;
    pvr_*_inf.zip)
	echo "$ME: Firmware is .zip variety"
	VERSION=$(echo "$BASENAME" | sed -e 's/pvr_\(.*\)_inf\.zip/\1/')
	;;
esac

if [ -z "$VERSION" ] ; then
    echo "$ME: Failed to determine firmware version." 1>&2
    exit 1
fi

echo "$ME: Firmware version is $VERSION"

echo "$ME: Maintainer: $MAINTAINER"

if [ -n "$USE_TEMP_DIR" ] ; then
    TEMPDIR=$(mktemp -d -t "$ME.XXXXXX")
    echo "$ME: Temporary directory is $TEMPDIR"
    trap 'if [ -n "$TEMPDIR" -a -d "$TEMPDIR" ] ; then rm -rf "$TEMPDIR" ; fi' EXIT

    PKGDIR="${TEMPDIR}/${PKGNAME}-${VERSION}"

    ORIG="${TEMPDIR}/${PKGNAME}_${VERSION}.orig.tar.gz"
    PKG="${TEMPDIR}/${PKGNAME}_${VERSION}_all.deb"
    DSC="${TEMPDIR}/${PKGNAME}_${VERSION}.dsc"
else
    PKGDIR="${PKGNAME}-${VERSION}"
    if [ -d "$PKGDIR" ] ; then
	echo "$ME: package directory \"${PKGDIR}\" already exists." 1>&2
	echo "$ME: please remove it to continue." 1>&2
	exit 1
    fi
fi

DEBDIR="${PKGDIR}/debian"

mkdir "$PKGDIR"
mkdir "$DEBDIR"

$IVTVFWEXTRACT $@ "$PKGDIR"
if [ $? -ne 0 ] ; then
    echo "$ME: failed to extract firmware." 1>&2
    exit 1
fi
if [ ! -f "${PKGDIR}/${DIST_FWENC}" -o ! -f "${PKGDIR}/${DIST_FWDEC}" ] ; then
    echo "$ME: extracted firmware not found." 1>&2
    exit 1
fi

[ -f "${PKGDIR}/${DIST_FWENC}" ] && mv "${PKGDIR}/${DIST_FWENC}" "${PKGDIR}/${FWENC}"
[ -f "${PKGDIR}/${DIST_FWDEC}" ] && mv "${PKGDIR}/${DIST_FWDEC}" "${PKGDIR}/${FWDEC}"
[ -f "${PKGDIR}/${DIST_AUDIO}" ] && mv "${PKGDIR}/${DIST_AUDIO}" "${PKGDIR}/${AUDIO}"

cat > "$DEBDIR/control" <<EOF
Source: ivtv-firmware
Section: non-free/x11
Priority: optional
Build-Depends: debhelper (>= 4)
Maintainer: $MAINTAINER
Standards-Version: 3.6.2.1

Package: ivtv-firmware
Architecture: all
Description: firmware for use with the ivtv kernel driver
 The primary goal of the IvyTV Project is to create a kernel driver
 for the iTVC15 familiy of MPEG codecs. The iTVC15 family includes the
 iTVC15 (CX24315) and iTVC16 (CX24316). These chips are commonly found
 on Hauppauge's WinTV PVR-250 and PVR-350 TV capture cards.
 .
 This package contains the firmware. It was extracted from 
 $BASENAME
EOF
#cat "$DEBDIR/control"

cat > "$DEBDIR/changelog" <<EOF
ivtv-firmware ($VERSION) unstable; urgency=low

  * Package created by $ME.

 -- $MAINTAINER  $(822-date)
EOF
#cat "$DEBDIR/changelog"

cat > "$DEBDIR/copyright" <<EOF
This package was created from $BASENAME using the $ME
tool by $MAINTAINER on $(822-date).

$ME was provided by ivtv version 0.8.2-2.

The licensing situation of these files is unknown. They
are free to download but I am unsure about redistribution,
hence this tool.
EOF

cat > "$DEBDIR/rules" <<EOF
#!/usr/bin/make -f
# -*- makefile -*-

#Architecture
# There is nothing to build
build:
build-arch:
build-indep:

clean:
	dh_testdir
	dh_testroot
	dh_clean

# Build architecture independant packages using the common target.
binary-indep:
	dh_testdir
	dh_testroot
	dh_installdirs lib/firmware
	cp $FWENC debian/ivtv-firmware/lib/firmware/
	cp $FWDEC debian/ivtv-firmware/lib/firmware/
	[ ! -f $AUDIO ] || ( cp $AUDIO debian/ivtv-firmware/lib/firmware/ )
	cd debian/ivtv-firmware/lib/firmware/ && ln -s $FWENC $DIST_FWENC
	cd debian/ivtv-firmware/lib/firmware/ && ln -s $FWDEC $DIST_FWDEC
	[ ! -f $AUDIO ] || ( cd debian/ivtv-firmware/lib/firmware/ && ln -s $AUDIO $DIST_AUDIO )
	dh_installdocs README
	dh_installchangelogs
	dh_compress
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Nothing to be done for architecture dependant packages
binary-arch:

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary

EOF
chmod +x "$DEBDIR/rules"

cat > "$PKGDIR/README" <<EOF
A list of all available firmware versions can be found at 
http://ivtv.writeme.ch/tiki-index.php?page=FirmwareVersions

This package contains firmware with the following md5sums:
EOF
( cd $PKGDIR ; md5sum ${FWENC} ${FWDEC}) >> "$PKGDIR/README"

echo "4" > "$DEBDIR/compat"

( cd "${TEMPDIR:-.}" ; tar czf "ivtv-firmware_${VERSION}.orig.tar.gz" "ivtv-firmware-${VERSION}" )
if [ $? -ne 0 ] ; then
    echo "$ME: Failed to pack .orig.tar.gz" 1>&2
    exit 1
fi

( cd "$PKGDIR" ; debuild -us -uc )
if [ $? -ne 0 ] ; then
    echo "$ME: Failed to build package" 1>&2
    exit 1
fi

if [ -n "$USE_TEMP_DIR" ] ; then
    cp -iv "${ORIG}" "${PKG}" "${DSC}" .
fi
