#!/bin/sh
#
# Title      :	bladeRF-install-firmware
# Purpose    :  fetch non-free BladeRF firmware
# Author     :	A. Maitland Bottoms <bottoms@debian.org>
# Date       :	2013-10-10
# Update     :	2019-08-20
#
# Copyright 2013-2019 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
}

FX3UPSTREAM='https://www.nuand.com/fx3/bladeRF_fw_v2.3.2.img'
FX3CHECKSUM='c823ea5aa2c25c26bbaf4ab395ac559d'
FX3SHA256SUM='ef92cd41513ab3c3d36174143545b7eab6b033fdcaaf661fec1d710a489fadbf'
FX3IMGFILE='/usr/share/Nuand/bladeRF/bladeRF_fw.img'

FPGA115UPSTREAM='https://www.nuand.com/fpga/v0.11.0/hostedx115.rbf'
FPGA115CHECKSUM='bee4542c4b020077c71ae630ef0d0fc8'
FPGA115SHA256SUM='604b12af77ce4f34db061e9eca4a38d804f676b8ad73ceacf49b6de3473a86f7'
FPGA115RBFFILE='/usr/share/Nuand/bladeRF/hostedx115.rbf'

FPGA40UPSTREAM='https://www.nuand.com/fpga/v0.11.0/hostedx40.rbf'
FPGA40CHECKSUM='7a9b8e7f0f81af6d393132d7eb3e6e18'
FPGA40SHA256SUM='a26e07b8ad0b4c20327f97ae89a89dbe2e00c7f90e70dae36902e62e233bd290'
FPGA40RBFFILE='/usr/share/Nuand/bladeRF/hostedx40.rbf'

FPGAA4UPSTREAM='https://www.nuand.com/fpga/v0.11.0/hostedxA4.rbf'
FPGAA4CHECKSUM='b23bf6088aff5cff90bdea9488a35d89'
FPGAA4SHA256SUM='c172e35c4a92cf1e0ca3b37347a84d8376b275ece16cb9c5142b72b82b16fe8e'
FPGAA4RBFFILE='/usr/share/Nuand/bladeRF/hostedxA4.rbf'

FPGAA9UPSTREAM='https://www.nuand.com/fpga/v0.11.0/hostedxA9.rbf'
FPGAA9CHECKSUM='a17a2859e4f28fdc3952de78aca1dabe'
FPGASHA256SUM='99f60e91598ea5b998873922eba16cbab60bfd5812ebc1438f49b420ba79d7e1'
FPGAA9RBFFILE='/usr/share/Nuand/bladeRF/hostedxA9.rbf'

ADSBA4UPSTREAM='https://www.nuand.com/fpga/adsbxA4.rbf'
ADSBA4CHECKSUM='7317e69e9bea3595a7babeadeb3633b0'
ADSBA4SHA256SUM='8e1dcde86203f89dcf8bec77e3740ea31917ec335f6b12e3f9194cedbb833e19'
ADSBA4RBFFILE='/usr/share/Nuand/bladeRF/adsbxA4.rbf'

ADSBA9UPSTREAM='https://www.nuand.com/fpga/adsbxA9.rbf'
ADSBA9CHECKSUM='9e70f6069680d7a2434ec9fb88ce2a36'
ADSBA9SHA256SUM='343241eba0942538d74a5290e47a4cdded8dbe810d19e6328dcf496efac676f1'
ADSBA9RBFFILE='/usr/share/Nuand/bladeRF/adsbxA9.rbf'

ADSB40UPSTREAM='https://www.nuand.com/fpga/adsbx40.rbf'
ADSB40CHECKSUM='15b7e988fcee2881f92df296bfcee9bf'
ADSB40SHA256SUM='a491fe4adc310b9ac71cab5017d2b99897b3d314b51130ed9b3c4c965d3ad4c4'
ADSB40RBFFILE='/usr/share/Nuand/bladeRF/adsbx40.rbf'

ADSB115UPSTREAM='https://www.nuand.com/fpga/adsbx115.rbf'
ADSB115CHECKSUM='30583f92aa4a3ec5660714eccae30082'
ADSB115SHA256SUM='c908baca2848efebc4c1aacec239179b548182406f398e608ad99dbbf0b72836'
ADSB115RBFFILE='/usr/share/Nuand/bladeRF/adsbx115.rbf'

# Default values:
# The imagedir is the path bladerf-host and libbladerf packages
# use to find firmware.
imagedir="/usr/share/Nuand/bladeRF"
# The imagetarball is the source of firmware built with matching
# source code.
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="$FX3UPSTREAM $FPGA40UPSTREAM $FPGA115UPSTREAM $FPGAA4UPSTREAM $FPGAA9UPSTREAM \
    			    $ADSB40UPSTREAM $ADSB115UPSTREAM $ADSBA4UPSTREAM $ADSBA9UPSTREAM"
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;
    if [ `basename $item` = "bladeRF_fw_v2.3.2.img" ] ; then
	md5sum --check - <<- EOF
$FX3CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FX3SHA256SUM  $tdir/`basename $item`
EOF
	cp -p $tdir/`basename $item` $imagedir/bladeRF_fw.img ;
    fi
    if [ `basename $item` = "hostedx40.rbf" ] ; then
	md5sum --check - <<- EOF
$FPGA40CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FPGA40SHA256SUM  $tdir/`basename $item`
EOF
    fi
    if [ `basename $item` = "hostedx115.rbf" ] ; then
	md5sum --check - <<- EOF
$FPGA115CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FPGA115SHA256SUM  $tdir/`basename $item`
EOF
    fi
    if [ `basename $item` = "hostedxa4.rbf" ] ; then
	md5sum --check - <<- EOF
$FPGAA4CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FPGAA4SHA256SUM  $tdir/`basename $item`
EOF
    fi
    if [ `basename $item` = "hostedxa9.rbf" ] ; then
	md5sum --check - <<- EOF
$FPGAA9CHECKSUM  $tdir/`basename $item`
EOF
	sha256sum --check - <<- EOF
$FPGAA9SHA256SUM  $tdir/`basename $item`
EOF
    fi
    cp -p $tdir/`basename $item` $imagedir/`basename $item`
done

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

exit 0
