#!/bin/bash

# Script originally from jk

maxconnections=10

if [ $# -eq 0 ]
then
	echo "Usage: $0 <packages>" >&2
	exit 1
fi

D=$(mktemp -d)
cd $D

apt-get -y --print-uris "$@" |
sed -ne "s/^'\(http.*\)' \([^_]*\)_.* .*$/\2/p" |
(
	while read url;
	do
		while [ $(jobs |wc -l) -ge $maxconnections ]
		do
			sleep 1
		done
		echo prefetching $url
		apt-get -q download $url &
	done
	wait
)

cd - > /dev/null
rm -rf $D
