#!/bin/sh
##########

# axelq
# 
# A queue manager for the popular download accelerator axel
# 
# You can download axel at: <http://www.lintux.cx/axel.html>
# axel is Copyright Wilmer van der Gaast <lintux@lintux.cx>
# axelq is Copyright Sjoerd Hemminga <sjoerd@huiswerkservice.nl>
# 
# Both programs are freely distributable under the GPL. See the file
# COPYING for details.

##########

version()
{
	cat <<EOH
axelq version 0.70

Copyright 2001-2002 Sjoerd Hemminga <sjoerd@huiswerkservice.nl>
axel is Copyright Wilmer van der Gaast <lintux@lintux.cx>
EOH
}

##########

help()
{
	version
	cat <<EOH

Usage: axelq [options]

-e <url>
	put <url> in the queue
-f <file>
	use <file> as the queue-file
-r
	start downloading, i.e. run axel
-c
	sort and clean the queue file
-v
	display version information
-h
	display this help text
--
	end of option parsing, the rest of the command-line will be ignored

Note: you must include the dash for every option, -re will not work.

Report bugs to <sjoerd@huiswerkservice.nl>.
EOH
}

##########

remove()
{
	tmp=`mktemp -q /tmp/axelq.XXXXXX`;
	if [ $? != 0 ]; then
		echo axelq: error creating temporary file
		echo axelq: download not removed
		exit 1
	fi
	tail -`expr \`cat ${qfile}|wc -l|sed "s/ //g"\` "-" 1` $qfile > $tmp
	mv $tmp $qfile
}

##########

add()
{
	echo $1 >> $qfile
}

##########

sortfile()
{
	if [ ! -r $qfile ]; then
		echo axelq: $qfile cannot be read. Does it exist?
		exit 1
	fi
	
	if [ ! -w $qfile ]; then
		echo axelq: cannot write to $qfile
		exit 1
	fi
	
	tmp=`mktemp -q /tmp/axelq.XXXXXX`;
	if [ $? != 0 ]; then
		echo axelq: error creating temporary file
		echo axelq: $qfile not sorted
		exit 1
	fi
	sort $qfile | uniq > $tmp
	mv $tmp $qfile
}

##########

failed ()
{
	if [ $1 = "ask" ]; then
		move="Move"
		remove="Remove"
		maintain="Maintain"
		
		echo Download $download has failed
		echo What do you want to do?
		
		echo Move will put the download in the end of the queue
		echo Remove will delete the file from the queue
		echo Maintain will cause axel to retry immediately
		
		select dofail in $move $remove $maintain; do
			if [ $dofail = $move ]; then
				failed move
			elif [ $dofail = $remove ]; then
				failed remove
			elif [ $dofail = $maintain ]; then
				failed maintain
			fi
			
			break
		done
	elif [ $1 = "move" ]; then
		echo Moving $download to end of queue...
		remove
		add $download
	elif [ $1 = "remove" ]; then
		echo Removing $download from queue...
		remove
	elif [ $1 != "maintain" ]; then
		echo axelq: unknown setting found: failaction: $failaction
		echo axelq: Valid failaction\'s: ask move remove maintain
		echo axelq: Case sensitive
		exit 1
	fi
}

##########

run()
{
	if [ ! -r $qfile ]; then
		echo axelq: $qfile cannot be read. Does it exist?
		exit 1
	fi
	
	if [ ! -w $qfile ]; then
		echo axelq: cannot write to $qfile
		exit 1
	fi
	
	while [ `cat ${qfile}|wc -l|sed "s/ //g"` -gt 0 ]; do
		download=`head -1 $qfile`
		state=`basename ${download}.st`
		$command $download
		exitcode=$?

		if [ $exitcode -eq 0 ]; then
				# Test to see if the state file exists...
				if [ ! -r $state ]; then
					echo axel says: download succesful
					echo Removing $download from queue...
					# remove url from top of the file only if the state file isn't there
					remove
				else exit 2
				fi
		elif [ $exitcode -eq 2 ]; then
			echo axel says: download interrupted
			echo axelq: Exiting...
			exit 0
		elif [ $exitcode -eq 1 ]; then
			echo axel says: download failed
			failed $failaction
		else
			echo axelq: axel talks gibberish
			echo axelq: Exiting...
			exit 1
		fi
	done
}

##########

# Default settings
qfile=~/.axelq
command="axel"
failaction="ask"

if [ -r /etc/axelq.rc ]; then
	source /etc/axelq.rc
fi

if [ -r ~/.axelqrc ]; then
	source ~/.axelqrc
fi

# Initialization defaults
didsomething=0

while [ -n "$1" ]; do
	case $1 in
		-h)
			help
			exit 0
		;;
		-v)
			version
			exit 0
		;;
		-f)
			qfile=$2
			shift 2
		;;
		-e)
			add $2
			echo $2 added to $qfile
			shift 2
			didsomething=1
		;;
		-r)
			run
			shift
			didsomething=1
		;;
		-c)
			sortfile
			echo $qfile sorted and cleaned
			shift
			didsomething=1
		;;
		--)
			shift
			break
		;;
		-*)
			echo axelq: no such option $1
			echo
			help
			exit 1
		;;
		*)
			break
		;;
	esac
done

if [ $didsomething -eq 0 ]; then
	help
fi


syntax highlighted by Code2HTML, v. 0.9.1