#!/bin/sh
#
# Title      :	uhd-install-firmware
# Purpose    :  fetch non-free UHD firmware
# Author     :	A. Maitland Bottoms <bottoms@debian.org>
# Date       :	2012-04-21
#
# Copyright 2011-2012 A. Maitland Bottoms <bottoms@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

show_usage()
{
    echo "Usage: $0 [-i imagedir] [imagetarball]" >&2
    exit 1
}

# Default values:
# The imagedir is the path uhd-host and libuhd packages
# use to find firmware.
imagedir="/usr/share/uhd/images"
# The imagetarball is the source of firmware built with matching
# source code.
imagetarball="http://files.ettus.com/binaries/uhd_stable/releases/uhd_003.004.002-release/uhd-images_003.004.002-release.tar.gz"
while [ "$1" != "" ]; do
    case $1 in
        -h | --help )           show_usage
                                exit 0
                                ;;
        -f | --file )           shift
                                imagetarball=$1
                                ;;
        -i | --imagedir )       shift
	                        imagedir=$1
                                ;;
        * )                     imagetarball=$1
                                ;;
    esac
    shift
done

if mkdir -p $imagedir ; then
    if [ -w $imagedir ] ;then
	echo Using imagedir: $imagedir
    else
	echo You need to run this script as a user who can write to $imagedir
	exit 1
    fi
else
    echo You need to run this script as a user who can create $imagedir
    exit 1
fi

tdir=`mktemp -d`
echo "Using tempdir:" $tdir
echo "Using imagetarball:" $imagetarball

# if URL, fetch it first
    if echo $imagetarball | grep -q :// ; then
	echo Fetching $imagetarball ;
	if [ -x /usr/bin/wget ] ; then
	    /usr/bin/wget --user-agent="Debian UHD image installer" -O $tdir/uhd-images_003.004.002-release.tar.gz $imagetarball
	else
	    curl --user-agent "Debian UHD image installer" -o $tdir/uhd-images_003.004.002-release.tar.gz $imagetarball
	fi
	imagetarball=$tdir/uhd-images_003.004.002-release.tar.gz
    else
	if [ -f $imagetarball ] ; then
	    echo Extracting $imagetarball
	else
	    echo "Cannot find" $imagetarball;
	    show_usage
	    exit 1
	fi
    fi

# check image tarball
    r=`echo 3a029f244df6dd9812e196ec905a1fc8\ \ $imagetarball | md5sum -c`
    if echo $r | grep -q OK ; then
	echo "It's OK: md5sum is as expected." ;
	rm -f $imagedir/*.tag
        # extract images into imagedir
	tar zx --no-same-owner --no-same-permissions -C $imagedir --strip-components=4 -f $imagetarball
	echo Installed
	cat $imagedir/*.tag
    else
	echo "Unrecognized image tarball." ;
    fi

rm -i -rf $tdir

exit 0
