]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - update-geoip.sh
- /etc/shrc.d/* files as configs
[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 #
6 # Changelog:
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
11
12 set -e
13
14 update=false
15 status=false
16 commit=true
17 while [ $# -gt 0 ]; do
18         case "$1" in
19         update|-u|-update|--update)
20                 update=true
21                 shift
22                 ;;
23         status|-s|-status|--status)
24                 status=true
25                 shift
26                 ;;
27         *)
28                 break
29                 ;;
30         esac
31 done
32
33 # get file DATE in GMT timezone
34 filedate() {
35         local file="$1"
36         TZ=GMT stat -c '%y' "$file" | awk '{print $1}'
37 }
38
39 # get package, no sources
40 get_package() {
41         local pkg=$1 out
42         out=$(builder -g -ns $pkg 2>&1) || echo "$out"
43 }
44
45 get_urls() {
46         local specfile=$1 t url
47
48         t=$(mktemp)
49         builder -su $specfile 2>/dev/null > $t
50
51         while read url; do
52                 # skip non-archives
53                 case "$url" in
54                 *.zip|*.gz|*.xz)
55                         echo "$url"
56                         ;;
57                 esac
58         done < $t
59         rm -f $t
60 }
61
62 update_urls() {
63         local specfile=$1 url fn z
64
65         for url in "$@"; do
66                 # take output filename (anything after last slash)
67                 fn=${url##*/}
68                 # remove querystring for mtime match to work
69                 url=${url%\?*}
70                 test -e "$fn" && z= || unset z
71                 curl ${z+-z "$fn"} -o "$fn" "$url" -R -s
72         done
73 }
74
75 # set version to $version in $specfile and build the package
76 update_version() {
77         local specfile="$1" version="$2" out
78
79         # update version
80         sed -i -e "
81                 s/^\(Version:[ \t]\+\)[.0-9]\+\$/\1$version/
82                 s/^\(Release:[ \t]\+\)[.0-9]\+\$/\11/
83         " $specfile
84
85         # update md5
86         out=$(md5 -p1 $specfile 2>&1) || echo "$out"
87
88         # build it
89         out=$(builder -bb $specfile 2>&1) || echo "$out"
90 }
91
92 commit_vcs() {
93         local specfile="$1" version="$2"
94
95         git commit -m "updated to $version" $specfile
96         git push || echo push failed
97 }
98
99 # get version from package files
100 # set $version variable
101 version_from_files() {
102         local pkg=$1 url fn dt d
103         shift
104
105         for url in "$@"; do
106                 # take output filename (anything after last slash)
107                 fn=${url##*/}
108                 # skip inexistent files
109                 test -e "$fn" || continue
110
111                 d=$(filedate "$fn")
112                 if [ "$(echo $d | tr -d -)" -gt "$(echo $dt | tr -d -)" ]; then
113                         dt=$d
114                 fi
115         done
116
117         case "$pkg" in
118         xtables-geoip)
119                 version=$(echo "$dt" | tr -d -)
120                 ;;
121         *)
122                 version=$(echo "$dt" | tr - .)
123                 ;;
124         esac
125 }
126
127 dir=$(dirname "$0")
128 APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
129 PATH=$APPDIR:$PATH
130 cd "$dir"
131
132 pkgs='GeoIP-db-City GeoIP-db-Country GeoIP-db-IPASNum xtables-geoip'
133 for pkg in ${*:-$pkgs}; do
134         pkg=${pkg%.spec}
135         $status && continue
136
137         get_package $pkg
138         cd $pkg
139         specfile=*.spec
140
141         urls=$(get_urls $specfile)
142         update_urls $urls
143         version_from_files $pkg $urls
144         oldvers=$(awk '/^Version:[      ]+/{print $NF}' $specfile)
145         if [ "$oldvers" != "$version" ]; then
146                 update_version $specfile $version
147                 if $commit; then
148                         commit_vcs $specfile $version
149                 fi
150         fi
151         cd ..
152 done
153
154 # report each package git status
155 for pkg in ${*:-$pkgs}; do
156         pkg=${pkg%.spec}
157         cd $pkg
158         out=$(
159         git status --porcelain
160         git status | grep ahead || :
161         )
162         test -n "$out" && echo "$pkg: $out"
163         cd ..
164 done
This page took 0.040451 seconds and 3 git commands to generate.