#!/bin/sh

. /etc/conf.d/libmaxminddb

set -e

[ -z "$MAXMINDDB_FILES"  ] && MAXMINDDB_FILES="GeoLite2-City.mmdb"
[ -z "$MAXMINDDB_URL"    ] && MAXMINDDB_URL="http://geolite.maxmind.com/download/geoip/database"
[ -z "$MAXMINDDB_LIBDIR" ] && MAXMINDDB_LIBDIR="/var/lib/libmaxminddb"

clean_up() {
	[ -n "$TMPDIR" ] && rm -rf "$TMPDIR"
}
trap clean_up EXIT SIGTERM SIGINT SIGQUIT

TMPDIR="$(mktemp -d)"
for filename in $MAXMINDDB_FILES; do
	curl --silent "$MAXMINDDB_URL/$filename.gz" -o "$TMPDIR/$filename.gz"
	gunzip "$TMPDIR/$filename.gz"
	mv "$TMPDIR/$filename" "$MAXMINDDB_LIBDIR"
done
exit 0
