#!/bin/bash
#
#   Script to download the firmware image
#
#   Copyright (C) 2005 Daniel Eiband <eiband@online.de>
#
#   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 2 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, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#


usage()
{
	echo "Usage: fwmod_download [-C <dir>] <site> <file>" 1>&2
}


while getopts "C:" opt
do
	case "$opt" in
		C)
			[ -e "$OPTARG" ] || mkdir -p "$OPTARG"
			cd "$OPTARG"
			;;
		*)
			usage
			exit 1
			;;
	esac
done

shift $(($OPTIND - 1))

if [ $# -ne 2 ]
then
	usage
	exit 1
fi

DL_SITE=$1
DL_SOURCE=$2

if [ ! -r "$DL_SOURCE" ]
then
	echo -e "\033[1mSTEP 0: DOWNLOAD\033[0m"

	# Download firmware
	echo "downloading firmware image"

	case "$DL_SITE" in
		ftp://*)
			wget --passive-ftp "${DL_SITE}/${DL_SOURCE}"
			exitval=$?
			;;
		http://*)
			wget "${DL_SITE}/${DL_SOURCE}"
			exitval=$?
			;;
		/*)
			cp "${DL_SITE}/${DL_SOURCE}" .
			exitval=$?
			;;
		*)
			echo "download site not supported!" 1>&2
			exit 1
			;;
	esac

	if [ "$exitval" -ne 0 ]
	then
		exit $exitval
	fi

	echo "done."
	echo ""
fi