#!/bin/bash
#############################
# This is used to configure the behaviout of the various bootstrap scripts.
#############################

IMPORT_DATA_OSM=false
IMPORT_DATA_OSM_CACHED=false
IMPORT_DATA_SQL=true
IMPORT_SCHEDULES=true

INSTALL_GEOSERVER=false
INSTALL_HTTPD=true
# install isochrone from vagrant start directory (eclipse project)
INSTALL_ISOCHRONE_LOCAL=false
# install latest isochrone release from nexus repository
INSTALL_ISOCHRONE_RELEASE=true
# install latest isochrone snapshot from nexus repository
INSTALL_ISOCHRONE_SNAPSHOT=true
# install latest isochrone-datamodel from nexus repository
INSTALL_ISOCHRONE_DATAMODEL=true
# install latest isochrone-tools from nexus repository
INSTALL_ISOCHRONE_TOOLS=true
INSTALL_NEO4J=false
INSTALL_NEO4J_SPATIAL=false
INSTALL_PGROUTING=false
INSTALL_POSTGIS=true
INSTALL_POSTGRES=true
INSTALL_POSTGRES_ADMIN=true
INSTALL_PROJECT_PSIPROBE=true
INSTALL_SPATIALITE=false
INSTALL_SPATIALITE_ADMIN=true

CACHE_DIR_LOCAL="/tmp/vagrant-cache"
CACHE_DIR_REMOTE="http://dbis-informatik.uibk.ac.at/static/ma/niko"
CACHE_DIR_REMOTE_ISO="${CACHE_DIR_REMOTE}/isochrone"
CACHE_DIR_REMOTE_DATA="${CACHE_DIR_REMOTE}/isochrone_dump"
DEPLOY_DIR="/opt"
DATA_DIR="$DEPLOY_DIR/data"
SETUP_DIR="/setup"
WORKING_DIR="/var/log/vagrant_bootstrap"

# Parameters that can also (some must) be passed as arguments
IS_LOCAL_TEST_DEPLOY=false
PG_DB_NAME="isochrone"
PG_DB_USER="@db_username@"
PG_DB_PASSWORD="@db_password@"
TOMCAT_PASSWORD="@tomcat_password@"

############################
# Calculated variables
############################

# Do not change anything below this line!

EXEC_POSTGRES="psql"
POSTGRES_SHARE="/usr/share/pgsql"
if [ ! -d ${POSTGRES_SHARE} ]; then
	POSTGRES_SHARE="$(dirname /usr/pgsql*/.)""/share"
fi

AVAILABLE_GEOSERVER=$([ -d "${DEPLOY_DIR}/geoserver" ] && echo true || echo false)
AVAILABLE_ISOCHRONE_DATAMODEL=$([ -f "${DEPLOY_DIR}/isochrone-datamodel.jar" ] && echo true || echo false)
AVAILABLE_ISOCHRONE_TOOLS=$([ -f "${DEPLOY_DIR}/isochrone-tools.jar" ] && echo true || echo false)
AVAILABLE_NEO4J=$([ -d "${DEPLOY_DIR}/neo4j" ] && echo true || echo false)
AVAILABLE_NEO4J_SPATIAL=$([ -d "${DEPLOY_DIR}/neo4j-spatial" ] && echo true || echo false)
AVAILABLE_POSTGRES=$(type ${EXEC_POSTGRES} >/dev/null 2>&1 && echo true || echo false)
AVAILABLE_POSTGIS=$([ -f "${POSTGRES_SHARE}/extension/postgis.control" ] && echo true || echo false)
AVAILABLE_PGROUTING=$([ -f "${POSTGRES_SHARE}/extension/pgrouting.control" ] && echo true || echo false)
AVAILABLE_SPATIALITE_ADMIN=$([ -f "${DEPLOY_DIR}/phpliteadmin/phpliteadmin.config.php" ] && echo true || echo false)

DOWNLOAD_DIR="${CACHE_DIR_LOCAL}/wget"
if [ ! -f "${SETUP_DIR}" ]; then
	SCRIPT="$(readlink -f ${BASH_SOURCE[0]})"
	SCRIPT_DIR="$(dirname ${SCRIPT})"
	SETUP_DIR="$(dirname ${SCRIPT_DIR})"
fi
SHARED_CONF_DIR="${SETUP_DIR}/conf"
SHARED_IMG_DIR="${SETUP_DIR}/img"

# Function definitions

fn_arg2boolean() {
	local OUTVAR="${1}"
	local ARG="${2}"
	local VALUE="${3}" # Default value
	 
	if [ -n "$ARG" ]; then
		if [ "$ARG" == "true" ]; then
			VALUE=true
		else
			VALUE=false
		fi
	fi

	eval $OUTVAR="\$VALUE"
}

fn_arg2string() {
	local OUTVAR="${1}"
	local ARG="${2}"
	local VALUE="${3}" # Default value

	if [ "${VALUE:0:1}" == "@" ]; then
			VALUE="${ARG}"
	fi
	if [ "${VALUE:0:1}" == "@" ]; then
		echo '- No (or invalid) database user supplied!'
		exit 1
	fi

	eval $OUTVAR="\$VALUE"
}

fn_download() {
	local FILE="$1"
	local URL="$2"

	if wget --no-check-certificate -q -O /dev/null "$URL"; then
		wget --no-check-certificate -q -O "$FILE" "$URL"
	fi
}

fn_download_newer() {
	# "wget -N" does not work with "-O" option... so we use a workaround here
	local FILE="$1"
	local URL="$2"

	if [ ! -f "$FILE" ]; then
		if wget --no-check-certificate -q -O /dev/null "$URL"; then
			wget --no-check-certificate -q -O "$FILE" "$URL"
		fi
	fi
}
