#!/bin/bash

# ip2clue downloader
# Author: Catalin(ux) M. BOIE
# Home: http://www.maxmind.com/app/geolitecountry
# TODO: Use ETag to not download again the data!

source /etc/ip2clue/download.conf
if [ "${maxmind_ipv6}" = "0" ]; then
	exit 0
fi

set -e

cd /var/cache/ip2clue

data_file="maxmind-ipv6.csv.gz"

if [ -r "${data_file}" ]; then
	mtime=`stat --format="%Z" ${data_file}`
	next=$[${mtime} + ${maxmind_ipv6} * 24 * 3600]
else
	next=0
fi
echo "mtime=${mtime}, next=${next}, now=`date +%s`"
if [ "${next}" -ge "`date +%s`" ]; then
	# do not update yet
	exit 0
fi

# wait some random time...
sleep $[${RANDOM}%120]

wget "http://geolite.maxmind.com/download/geoip/database/GeoIPv6-20100330.csv.gz" -O "${data_file}"
gzip -d "${data_file}"
