2 # Update GeoIP packages to new version provided by MaxMind.
4 # Author: Elan Ruusamäe <glen@pld-linux.org>
7 # 2012-07-04 Created initial version
8 # 2014-03-04 Rewritten to be smarter when checking for updates avoiding full download if no changes.
9 # 2014-06-06 Fix finding new versions if multiple previous archives were present
10 # 2015-08-25 Add auto commit support
17 while [ $# -gt 0 ]; do
19 update|-u|-update|--update)
23 status|-s|-status|--status)
33 # get file DATE in GMT timezone
36 TZ=GMT stat -c '%y' "$file" | awk '{print $1}'
39 # get package, no sources
42 out=$(builder -g -ns $pkg 2>&1) || echo "$out"
46 local specfile=$1 t url
49 builder -su $specfile 2>/dev/null > $t
63 local specfile=$1 url fn z
66 # take output filename (anything after last slash)
68 # remove querystring for mtime match to work
70 test -e "$fn" && z= || unset z
71 curl ${z+-z "$fn"} -o "$fn" "$url" -R -s
75 # set version to $version in $specfile and build the package
77 local specfile="$1" version="$2" out
81 s/^\(Version:[ \t]\+\)[.0-9]\+\$/\1$version/
82 s/^\(Release:[ \t]\+\)[.0-9]\+\$/\11/
86 out=$(md5 -p1 $specfile 2>&1) || echo "$out"
89 out=$(builder -bb $specfile 2>&1) || echo "$out"
93 local specfile="$1" version="$2"
95 git commit -m "updated to $version" $specfile
96 git push || echo push failed
99 # get version from package files
100 # set $version variable
101 version_from_files() {
102 local pkg=$1 url fn dt d
106 # take output filename (anything after last slash)
108 # skip inexistent files
109 test -e "$fn" || continue
112 if [ "$(echo $d | tr -d -)" -gt "$(echo $dt | tr -d -)" ]; then
119 version=$(echo "$dt" | tr -d -)
122 version=$(echo "$dt" | tr - .)
127 version_from_attachment() {
132 curl -Is "$url" -o "$t"
133 fn=$(awk 'BEGIN {FS=": "}/^Content-Disposition/{sub(/.*filename=/, "", $2); print $2}' "$t")
134 fn=${fn#GeoLite2-Country-CSV_}
135 d=$(echo "$fn" | sed -e 's/[^0-9-]//g')
137 if [ "$d" -gt "$dt" ]; then
147 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
151 pkgs='GeoIP-db-City GeoIP-db-Country GeoIP-db-IPASNum xtables-geoip'
152 for pkg in ${*:-$pkgs}; do
160 urls=$(get_urls $specfile)
161 if [ "$pkg" = "xtables-geoip" ]; then
162 version_from_attachment $urls
165 version_from_files $pkg $urls
168 oldvers=$(awk '/^Version:[ ]+/{print $NF}' $specfile)
169 if [ "$oldvers" != "$version" ]; then
170 update_version $specfile $version
172 commit_vcs $specfile $version
178 # report each package git status
179 for pkg in ${*:-$pkgs}; do
183 git status --porcelain
184 git status | grep ahead || :
186 test -n "$out" && echo "$pkg: $out"