#!/bin/bash
#
#Start script for the XUUDB admin client
#

#
#Java command 
#
JAVA=java

LOCAL_DIR="${HOME}"/.xuudb-adminclient

if [ ! -d "$LOCAL_DIR" ]; then
    mkdir "$LOCAL_DIR"
fi


for f in /etc/unicore/xuudb/*client*; do
    if [ ! -e "$LOCAL_DIR"/`basename $f` ]; then
		cp $f "$LOCAL_DIR"
    fi
done

#
# helper function to set an option if it is not already set
#
# arg1: option name (without leading "-", e.g "Ducc.extensions")
# arg2: option value (e.g. =conf/extensions)
#
Options=( )
	set_option(){
	    if [[ "$UVOSCLC_OPTS" != *$1* ]]
	        then
	                N=${#Options[*]}
	                Options[$N]="-$1$2"
	        fi
	}

#
#Options
#

set_option "Dxuudb.batch" "=true"
set_option "Dxuudb.client.conf" "=${LOCAL_DIR}/xuudb_client.conf"

#
# log configuration
#
set_option "Djava.util.logging.config.file" "=${LOCAL_DIR}/client_logging.properties"
set_option "Dlog4j.configuration" "=file:///${LOCAL_DIR}/client_logging.properties"

#
#Memory for the VM
#
MEM=-Xmx128m

#
#put all jars in lib/ on the classpath
#
JARS=/usr/share/unicore/xuudb/lib/*.jar
CP=.
for JAR in $JARS ; do 
    CP=$CP:$JAR
done


# run
$JAVA "${Options[@]}" ${XUUDB_ADMIN_OPTS} -cp ${CP} de.fzj.unicore.xuudb.client.XUUDBClient "$@"
