#!/bin/bash
#
# quick and dirty MaxMind database downloader; assumes your data lives in /var/lib/maxmind
#
# (c) th@bogus.net
#
cd /var/lib/maxmind
PATH=/bin:/usr/bin

# Database URL
wget 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=INSERT_YOUR_LICENCE_KEY_HERE&suffix=tar.gz' -O GeoLite2-ASN.tar.gz

# SHA256 URL
wget 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&license_key=INSERT_YOUR_LICENCE_KEY_HERE&suffix=tar.gz.sha256' -O GeoLite2-ASN.tar.gz.sha256
perl -pi -e 's/_\d+//g' GeoLite2-ASN.tar.gz.sha256

# Database URL
wget 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=INSERT_YOUR_LICENCE_KEY_HERE&suffix=tar.gz' -O GeoLite2-City.tar.gz

# SHA256 URL
wget 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=INSERT_YOUR_LICENCE_KEY_HERE&suffix=tar.gz.sha256' -O GeoLite2-City.tar.gz.sha256
perl -pi -e 's/_\d+//g' GeoLite2-City.tar.gz.sha256

# Database URL
wget 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=INSERT_YOUR_LICENCE_KEY_HERE&suffix=tar.gz' -O GeoLite2-Country.tar.gz

# SHA256 URL
wget 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&license_key=INSERT_YOUR_LICENCE_KEY_HERE&suffix=tar.gz.sha256' -O GeoLite2-Country.tar.gz.sha256
perl -pi -e 's/_\d+//g' GeoLite2-Country.tar.gz.sha256

SHA=`sha256sum -c GeoLite2-ASN.tar.gz.sha256 | grep OK`

if [ -z "$SHA" ]
then
	echo "GeoLite2-ASN.tar.gz: SHA256 hash does not match, exiting."
	exit 1
else
	tar zxvf GeoLite2-ASN.tar.gz --strip-components=1
fi
SHA=`sha256sum -c GeoLite2-City.tar.gz.sha256 | grep OK`

if [ -z "$SHA" ]
then
	echo "GeoLite2-City.tar.gz: SHA256 hash does not match, exiting."
	exit 1
else
	tar zxvf GeoLite2-City.tar.gz --strip-components=1
fi

SHA=`sha256sum -c GeoLite2-Country.tar.gz.sha256 | grep OK`

if [ -z "$SHA" ]
then
	echo "GeoLite2-Country.tar.gz: SHA256 hash does not match, exiting."
	exit 1
else
	tar zxvf GeoLite2-Country.tar.gz --strip-components=1
fi

