Skip to content

Instantly share code, notes, and snippets.

@legionus
Last active September 20, 2021 21:09
Show Gist options
  • Save legionus/f8ab3634a66723a0dfc09cdad2e5321b to your computer and use it in GitHub Desktop.
Save legionus/f8ab3634a66723a0dfc09cdad2e5321b to your computer and use it in GitHub Desktop.
#!/bin/bash -efu
. shell-error
. shell-temp
get_apt_config()
{
local get_eval get_name get_value
get_name="$1"
shift || return 1
get_value="$1"
shift || return 1
get_eval="$(apt-config shell "$get_value" "$get_name" </dev/null)" || return 1
eval "$get_eval"
}
create_temporary workdir
get_apt_config Dir::Cache::archives/d apt_archives
apt_archives="${apt_archives%/}"
apt-get install -qq --print-uris "$@" > "$workdir/pkgs"
total_size=0
while read -r s; do
csum="${s##* }"; s="${s% *}"
size="${s##* }"; s="${s% *}"
name="${s##* }"; s="${s% *}"
url="${s#\'}"; url="${url%\'}"
printf -v fname '%b' "${url//%/\\x}"
fname="${fname##*/}"
algo=md5
if [ -z "${csum##*:*}" ]; then
algo="${csum%%:*}"
algo="${algo,,}"
csum="${csum##*:}"
fi
case "$algo" in
md5|sha1|sha256)
;;
*)
fatal "unknown checksum algorithm: $algo"
;;
esac
printf '%s\n' "$url" >> "$workdir/uris"
printf '%s %s\n' "$csum" "$fname" >> "$workdir/checksum.$algo"
printf '%s %s\n' "$fname" "$name" >> "$workdir/meta"
total_size=$(( $total_size + $size ))
done < "$workdir/pkgs"
if [ -s "$workdir/uris" ]; then
if [ -t 1 ]; then
printf 'Need to get %s of archives.\n' "$(numfmt --to=iec $total_size)"
read -p 'Do you want to continue? [Y/n] ' ans
ans="${ans:-y}"
if [ "${ans,,}" != "y" ]; then
printf 'Abort.\n'
exit
fi
fi
mkdir -p -- "$apt_archives/downloads"
pushd "$apt_archives"
aria2c \
--download-result=hide \
--max-concurrent-downloads=5 \
--dir="downloads" \
--input-file="$workdir/uris" \
#
pushd "downloads"
for checksum in $(set +f && printf '%s ' "$workdir"/checksum.*); do
"${checksum##*.}sum" --quiet -c "$checksum"
done
while read -r fname name; do
mv -f -- "$fname" ".."
done < "$workdir/meta"
popd > /dev/null
popd > /dev/null
fi
apt-get install -y "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment