#!/bin/sh
#
# absDM - absolute downoad manager
# Little helper script to download with Midori
# c2009 by Paul Sherman for Absolute Linux
# GPL2

test -z "$1" && exit 1

if [ -d $HOME/Desktop/Downloads ];then
	dl=$HOME/Desktop/Downloads
else
	dl=$HOME
fi

f=$(basename $1)

DIR=`Xdialog --title "Download Directory" --dselect $dl 0 0 2>&1`

case $? in
	0)
		 ddir="$DIR" ;;
	1)
		exit ;;
	255)
		exit ;;
esac

if [ -e "$ddir/$f" ]; then
	Xdialog --wrap --title "File Exists Already"\
        	--yesno "\n$ddir$f\n \
			already exists.\nDownload the file somewhere else?\n" 0 0

	case $? in
	  0)
		newdir="1" ;;
	  1)
    	exit ;;
	  255)
    	exit ;;
	esac
fi

if [ $newdir ]; then
	nDIR=`Xdialog --title "Download Directory" --dselect $HOME 0 0 2>&1`
	case $? in
		0)
			newdir="$nDIR" ;;
		1)
			exit ;;
		255)
			exit ;;
	esac
fi
if [ $newdir ]; then
	ddir="$newdir"
fi	
cd "$ddir"
xterm -bg gray88 -geom 72x20 -e wget $1
if [ -e "$ddir/$f" ]; then
	Xdialog --title "Complete" --infobox "\nDownload Complete\n" 0 0 6000
else
	Xdialog --title "Error" --infobox "\nDownload Failed\n" 0 0 6000
fi
