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)
34 echo >&2 "$0: ERROR: $*"
38 # get file DATE in GMT timezone
41 TZ=GMT stat -c '%y' "$file" | awk '{print $1}'
44 # get package, no sources
47 out=$(builder -g -ns $pkg 2>&1) || echo "$out"
51 local specfile=$1 t url
54 builder -su $specfile 2>/dev/null > $t
68 local specfile=$1 url fn z
71 # take output filename (anything after last slash)
73 # remove querystring for mtime match to work
75 test -e "$fn" && z= || unset z
76 curl ${z+-z "$fn"} -o "$fn" "$url" -R -s
80 # set version to $version in $specfile and build the package
82 local specfile="$1" version="$2" out
86 s/^\(Version:[ \t]\+\)[.0-9]\+\$/\1$version/
87 s/^\(Release:[ \t]\+\)[.0-9]\+\$/\11/
92 out=$(md5 -p1 $specfile 2>&1) || echo "$out"
95 out=$(builder -bb $specfile 2>&1) || echo "$out"
99 local specfile="$1" version="$2"
101 git commit -m "updated to $version" $specfile
102 git push || echo push failed
105 # get version from package files
106 # set $version variable
107 version_from_files() {
108 local pkg=$1 url fn dt d
112 # take output filename (anything after last slash)
114 # skip inexistent files
115 test -e "$fn" || continue
118 if [ "$(echo $d | tr -d -)" -gt "$(echo $dt | tr -d -)" ]; then
125 version=$(echo "$dt" | tr -d -)
128 version=$(echo "$dt" | tr - .)
133 version_from_attachment() {
138 # remove querystring to receive proper headers
140 curl -Is "$url" -o "$t"
141 fn=$(awk 'BEGIN {FS=": "}/^[Cc]ontent-[Dd]isposition/{sub(/.*filename=/, "", $2); print $2}' "$t")
142 fn=${fn#GeoLite2-Country-CSV_}
143 d=$(echo "$fn" | sed -e 's/[^0-9-]//g')
145 if [ "$d" -gt "$dt" ]; then
150 test -n "$dt" || die "Failed to find date from $*"
157 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
161 pkgs='GeoIP-db-City GeoIP-db-Country GeoIP-db-IPASNum xtables-geoip'
162 for pkg in ${*:-$pkgs}; do
170 urls=$(get_urls $specfile)
171 if [ "$pkg" = "xtables-geoip" ]; then
172 version_from_attachment $urls
175 version_from_files $pkg $urls
178 oldvers=$(awk '/^Version:[ ]+/{print $NF}' $specfile)
179 if [ "$oldvers" != "$version" ]; then
180 update_version $specfile $version
182 commit_vcs $specfile $version
188 # report each package git status
189 for pkg in ${*:-$pkgs}; do
193 git status --porcelain
194 git status | grep ahead || :
196 test -n "$out" && echo "$pkg: $out"