42703be
#! /bin/bash
42703be
# Copyright (C) 2008 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
42703be
#
42703be
# This program is free software; you can redistribute it and/or modify
42703be
# it under the terms of the GNU General Public License as published by
42703be
# the Free Software Foundation; version 3 of the License.
42703be
#
42703be
# This program is distributed in the hope that it will be useful,
42703be
# but WITHOUT ANY WARRANTY; without even the implied warranty of
42703be
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42703be
# GNU General Public License for more details.
42703be
#
42703be
# You should have received a copy of the GNU General Public License
42703be
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
42703be
42703be
### NOTE: this program is part of the Fedora 'tor' package. Hence, file
42703be
### bugreports at http://bugzilla.redhat.com, but *NOT* at upstream tor
42703be
### webpage/maillists.
42703be
42703be
42703be
URL=http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip
42703be
: ${DESTDIR=/var/lib/tor-data}
42703be
42703be
set -e
42703be
d=$(mktemp -d -t tor-geoip.XXXXXX)
42703be
trap "rm -rf $d" EXIT
42703be
42703be
cd $d
42703be
wget "$URL"
42703be
unzip "$(basename "$URL")"
42703be
42703be
date=$(stat -c '%Y' ip-to-country.csv)
42703be
date=$(date +"%Y%m%dT%H%M%S" -d "1970-01-01 00:00:00 UTC $date seconds")
42703be
fname=geoip-$date
42703be
42703be
test ! -e "$DESTDIR/$fname" || exit 0
42703be
42703be
cut -d, -f1-3 < ip-to-country.csv | sed 's/"//g' > "$fname"
42703be
rm -f "$DESTDIR/$fname"
42703be
cp -p "$fname" "$DESTDIR/$fname"
42703be
rm -f "$DESTDIR/geoip"
42703be
ln -s "$fname" "$DESTDIR/geoip"