+
+update=false
+status=false
+commit=true
+while [ $# -gt 0 ]; do
+ case "$1" in
+ update|-u|-update|--update)
+ update=true
+ shift
+ ;;
+ status|-s|-status|--status)
+ status=true
+ shift
+ ;;
+ *)
+ break
+ ;;
+ esac
+done
+
+die() {
+ echo >&2 "$0: ERROR: $*"
+ exit 1
+}
+
+# get file DATE in GMT timezone
+filedate() {
+ local file="$1"
+ TZ=GMT stat -c '%y' "$file" | awk '{print $1}'
+}
+
+# get package, no sources
+get_package() {
+ local pkg=$1 out
+ out=$(builder -g -ns $pkg 2>&1) || echo "$out"
+}
+
+get_urls() {
+ local specfile=$1 t url
+
+ t=$(mktemp)
+ 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
+}
+
+# set version to $version in $specfile and build the package
+update_version() {
+ local specfile="$1" version="$2" out
+
+ # update version
+ sed -i -e "
+ s/^\(Version:[ \t]\+\)[.0-9]\+\$/\1$version/
+ s/^\(Release:[ \t]\+\)[.0-9]\+\$/\11/
+ " $specfile
+
+ rm *.zip *.gz *.xz
+ # update md5
+ out=$(md5 -p1 $specfile 2>&1) || echo "$out"
+
+ # build it
+ 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 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)
+ version=$(echo "$dt" | tr -d -)
+ ;;
+ *)
+ 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
+cd "$dir"
+