#!/bin/sh
#
# Title      :	bladeRF-install-firmware
# Purpose    :  fetch non-free BladeRF firmware
# Author     :	A. Maitland Bottoms <bottoms@debian.org>
# Date       :	2013-10-10
#
# Copyright 2013 A. Maitland Bottoms <bottoms@debian.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

set -e

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

# Default values:
# The imagedir is the path bladerf-host and libbladerf packages
# use to find firmware.
imagedir="/usr/share/bladerf/images"
# The imagetarball is the source of firmware built with matching
# source code.
fx3imageurl="http://nuand.com/fx3/latest.img"
fpga40imageurl="http://nuand.com/fpga/ddc81d0fb1b653227b5824d8d5fcb444556da175/hostedx40.rbf"
fpga115imageurl="http://nuand.com/fpga/ddc81d0fb1b653227b5824d8d5fcb444556da175/hostedx115.rbf"
fpgaimageurl=""
usbimageurl=""
while [ "$1" != "" ]; do
    case $1 in
        -h | --help )           show_usage
                                exit 0
                                ;;
        -i | --imagedir )       shift
	                        imagedir=$1
                                ;;
        -f | --fpgafile )       shift
                                fpgaimageurl=$1
                                ;;
        -u | --usbfile )        shift
                                usbimageurl=$1
                                ;;
        * )                     fpgaimageurl=$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`
fetchlist=""
echo "Using tempdir:" $tdir
if [ "x$fpgaimageurl" = "x" ] && [ "x$usbimageurl" = "x" ] ; then
    fetchlist="$fx3imageurl $fpga40imageurl $fpga115imageurl"
else
    if [ "x$fpgaimageurl" != "x" ] ; then
	fetchlist="$fetchlist $fpgaimageurl"
    fi
    if [ "x$usbimageurl" != "x" ] ; then
	fetchlist="$fetchlist $usbimageurl"
    fi
fi

for item in $fetchlist; do
echo "Using: " $item

# if URL, fetch it first
    if echo $item | grep -q :// ; then
	echo Fetching $item ;
	if [ -x /usr/bin/wget ] ; then
	    /usr/bin/wget --user-agent="Debian BladeRF image installer" -O $tdir/`basename $item` $item
	else
	    curl --user-agent "Debian BladeRF image installer" -o $tdir/`basename $item` $item
	fi
    else
	if [ -f $item ] ; then
	    echo Copying $item
	    cp -p $item $tdir/
	else
	    echo "Cannot find" $item;
	    show_usage
	    exit 1
	fi
    fi;
    cp -p $tdir/`basename $item` $imagedir/`basename $item`
done

for file in `ls $tdir`; do md5sum $file ; done
rm -i -rf $tdir

exit 0
