#!/bin/bash -

#
# This script reads and writes entries from a Chado database.
# Examples:
# writedb_entry -help
# writedb_entry -s Pf3D7_01 Pf3D7_05 Pf3D7_07
#
# If the environment variable ARTEMIS_JAVA_JRE points to a Java JRE
# then this JRE will be used in preference to any other.
#

QUIET=no
PROXY_SETTINGS=
HELP_FLAG=false

add_proxy_properties() {

	if [[ "$http_proxy" = "" ]]
	then
		http_proxy=$HTTP_PROXY
	fi
	
	if [[ "$http_proxy" = "" ]]
	then
	 	http_proxy=$HTTP_proxy
	fi
	
	if [[ "$http_proxy" != "" ]]
	then
		PROXY_SETTINGS="${PROXY_SETTINGS} -DproxySet=true "`echo $http_proxy | sed 's/http:\/\/\(.*\):\(.*\)/ -Dhttp.proxyHost=\1 -Dhttp.proxyPort=\2/'`
	fi
}


#
# Resolve script path (inc symlinks)
#
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
PRG="$( cd -P "$( dirname "$SOURCE" )" && pwd )"; # get final path of this script

#
# Allow URLs to work from behind firewalls.
#
add_proxy_properties

FLAGS="-mx2048m -ms20m -Djdbc.drivers=org.postgresql.Driver -Dibatis"
DEFAULT_CONNECTION=
APPLICATION_HOME="$PRG/.."
JAR_NAME=artemis.jar
JAR_FILE_DEFAULT=$APPLICATION_HOME/target/jars/$JAR_NAME
JAR_FILE_INSTALLED=$APPLICATION_HOME/dist/$JAR_NAME
WRITE_DBENTRY_CP=$APPLICATION_HOME:$JAR_FILE_DEFAULT:$JAR_FILE_INSTALLED

#
# Use a custom Java version if necessary
#
if [[ "$ARTEMIS_JAVA_JRE" = "" ]]
then
	JAVA=`which java`
else
	JAVA_HOME=$ARTEMIS_JAVA_JRE
	JAVA=$ARTEMIS_JAVA_JRE/bin/java
fi

JAVA_VERSION=`${JAVA} -version 2>&1`
echo ${JAVA_VERSION}

#
# Parse arguments.
#
while test $# != 0
    do
    case $1 in
    -Dchado*)
       DEFAULT_CONNECTION="$1" ;;
    -D*)
       FLAGS="$FLAGS $1" ;;
    -help)
       HELP_FLAG=true ;;
    -h)
       HELP_FLAG=true ;;
    *) break ;;
    esac
    shift
done

PLATTMP=`uname`
if [[ "$PLATTMP" = "Darwin" ]]
then
	# For running within an App folder
  	WRITE_DBENTRY_CP="$APPLICATION_HOME/Java/$JAR_NAME:$WRITE_DBENTRY_CP"
fi

# searches for the -c option (needs to know the index)
idx=0
for arg in "$@"
do
	if [ '-c' == "${arg}" ]; then
		let "nextID = $idx + 1";
		DEFAULT_CONNECTION="-Dchado=${!nextID}"
	fi
	if [ '-l' == "${arg}" ]; then
		let "nextID = $idx + 2";
        eval MAPPING_PATH="${!nextID}"
		WRITE_DBENTRY_CP="${MAPPING_PATH}:$WRITE_DBENTRY_CP"
	fi
	let idx++
done

# Check that a database connection has been provided
if [[ "$DEFAULT_CONNECTION" = "" ]] && [[ "$HELP_FLAG" != "true" ]]
then 
	echo "ERROR: Please supply a database connection argument"
	exit 1
fi

if [[ "$QUIET" = "no" ]]
then
	echo "Starting writedb_entry with arguments: $PROXY_SETTINGS $DEFAULT_CONNECTION $FLAGS $@"
	echo "Using classpath: $WRITE_DBENTRY_CP"
fi

$JAVA $PROXY_SETTINGS $DEFAULT_CONNECTION $FLAGS -cp "$WRITE_DBENTRY_CP" uk.ac.sanger.artemis.io.ReadAndWriteEntry "$@"
result=$?

exit $result
