#!/bin/bash
#
# Startup script for the UNICORE uvos command line client
#

if [ ! -e ${HOME}/.uvos-clc/ ]; then
    mkdir ${HOME}/.uvos-clc/
fi


for f in /etc/unicore/uvos-clc/*; do
    if [ ! -e ${HOME}/.uvos-clc/`basename $f` ]; then
	cp $f ${HOME}/.uvos-clc/`basename $f`
    fi
done


if [ -z "$UVOSCLC_CONFIG" ]
then
	UVOSCLC_CONFIG="${HOME}/.uvos-clc/uvosClient.conf"
fi

export UVOSCLC_CONFIG

#
# Java command 
#
JAVA=java


# copy options from the OPTS environment variable
OPTS=$UVOSCLC_OPTS


#where the uvos-clc jar files are
UVOS_CLC_LIB=/usr/share/unicore/uvos-clc/lib


#
# 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 to the Java VM
#

#
#Memory for the VM
#
set_option "Xmx" "128m"

set_option "Djava.endorsed.dirs" "=$UVOS_CLC_LIB/endorsed"

#
# log configuration
#
set_option "Dlog4j.configuration" "=file://${HOME}/.uvos-clc/log4j.properties"





CP=$(cd /; find "$UVOS_CLC_LIB" -name '*.jar' -exec printf ":{}" \;)

if [ -d $HOME/.uvos-clc/lib ]
	then
	  CP="$CP"$(find "$HOME/.uvos-clc/lib" -name '*.jar' -exec printf ":{}" \;)
	fi

CP=."$CP"




#
#go
#
export CLASSPATH=$CP

$JAVA "${Options[@]}" ${UVOSCLC_OPTS} pl.edu.icm.unicore.uvos.clc.CLClient ${1+"$@"} 




