#!/bin/sh

# Get firmware for ezusb used in Orinoco USB cards.
# Both the header file and the binary firmware file are produced.

# Copyright (C) 2004 Pavel Roskin <proski@gnu.org>

# This script is Free Software, and it can be copied, distributed and
# modified as defined in the GNU General Public License.  A copy of
# its license can be downloaded from http://www.gnu.org/copyleft/gpl.html

# Usage: get_orinoco_fw
# Output: orinoco_usb_fw.h orinoco_ezusb_fw
# Needed tools: curl (or wget), unzip, dd, od, sed.

set -e

URL_BASE=ftp://ftp.avaya.com/incoming/Up1cku9/tsoweb/avayawireless
DL_FILE=AV_WINXP_PC_USB_SR0201.zip
URL="$URL_BASE/$DL_FILE"
DL_FRAG=ezusb.drv.pkz
DL_INT_PATH="USB/WLAGS51B.sys"
DL_INT_OFFSET=6117483

get_file_short() {
	curl --range $DL_INT_OFFSET -o "$1" "$2"
}

get_file() {
	curl --remote-name "$1" || \
	wget --passive-ftp "$1" || \
	wget "$1" || \
	ftp "$1" </dev/null || \
	exit 1
}

if ! test -f $DL_FRAG; then
	get_file_short $DL_FRAG "$URL" || true
fi
gzip -cd <$DL_FRAG >ezusb.drv || true

if ! test -s ezusb.drv; then
	if ! test -f $DL_FILE; then
		echo "WARNING: Quick download failed, fetching full file"
		get_file "$URL"
	fi
	unzip -p $DL_FILE "$DL_INT_PATH" >ezusb.drv
fi

dd if=ezusb.drv of=orinoco_ezusb_fw skip=9983 count=436 bs=16
echo 'static u8 ezusb_fw[] = {' > orinoco_usb_fw.h
od -t x1 -v orinoco_ezusb_fw | \
  sed 's/^[^ ]* *//;/^$/d;s/^/	0x/;s/ /,0x/g;s/$/,/' >>orinoco_usb_fw.h
echo '};' >>orinoco_usb_fw.h

rm -f ezusb.drv
