#!/bin/bash

#
# Sparqlify profile tool
# ./sparqlify-tool
#
#

usage="$(basename "$0") options -- Tool to simply invoking sparqlify based on profiles

where:
    -P  profile name
    -h  database host name
    -d  database name
    -p  database port
    -U  database user name
    -W  database password
    -Q  SPARQL query string

If the query string is the last argument, -Q can be omitted.
"


#
# Hard coded profile paths, with folder precedence order: local > home > etc
#
configFiles[0]="/etc/sparqlify/sparqlify.conf"
configFiles[1]="./sparqlify.conf.dist"
configFiles[2]="./sparqlify.conf"

for configFile in "${configFiles[@]}"; do
        [ -f "$configFile" ] && source "$configFile"

#       echo "Checking $configFile --- $sparqlifyCmd"
done


profileName="default"


# By default, the last argument becomes the query string
#     Source: http://stackoverflow.com/questions/1853946/getting-the-last-argument-passed-to-a-shell-script
for queryString; do true; done


# Simple function to echo to stderr
echoerr() { echo "$@" 1>&2; }

# Support for simple command line args
# Source: http://mywiki.wooledge.org/BashFAQ/035#getopts
# A POSIX variable
OPTIND=1         # Reset in case getopts has been used previously in the shell.

while getopts "?h:p:j:P:m:U:W:d:Q:" opt; do
    case "$opt" in
        \?)
            echoerr "$usage"
            exit 0
            ;;
        h)  dbHost="$OPTARG"
            ;;
        U)  dbUser="$OPTARG"
            ;;
        W)  dbPass="$OPTARG"
            ;;
        d)  dbName="$OPTARG"
            ;;
        p)  dbPort="$OPTARG"
            ;;
        j)  dbJdbc="$OPTARG"
            ;;
	m)  mappingFile="$OPTARG"
            ;;
        Q)  queryString="$OPTARG"
            ;;
        P)  profileName="$OPTARG"
            ;;
    esac
done

shift $((OPTIND-1))

[ "$1" = "--" ] && shift


for profilePath in "${profilePaths[@]}"; do
	profileFile="$profilePath/$profileName.conf"
	profileFileDist="$profileFile.dist"

	echoerr "Searching for profile: $profileFile"
        [ -f "$profileFileDist" ] && source "$profileFileDist"
	[ -f "$profileFile" ] && source "$profileFile"
done



# TODO This does not work as expected - need to find a more clever way, possible solution: http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
#if [ ! -f "$sparqlifyFile" ]; then
#	echoerr "Sparqlify executable not found"
#	exit 1
#fi


# Check if the query argument actually matches the name of a query template

if [ -n "$queryString" ]; then
    [ ${namedQueries[$queryString]+isset} ] && queryString="${namedQueries[$queryString]}"
fi


# Confirm settings before continuation
dbPassText=`echo "$dbPass" | cut -c1-2`

[ -z "$dbPort" ] && dbPort="5432"

echoerr "-------------------------------------------------------------------"
echoerr "Your settings are:"
echoerr "Database"
if [ -z "$dbJdbc" ]; then
    echoerr "  Name: $dbName"
    echoerr "  Host: $dbHost"
    echoerr "  Port: $dbPort"
else
    echoerr "  Jdbc: $dbJdbc"
fi
echoerr "  Username: $dbUser"
echoerr "  Password: $dbPassText..."
echoerr ""
echoerr "Paths:"
echoerr "  Sparqlify command: $sparqlifyCmd"
echoerr "  View definition: $mappingFile"
echoerr ""
echoerr "Query:"
echoerr "  $queryString"
echoerr "-------------------------------------------------------------------"
#read -p "Press [Enter] key to start loading"

if [ -z "$queryString" ]; then
    echoerr "Error: No query string specified"
    exit 1
fi

# Note: ${arr[@]/#/-m } will turn an array of mapping file names m1, ... m2 into multiple command line options -m m1 ... -m m2
if [ -z "$dbJdbc" ]; then
    "$sparqlifyCmd" -h "$dbHost" -p "$dbPort" -d "$dbName" -U "$dbUser" -W "$dbPass" ${arr[@]/#/-m } -Q "$queryString"
else
    "$sparqlifyCmd" -j "$dbJdbc" -U "$dbUser" -W "$dbPass" ${arr[@]/#/-m } -Q "$queryString"
fi


