#!/bin/bash

# before you start installing opengeodb download:
# CREATE_ZIP_SQL from gist https://gist.github.com/3942008
# as create_zip.sql in the same directory than this script

DB=dbname
DB_USER=dbuser
DB_PASS=xxx
URL=http://fa-technik.adfc.de/code/opengeodb/
ALTER_SQL=opengeodb-end.sql
EXTRA_SQL=extra.sql
CREATE_ZIP_SQL=create_zip.sql
# CREATE_ZIP_SQL from gist https://gist.github.com/3942008

read -n1 -p "This script is installing the opengeodb schema and is importing lot of data. Do you really want to proceed? y|n " key
echo
case $key in
"Y" | "y") true ;;
"N" | "n") echo 'Installation aborted'; exit;;
*) exit; echo ' ' ;;
esac

read -p "Do you want to install a InnoDB or MyISAM driven opengeodb driven schema? [myisam] " engine
case "$engine" in
	innodb) SCHEMA=setup_innodb_geodb.sql
		echo "creating InnoDB opengeodb schema"
		echo " "
	;;
	*) 	SCHEMA=setup_myisam_geodb.sql
		echo "creating MyISAM opengeodb schema" 
		echo " "
	;;
esac

mysql -u $DB_USER -p $DB_PASS $DB < $SCHEMA

read -p "Which country data do you want to import? (comma separated) [AT,BE,CH,DE,LI] " country

if [ "$country" == "" ]; then
	country="AT,BE,CH,DE,LI"
fi

IFS=','
for sql in $country
do
	echo "downloading $sql.sql"
	wget $URL$sql.sql
	echo "importing $sql data"
	mysql -u $DB_USER -p $DB_PASS $DB < $sql.sql
done

echo "Downloading metadata, creating indices"
wget $URL$ALTER_SQL
echo "Inserting metadata, creating indices"
mysql -u $DB_USER -p $DB_PASS $DB < $ALTER_SQL

read -p "Do you want to install extra information (Höhenangaben, Kontinente, Daten mit Versionierung etc.)? y|n " extra
echo
case $extra in
"Y" | "y") 	echo "Downloading $EXTRA_SQL"
		wget $URL$EXTRA_SQL
		echo "Inserting extra informations"
	 	mysql -u $DB_USER -p $DB_PASS $DB < $EXTRA_SQL
;;
*) 	echo "No extra data will be installed"
;;
esac

read -p "Do you want to install hierachy informations? y|n " location
echo
case $location in
"Y" | "y") 	echo "Downloading hierarchy data for selected countries"
	 	for sql in $country
		do
        		echo "Downloading ${sql}hier.sql"
        		wget $URL${sql}hier.sql
        		echo "Importing ${sql}hier data"
        		mysql -u $DB_USER -p $DB_PASS $DB < ${sql}hier.sql
		done
;;
*)	echo "No hierachy data will be installed"
;;
esac

read -p "Do you want to create a single zip table for better radius search? y|n " single
echo
case $single in
"Y" | "y")      echo "Installing zip table"
                mysql -u $DB_USER -p $DB_PASS $DB < $CREATE_ZIP_SQL
;;
*)      echo "No zip table will be installed"
;;
esac

echo
echo "Installing opengeodb finished"
echo