]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - update-geoip.sh
fix version detect if old database files lying around
[packages/rpm-build-tools.git] / update-geoip.sh
1 #!/bin/sh
2 # Update GeoIP packages to new version provided by MaxMind.
3 #
4 # Author: Elan Ruusamäe <glen@pld-linux.org>
5 # 2012-07-04 Created initial version
6 # 2014-03-04 Rewritten to be smarter when checking for updates avoiding full download if no changes.
7 # 2014-06-06 Fix finding new versions if multiple previous archives were present
8
9 set -e
10
11 update=false
12 status=false
13 while [ $# -gt 0 ]; do
14         case "$1" in
15         update|-u|-update|--update)
16                 update=true
17                 shift
18                 ;;
19         status|-s|-status|--status)
20                 status=true
21                 shift
22                 ;;
23         *)
24                 break
25                 ;;
26         esac
27 done
28
29 # get file DATE in GMT timezone
30 filedate() {
31         local file="$1"
32         TZ=GMT stat -c '%y' "$file" | awk '{print $1}'
33 }
34
35 # get package, no sources
36 get_package() {
37         local pkg=$1 out
38         out=$(builder -g -ns $pkg 2>&1) || echo "$out"
39 }
40
41 get_urls() {
42         local specfile=$1 t url
43
44         t=$(mktemp)
45         builder -su $specfile 2>/dev/null > $t
46
47         while read url; do
48                 # skip non-archives
49                 case "$url" in
50                 *.zip|*.gz|*.xz)
51                         echo "$url"
52                         ;;
53                 esac
54         done < $t
55         rm -f $t
56 }
57
58 update_urls() {
59         local specfile=$1 url fn z
60
61         for url in "$@"; do
62                 # take output filename (anything after last slash)
63                 fn=${url##*/}
64                 # remove querystring for mtime match to work
65                 url=${url%\?*}
66                 test -e "$fn" && z= || unset z
67                 curl ${z+-z "$fn"} -o "$fn" "$url" -R -s
68         done
69 }
70
71 # set version to $version in $specfile and build the package
72 update_version() {
73         local specfile="$1" version="$2" out
74
75         # update version
76         sed -i -e "
77                 s/^\(Version:[ \t]\+\)[.0-9]\+\$/\1$version/
78                 s/^\(Release:[ \t]\+\)[.0-9]\+\$/\11/
79         " $specfile
80
81         # update md5
82         out=$(md5 -p1 $specfile 2>&1) || echo "$out"
83
84         # build it
85         out=$(builder -bb $specfile 2>&1) || echo "$out"
86 }
87
88 # get version from package files
89 # set $version variable
90 version_from_files() {
91         local pkg=$1 url fn dt d
92         shift
93
94         for url in "$@"; do
95                 # take output filename (anything after last slash)
96                 fn=${url##*/}
97                 # skip inexistent files
98                 test -e "$fn" || continue
99
100                 d=$(filedate "$fn")
101                 if [ "$(echo $d | tr -d -)" -gt "$(echo $dt | tr -d -)" ]; then
102                         dt=$d
103                 fi
104         done
105
106         case "$pkg" in
107         xtables-geoip)
108                 version=$(echo "$dt" | tr -d -)
109                 ;;
110         *)
111                 version=$(echo "$dt" | tr - .)
112                 ;;
113         esac
114 }
115
116 dir=$(dirname "$0")
117 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
118 PATH=$APPDIR:$PATH
119 cd "$dir"
120
121 pkgs='GeoIP-db-City GeoIP-db-Country GeoIP-db-IPASNum xtables-geoip'
122 for pkg in ${*:-$pkgs}; do
123         pkg=${pkg%.spec}
124         $status && continue
125
126         get_package $pkg
127         cd $pkg
128         specfile=*.spec
129
130         urls=$(get_urls $specfile)
131         update_urls $urls
132         version_from_files $pkg $urls
133         oldvers=$(awk '/^Version:[      ]+/{print $NF}' $specfile)
134         if [ "$oldvers" != "$version" ]; then
135                 update_version $specfile $version
136         fi
137         cd ..
138 done
139
140 # report each package git status
141 for pkg in ${*:-$pkgs}; do
142         pkg=${pkg%.spec}
143         cd $pkg
144         git status --porcelain
145         cd ..
146 done
This page took 0.082789 seconds and 4 git commands to generate.