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

# Prevent double inclusion (so vars can be modifed after first include)
if [ -n "${WORKING_DIR}" ]; then
	return
fi

IMPORT_DATA_GEOSERVER=false
IMPORT_DATA_NEO4J_SPATIAL=false
IMPORT_DATA_POSTGIS=true
IMPORT_DATA_SPATIALITE=true

# Comma separated list of srids for which datasets will be generated
# This is useful when creating multiple datasets (when creating/updating data from scratch)
# Note that only the last SRID in the list will be kept in the database, all the others will be overwritten
#TARGET_SRID="4326,3857"
TARGET_SRID="3857"
UPDATE_DATA=false

CACHE_DIR_LOCAL="/tmp/vagrant-cache"
CACHE_DIR_REMOTE="https://dbis-owncloud.uibk.ac.at"
CACHE_DIR_REMOTE_ISO="${CACHE_DIR_REMOTE}/index.php/s/C6OdFprXW71ugNW/download?path=%2F&files="
CACHE_DIR_REMOTE_DATA="${CACHE_DIR_REMOTE}/index.php/s/kgjm3CItQJ6P374/download?path=%2F2016&files="
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
DEPLOY_ALL_DATASETS=false
IS_JENKINS_DEPLOY=false
IS_LOCAL_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="$(dirname /usr/pgsql*/.)""/share"
if [ ! -d ${POSTGRES_SHARE} ]; then
	POSTGRES_SHARE="/usr/share/pgsql"
fi

AVAILABLE_GDAL=$([ -d "${DEPLOY_DIR}/gdal" ] && echo true || echo false)
AVAILABLE_GEOSERVER=$([ -d "${DEPLOY_DIR}/geoserver" ] && echo true || echo false)
AVAILABLE_ISOCHRONE_DATAMODEL=$([ -d "${DEPLOY_DIR}/isochrone-datamodel" ] && 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_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

function 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"
}

function 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"
}

function 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
}

function 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
}

function fn_service_start() {
	local SERVICE_NAME="$1"

	echo "Starting service \"${SERVICE_NAME}\""
	touch "service_start_$SERVICE_NAME.log" 2>&1
	if [ "$DISTRI_TYPE" == "CentOS 6" ]; then
		service $SERVICE_NAME start >> "service_start_$SERVICE_NAME.log" 2>&1
		chkconfig $SERVICE_NAME on "service_start_$SERVICE_NAME.log" 2>&1
	else
		systemctl start $SERVICE_NAME >> "service_start_$SERVICE_NAME.log" 2>&1
		systemctl enable $SERVICE_NAME >> "service_start_$SERVICE_NAME.log" 2>&1
	fi

	sleep 2
}

function fn_service_stop() {
	local SERVICE_NAME="$1"

	touch "service_stop_$SERVICE_NAME.log" 2>&1
	if [ "$DISTRI_TYPE" == "CentOS 6" ]; then
		service $SERVICE_NAME stop >> "service_stop_$SERVICE_NAME.log" 2>&1
		chkconfig $SERVICE_NAME off "service_stop_$SERVICE_NAME.log" 2>&1
	else
		systemctl stop $SERVICE_NAME >> "service_stop_$SERVICE_NAME.log" 2>&1
		systemctl disable $SERVICE_NAME >> "service_stop_$SERVICE_NAME.log" 2>&1
	fi

	sleep 2
}

