#!/bin/sh
GEOIP_MIRROR="http://geolite.maxmind.com/download/geoip/database"
GEOIPDIR=/usr/share/GeoIP
TMPDIR=
EMAIL_TO="tdperry@ride-texas.org"
EMAIL_SUBJECT="[WARNING] GeoIP update error"
EMAIL_HEAD="Dear Master,"
EMAIL_CONTENT="There was an error updating the following GeoIP databases:"
EMAIL_FIX="Don't forget to launch a new update manually. No new automatic update will be done until next week!"
EMAIL_FOOT="Your Slave, from Palestine, TX (USA)."
EMAIL_META="VM: TwoWheelDemons - DNS: twowheeldemon.com - DC: Ram Node"
EMAIL_BUGGY=""
DATABASES="GeoLiteCity GeoIP"
if [ -d "${GEOIPDIR}" ]; then
cd $GEOIPDIR
if [ -n "${DATABASES}" ]; then
TMPDIR=$(mktemp -d geoipupdate.XXXXXXXXXX)
echo "Updating GeoIP databases..."
for db in $DATABASES; do
fname=$(basename $db)
wget --no-verbose -t 3 -T 60 "${GEOIP_MIRROR}/${db}.dat.gz" -O "${TMPDIR}/${fname}.dat.gz"
if [ "$?" = "0" ]; then
gunzip -fdc "${TMPDIR}/${fname}.dat.gz" > "${TMPDIR}/${fname}.dat"
mv "${TMPDIR}/${fname}.dat" "${GEOIPDIR}/${fname}.dat"
chmod 0644 "${GEOIPDIR}/${fname}.dat"
mv /usr/share/GeoIP/GeoIPLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
else
if [ "${EMAIL_BUGGY}" = "" ]; then
EMAIL_BUGGY=$db
else
EMAIL_BUGGY=${EMAIL_BUGGY}", $db"
fi
fi
done
[ -d "${TMPDIR}" ] && rm -rf $TMPDIR
if [ "${EMAIL_BUGGY}" != "" ]; then
echo "${EMAIL_HEAD}\n\n${EMAIL_CONTENT} ${EMAIL_BUGGY}\n\n${EMAIL_FIX}\n\n\n${EMAIL_FOOT}\n\n\n${EMAIL_META}" | /usr/bin/mail -s "${EMAIL_SUBJECT}" "${EMAIL_TO}"
fi
fi
fi