#!/bin/sh
# https://gist.github.com/smoser/1d441e6c8514c9c6edc9df2a9903e803
package=${1:-resolvconf}
version=${2}
last=""
mirror="http://us.archive.ubuntu.com/ubuntu/"
pocket="${POCKET:-bionic-proposed}"
component="main"
arch=amd64
case "$pocket" in
trusty*) ext=".bz2";;
*) ext=".xz";;
esac
url="${mirror}dists/$pocket/$component/binary-$arch/Packages$ext"
error() { echo "$(date -R)" "$@" 1>&2; }
fail() { echo "$@" 1>&2; exit 1; }
find_pkg() {
awk '{
if ($1 == "Package:") cpkg=$2;
else if ($1 == "Version:") cver=$2;
else if ($1 == "Source:") csrc=$2;
else if ($1 == "") {
if (pkg == csrc || pkg == cpkg) {
printf("%s\n", cver)
exit(0);
}
csrc=""; cpkg=""; cver="";
}
}' "pkg=$1" "$2"
}
case "${url##*/}" in
*.gz) decomp="zcat";;
*.bz2) decomp="bzcat";;
*.xz) decomp="xzcat";;
*) decomp="cat";;
esac
naplen=4m
lastmod=""
lastpkg=""
while :; do
headers=$(curl --fail --silent --head "$url") ||
fail "failed to download headers $url"
curmod=$(echo "$headers" | sed 's,
,,' |
awk -F: '$1 == "Last-Modified" { print $0 }')
curmod=${curmod#*: }
[ -n "$curmod" ] || fail "no Last-Modified in headers: $headers"
if [ "$curmod" = "$lastmod" ]; then
error "sleeping $naplen, not updated ($lastmod)"
else
compf="${url##*/}"
uncompf=${compf%.*}
curl --fail --silent "$url" > "${url##*/}" ||
fail "failed to download $url"
$decomp "$compf" > "$uncompf.tmp" && mv "$uncompf.tmp" "$uncompf" ||
fail "failed decompress $uncompf"
if found_ver=$(find_pkg "$package" "$uncompf") &&
[ -n "$found_ver" ]; then
if [ -z "$version" ]; then
echo "found $package/$found_ver in ${url#$mirror}"
exit 0
elif dpkg --compare-versions "${found_ver}" ge "${version}"; then
echo "found $package/$found_ver (>=$version) in ${url#$mirror}"
exit 0
else
echo "found $package/$found_ver (<$version) in ${url#$mirror}"
fi
else
echo "No $package in $url"
fi
fi
lastmod="$curmod"
echo "sleeping $naplen"
sleep $naplen
done
# vi: ts=4 expandtab