#!/bin/sh

show_version() {
	echo "GWARE (I) - GNOME 2.22.0 for Slackware 12.1"
}

check_progs() {
	wget --version &> /dev/null
	if [ ! $? = 0 ]; then
		echo "$0: wget required but not found!"
		exit 0
	fi
}

preset_mirror() {
	if [ -z "$1" ]; then
		get_mirror
	else
		MIRROR="$1"
	fi
}

get_mirror() {
	echo
	echo "1 - GWARE FTP (Official Mirror, US, high bandwidth)"
#	echo "2 - WebSight Designs HTTP (Official Mirror, US, high bandwidth)"
#	echo "3 - Darkstar FTP (Unofficial, Portugal, high bandwidth)"
#	echo "4 - Darkstar HTTP (Unofficial, Portugal, high bandwidth)"
	echo "Q - Quit"
	echo
	read -n 1 -p "Select Mirror: "
	echo

	case $REPLY in
		1) MIRROR="ftp://ftp.gware.org/12.1/2.22.0/packages/";;
#		2) MIRROR="http://www.websightdesigns.com/files/gware/12.1/2.22.0/packages/";;
#		3) MIRROR="ftp://darkstar.ist.utl.pt/pub/slackware/gware/12.1/2.22.0/packages/";;
#		4) MIRROR="http://darkstar.ist.utl.pt/slackware/gware/12.1/2.22.0/packages/";;
		[Qq]) echo;exit 0;;
		*) echo;echo "Invalid Selection";exit 0;;
	esac
}

get_list() {
	MIRROR_BASE=`echo $1 | awk -F "/" '{ print $3 }'`
	echo -n "Fetching package list from $MIRROR_BASE ... "
	PACKAGES=`wget --quiet -O - $1 | grep "PACKAGE NAME:" | cut -d ' ' -f 4-4 | xargs`
}

show_version
check_progs
preset_mirror

get_list "$MIRROR/PACKAGES.TXT"
if [ ! "$PACKAGES" == "" ]; then
	echo -e "Done.\n"
else
	echo -e "Failed. Try another mirror.\n"
fi

for PACKAGE in $PACKAGES; do
	wget --progress=bar -c $MIRROR$PACKAGE
done

