#!/bin/bash
# If the thing crashes, it might leave small empty holes in the file
# in that case, run rsync over it then stop it once it's covered the holes
# (file getting bigger) before continuing.  Sorry, no better fix for that yet.
# It could just leave the running child processes running.

if [ -e ~/.fastgetrc ]; then
	source ~/.fastgetrc
fi

export chunk_size PARALLEL_MAX NC_TIMEOUT no_url_encode

#trap "trap - INT; read -p 'kill fetchers? ' -n1 A ; echo; case _$A in _[yY]) kill -TERM 0;; esac" HUP INT TERM PIPE QUIT

prog="`readlink -f "$0"`"
case "$prog" in
/*) ;;
*) prog="$PWD/$prog"
esac
export PATH="`dirname "$prog"`:$PATH"

bytes=0
time0=`date +%s`

url="$1"
if [ -z "$url" ]; then
	echo >&2 "usage: ${0##*/} url [outfile]"
	exit 1
fi
out="$2"
if [ -z "$out" ]; then out=`basename "$url"`; out="${out%%\?*}"; fi
size=`HEAD "$url" | sed -n 's/^Content-Length: //p'`
if [ -z "$size" ]; then
	echo >&2 "FIXME: missing Content-Length => cannot download!"
	echo >&2 HEAD "$url"
	HEAD "$url" >&2
	exit 1
fi
echo >&2 "size: $size"
chunk_size=${chunk_size:-$[32*1024]}
PARALLEL_MAX=${PARALLEL_MAX:-30}
nc_timeout=${nc_timeout:-10}

if [ -e "$out" ]; then
	start=$[`stat -c %s "$out"`]
else
	start=0
fi

from=$start

while true; do
	to=$[$from+$chunk_size-1]
	if [ $to -ge $size ]; then
		to=$[$size - 1]
	fi
	if [ $from -ge $to ]; then
		break
	fi
	. fg-parallel get-range-into "$url" "$out" $from $to
	echo >&2 "get-range-into $url $out $from $to"
	bytes=$[$bytes+$to-$from+1]
	from=$[$to+1]
done

wait

time1=`date +%s`
duration=$[$time1-$time0]
if [ $duration = 0 ]; then
	rate=X
else
	rate=$[$bytes/$duration]
fi

echo >&2 OK fast-get transferred $bytes in $duration seconds, at $rate bytes per second.
