#!/bin/bash

# ip2clue downloader
# Author: Catalin(ux) M. BOIE
# Home: http://www.ip2location.com/IPV6-COUNTRY.aspx
# TODO: Use ETag to not download again the data!
# WARNING! This is only a sample!

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

set -e

cd /var/cache/ip2clue

data_file="IPV6-COUNTRY.SAMPLE.ZIP"

if [ -r "${data_file}" ]; then
	mtime=`stat --format="%Z" ${data_file}`
	next=$[${mtime} + ${ip_to_location} * 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://www.ip2location.com/samples/IPV6-COUNTRY.SAMPLE.ZIP" -O "${data_file}"
unzip -o "${data_file}" "IPV6-COUNTRY.SAMPLE.BIN" 1>/dev/null
rm -f "${data_file}"
