#! /bin/sh
# Update the MaxMind free GeoLite databases
# by:  Gianluca Toso <gtoso@presso.net>

GEODBDIR="/usr/share/GeoIP";

GEOCOUNTRYURL="http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz";
GEOCITYURL="http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz";

if [ ! -d "$GEODBDIR" ]; then
  echo "Please set the correct directory target to install GeoIP DB";
  exit 1;
fi;

update_country() {
  cd "$GEODBDIR" \
  && wget -q -O GeoIP.dat.gz $GEOCOUNTRYURL \
  && gzip -fd GeoIP.dat.gz \
  || { echo "ERROR: Can't update GeoIP Country DB!" >&2; exit 1; }
}

update_city() {
  cd "$GEODBDIR" \
  && wget -q -O GeoIPCity.dat.gz $GEOCITYURL \
  && gzip -fd GeoIPCity.dat.gz \
  || { echo "ERROR: Can't update GeoIP City DB!" >&2; exit 1; }
}

case "$1" in
-h|--help)
  cat << EOF;
GeoIPUpdate.sh 0.0.2 - Copyright 2008  Gianluca Toso
A script to updating the MaxMind free GeoLite databases.

	Usage:
	  	$0 [-C | -c | -a]
		$0 -h|--help

	Options:
		-C	update Country DB (default).
		-c	update City DB.
		-a	update all DB.
		-h	prints help (this message).

	Notes:
		Any incorrect or unknown parameter will be silently ignored.

EOF
  ;;
-C)
  update_country;
  ;;
-c)
  update_city;
  ;;
-a)
  update_country;
  update_city;
  ;;
*)
  update_country;
  ;;
esac;

