#!/usr/bin/env bash

# Download the databases and create the local SQLite database
# Be aware that this takes significant disc space and can take a day or two to complete

set -ex

export WHOSONFIRST_HOME="${WHOSONFIRST_HOME:-$HOME/src/whosonfirst-sqlite}"
export OPENADDR_HOME="${OPENADDR_HOME:-$HOME/etc/openaddr}"
export DR5HN_HOME="${DR5HN_HOME:-$HOME/src/countries-states-cities-database}"
export OSM_HOME="${OSM_HOME:-$HOME/etc/osm}"

# export http_proxy=http://zack:3128
# export https_proxy=http://zack:3128
export http_proxy=
export https_proxy=
export no_proxy=localhost,127.0.0.0/8,192.168.1.0/16,utilite
export REDIS_SERVER=
export NO_PROXY=

cd $HOME/src/njh/Geo-Coder-Free && git pull
mkdir -p $HOME/Downloads
cd $HOME/Downloads && wget -N -c --no-check-certificate --quiet https://data.openaddresses.io/openaddr-collected-global.zip
cd $HOME/Downloads && wget -N -c --no-check-certificate --quiet https://data.openaddresses.io/openaddr-collected-global-sa.zip

mkdir -p $OPENADDR_HOME
cd $OPENADDR_HOME && rm -rf ?? *.txt summary br mx fr it za es pl be jp kr pt fi de by ch dk ru cz nz tw cl at kz co no uy si xk */*.vrt */*/*.vrt && unzip -o $HOME/Downloads/openaddr-collected-global-sa.zip && unzip -o $HOME/Downloads/openaddr-collected-global.zip

# wof-update etc. are in the NJH-Snippets package on GitHub
if [ ! -d $WHOSONFIRST_HOME ]; then
	mkdir -p $WHOSONFIRST_HOME
	# cd $WHOSONFIRST_HOME && wof-sqlite-download
	cd $WHOSONFIRST_HOME && wof-clone
else
	cd $WHOSONFIRST_HOME && wof-update
fi

if [ -r $OPENADDR_HOME/openaddresses.sql ]; then
	mv $OPENADDR_HOME/openaddresses.sql $OPENADDR_HOME/openaddresses.sql.keep
	chmod 444 $OPENADDR_HOME/openaddresses.sql.keep
fi

if [ -d $DR5HN_HOME ]; then
	cd $DR5HN_HOME
	git pull
else
	mkdir -p $DR5HN_HOME
	cd $DR5HN_HOME/..
	rmdir $DR5HN_HOME
	git clone https://github.com/dr5hn/countries-states-cities-database.git
fi

mkdir -p $OSM_HOME
# cd $HOME/Downloads && wget -N -c https://ftpmirror.your.org/pub/openstreetmap/planet/planet-latest.osm.bz2
cd $OSM_HOME && wget -N -c https://download.geofabrik.de/europe-latest.osm.bz2
wget -N -c https://download.geofabrik.de/north-america-latest.osm.bz2
wget -N -c https://download.geofabrik.de/australia-oceania-latest.osm.bz2

# If you're paranoid, enable this line to verify the download
# bunzip2 -t *.osm.bz2

# cd $HOME/src/njh/Geo-Coder-Free && git pull && perl Makefile.PL && make && make test AUTHOR_TESTING=1 TEST_FILES="t/scantext.t t/whosonfirst.t t/openaddr.t t/maxmind.t t/local.t"
cd $HOME/src/njh/Geo-Coder-Free && git pull && perl Makefile.PL && make && make test TEST_FILES="t/scantext.t t/whosonfirst.t t/openaddr.t t/maxmind.t t/local.t"
