#!/bin/sh

OLDDIR=$(pwd)
TMPDIR=$(mktemp -d)
DATE=$(date +'%Y%m%d')
YEARMONTH=$(date +'%Y-%m')

cd ${TMPDIR}

wget https://download.db-ip.com/free/dbip-country-lite-${YEARMONTH}.csv.gz
gunzip dbip-country-lite-${YEARMONTH}.csv.gz

mkdir ${TMPDIR}/geoip-database-${DATE}

mv dbip-country-lite-${YEARMONTH}.csv ${TMPDIR}/geoip-database-${DATE}/dbip-country-lite.csv
tar -cJf geoip-database_${DATE}.orig.tar.xz geoip-database-${DATE}

mv geoip-database_${DATE}.orig.tar.xz ${OLDDIR}/../geoip-database_${DATE}.orig.tar.xz
cd ${OLDDIR}
rm -rf ${TMPDIR}

echo "Saved tarball: ${OLDDIR}/../geoip-database_${DATE}.orig.tar.xz"

exit 0
