# Update GeoIP packages to new version provided by MaxMind.
#
# Author: Elan Ruusamäe <glen@pld-linux.org>
+#
+# Changelog:
# 2012-07-04 Created initial version
# 2014-03-04 Rewritten to be smarter when checking for updates avoiding full download if no changes.
+# 2014-06-06 Fix finding new versions if multiple previous archives were present
+# 2015-08-25 Add auto commit support
set -e
update=false
status=false
+commit=true
while [ $# -gt 0 ]; do
case "$1" in
update|-u|-update|--update)
esac
done
+die() {
+ echo >&2 "$0: ERROR: $*"
+ exit 1
+}
+
# get file DATE in GMT timezone
filedate() {
local file="$1"
out=$(builder -g -ns $pkg 2>&1) || echo "$out"
}
-update_urls() {
- local specfile=$1 t fn z
- # update urls
+get_urls() {
+ local specfile=$1 t url
+
t=$(mktemp)
- builder -su $specfile > $t 2>/dev/null
+ builder -su $specfile 2>/dev/null > $t
+
while read url; do
+ # skip non-archives
+ case "$url" in
+ *.zip|*.gz|*.xz)
+ echo "$url"
+ ;;
+ esac
+ done < $t
+ rm -f $t
+}
+
+update_urls() {
+ local specfile=$1 url fn z
+
+ for url in "$@"; do
# take output filename (anything after last slash)
fn=${url##*/}
# remove querystring for mtime match to work
url=${url%\?*}
test -e "$fn" && z= || unset z
curl ${z+-z "$fn"} -o "$fn" "$url" -R -s
- done < $t
- rm -f $t
+ done
}
# set version to $version in $specfile and build the package
s/^\(Release:[ \t]\+\)[.0-9]\+\$/\11/
" $specfile
+ rm *.zip *.gz *.xz
# update md5
out=$(md5 -p1 $specfile 2>&1) || echo "$out"
out=$(builder -bb $specfile 2>&1) || echo "$out"
}
+commit_vcs() {
+ local specfile="$1" version="$2"
+
+ git commit -m "updated to $version" $specfile
+ git push || echo push failed
+}
+
# get version from package files
# set $version variable
version_from_files() {
- local pkg=$1 dt4 dt6
+ local pkg=$1 url fn dt d
+ shift
+
+ for url in "$@"; do
+ # take output filename (anything after last slash)
+ fn=${url##*/}
+ # skip inexistent files
+ test -e "$fn" || continue
+
+ d=$(filedate "$fn")
+ if [ "$(echo $d | tr -d -)" -gt "$(echo $dt | tr -d -)" ]; then
+ dt=$d
+ fi
+ done
+
case "$pkg" in
xtables-geoip)
- dt4=$(filedate *.zip | tr -d -)
- dt6=$(filedate *.gz | tr -d -)
- if [ "$dt4" -gt "$dt6" ]; then
- version=$dt4
- else
- version=$dt6
- fi
- ;;
- GeoIP-db-City)
- dt4=$(filedate GeoLiteCity-*.dat.xz | tr - .)
- dt6=$(filedate GeoLiteCityv6-*.dat.gz | tr - .)
- if [ "$(echo $dt4 | tr -d .)" -gt "$(echo $dt6 | tr -d .)" ]; then
- version=$dt4
- else
- version=$dt6
- fi
- ;;
- GeoIP-db-Country)
- dt4=$(filedate GeoIP-*.dat.gz | tr - .)
- dt6=$(filedate GeoIPv6-*.dat.gz | tr - .)
- if [ "$(echo $dt4 | tr -d .)" -gt "$(echo $dt6 | tr -d .)" ]; then
- version=$dt4
- else
- version=$dt6
- fi
+ version=$(echo "$dt" | tr -d -)
;;
*)
- version=$(filedate *.gz | tr - .)
+ version=$(echo "$dt" | tr - .)
;;
esac
}
+version_from_attachment() {
+ local url fn dt d t
+ t=$(mktemp)
+
+ for url in "$@"; do
+ # remove querystring to receive proper headers
+ url=${url%\?*}
+ curl -Is "$url" -o "$t"
+ fn=$(awk 'BEGIN {FS=": "}/^[Cc]ontent-[Dd]isposition/{sub(/.*filename=/, "", $2); print $2}' "$t")
+ fn=${fn#GeoLite2-Country-CSV_}
+ d=$(echo "$fn" | sed -e 's/[^0-9-]//g')
+
+ if [ "$d" -gt "$dt" ]; then
+ dt=$d
+ fi
+ done
+
+ test -n "$dt" || die "Failed to find date from $*"
+
+ rm -f $t
+ version=$dt
+}
+
dir=$(dirname "$0")
APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
PATH=$APPDIR:$PATH
pkgs='GeoIP-db-City GeoIP-db-Country GeoIP-db-IPASNum xtables-geoip'
for pkg in ${*:-$pkgs}; do
+ pkg=${pkg%.spec}
$status && continue
get_package $pkg
cd $pkg
specfile=*.spec
- update_urls $specfile
- version_from_files $pkg
+ urls=$(get_urls $specfile)
+ if [ "$pkg" = "xtables-geoip" ]; then
+ version_from_attachment $urls
+ else
+ update_urls $urls
+ version_from_files $pkg $urls
+ fi
+
oldvers=$(awk '/^Version:[ ]+/{print $NF}' $specfile)
if [ "$oldvers" != "$version" ]; then
update_version $specfile $version
+ if $commit; then
+ commit_vcs $specfile $version
+ fi
fi
cd ..
done
# report each package git status
for pkg in ${*:-$pkgs}; do
+ pkg=${pkg%.spec}
cd $pkg
+ out=$(
git status --porcelain
+ git status | grep ahead || :
+ )
+ test -n "$out" && echo "$pkg: $out"
cd ..
done