#!/bin/bash
#
# Startup script for the UNICORE Commandline client
#

if [ ! -e ${HOME}/.ucc/ ]; then
    mkdir -m 700 ${HOME}/.ucc/
fi


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


#
# where the UCC jar files are
#
UCC_LIB=/usr/share/unicore/ucc/lib

#
# Java command 
#
JAVA=java

#
# 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 [[ "$UCC_OPTS" != *$1* ]]
        then
		N=${#Options[*]}
		Options[$N]="-$1$2"
	fi
}


#
# Options to the Java VM
#

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

#set this one if you have ssl problems and need debug info
#OPTS=$OPTS" -Djavax.net.debug=ssl,handshake"

#
# default location of user preferences file
#
set_option "Ducc.preferences" "=${HOME}/.ucc/preferences"

#
# extensions file
#
set_option "Ducc.extensions" "=${HOME}/.ucc/extensions"

#
# log configuration
#
set_option "Dlog4j.configuration" "=file://${HOME}/.ucc/logging.properties"



#
# put all jars in lib/ on the classpath
#
CP=$(find "$UCC_LIB" -name "*.jar" -exec printf ":{}" \; )

#
# put all jars in ~/.ucc/lib/ on the classpath
#
if [ -d $HOME/.ucc/lib ]
then
  CP=$CP$(find "$HOME/.ucc/lib" -name "*.jar" -exec printf ":{}" \; )
fi


CP=."$CP"


#
# setup endorsed directory
#
DEFS="-Djava.endorsed.dirs=${UCC_LIB}/endorsed"


#
# go
#
$JAVA "${Options[@]}" ${DEFS} ${UCC_OPTS} -cp ${CP} de.fzj.unicore.ucc.UCC ${1+"$@"}
