#! /bin/sh

NAME=disksrm
GRIDFTP_HOST=fnisd1.fnal.gov
GRIDFTP_PORT=2811
CHOWN=./chown.sh
STAT=./stat.sh
LOG=/tmp/$NAME.log
#DEBUG=true
#SECURITY_DEBUG=true 
#DEBUG=false
#java -cp ../classes/dcache.jar:../classes/pgjdbc2.jar: org.dcache.srm.unixfs/Main /tmp/config1.xml disksrm
if [ ! -f ../classes/cells.jar ]
then
  echo cannot find ../../classes/srm.jar
  exit 1
fi

if [ ! -f ../classes/srm.jar ]
then
  echo cannot find ../../classes/srm.jar
  exit 1
fi

if [ ! -f ../classes/glue/GLUE-STD.jar ]
then
  echo cannot find ../../classes/glue/GLUE-STD.jar
  exit 1
fi


SRM_CP=../classes/srm.jar
SRM_CP=$SRM_CP:../classes/cells.jar
SRM_CP=$SRM_CP:../classes/derby.jar

SRM_CP=$SRM_CP:../classes/concurrent.jar

# glue
SRM_CP=$SRM_CP:../classes/glue/GLUE-STD.jar
SRM_CP=$SRM_CP:../classes/glue/dom.jar
SRM_CP=$SRM_CP:../classes/glue/servlet.jar
SRM_CP=$SRM_CP:../classes/glue/jnet.jar
SRM_CP=$SRM_CP:../classes/glue/jsse.jar
SRM_CP=$SRM_CP:../classes/glue/jcert.jar


# old globus cog jars
#SRM_CP=$SRM_CP:$SRM_PATH/lib/globus/cog.jar:$SRM_PATH/lib/globus/iaik_javax_crypto.jar
#SRM_CP=$SRM_CP:$SRM_PATH/lib/globus/iaik_jce_full.jar:$SRM_PATH/lib/globus/iaik_ssl.jar
#SRM_CP=$SRM_CP:$SRM_PATH/lib/globus/log4j-core.jar

# globus cog
SRM_CP=$SRM_CP:../classes/globus/cryptix.jar
SRM_CP=$SRM_CP:../classes/globus/ce-jdk13-120.jar
SRM_CP=$SRM_CP:../classes/globus/log4j-1.2.8.jar
SRM_CP=$SRM_CP:../classes/globus/cog-jglobus.jar
SRM_CP=$SRM_CP:../classes/globus/cryptix-asn1.jar
SRM_CP=$SRM_CP:../classes/globus/cryptix32.jar
SRM_CP=$SRM_CP:../classes/globus/junit.jar
SRM_CP=$SRM_CP:../classes/globus/puretls.jar


# apache xerces xml parcer jars

SRM_CP=$SRM_CP:../classes/xml/xmlParserAPIs.jar
SRM_CP=$SRM_CP:../classes/xml/xercesImpl.jar

OPTIONS="-Djava.protocol.handler.pkgs=org.globus.net.protocol"
if [ "$DEBUG" = "true" ]
then
  OPTIONS=" ${OPTIONS} -Delectric.logging=SOAP,HTTP"
fi

if [ "$SECURITY_DEBUG" = "true" ]
then
  OPTIONS=" ${OPTIONS} -Dlog4j.configuration=${SRM_PATH}/bin/log4j.properties.all"
fi

#OPTIONS=" ${OPTIONS} -Dorg.globus.tcp.port.range=50000,50050"
if [ ! -d .srmconfig ]
then
	mkdir .srmconfig
fi
SRM_CONFIG=.srmconfig/config-derby.xml
#cd $SRM_PATH/bin

if [ ! -f $SRM_CONFIG ]
then
	echo configuration file not found, configuring srmcp >&2
	
	cmd="java -cp $SRM_CP $OPTIONS \
          org.dcache.srm.unixfs.Main $SRM_CONFIG $NAME $GRIDFTP_HOST $GRIDFTP_PORT $STAT $CHOWN"

	if [ "$DEBUG" = "true" ]
	then
		echo $cmd
	fi
	$cmd
	if tty -s
	then
		echo would you like to view/edit configuration file?[y/n] >&2
		read -t 60 ANSWER
		if [ "$ANSWER" = "y" ]
		then
			${VISUAL:-${EDITOR:-vi}} $SRM_CONFIG
		fi
	fi
fi
	
cmd="java -cp $SRM_CP $OPTIONS org.dcache.srm.unixfs.Main $SRM_CONFIG $NAME $GRIDFTP_HOST $GRIDFTP_PORT $STAT $CHOWN $LOG"
#cmd="java -cp $SRM_CP $OPTIONS org.dcache.srm.unixfs.Main $SRM_CONFIG $NAME $GRIDFTP_HOST $GRIDFTP_PORT $STAT $CHOWN"
	echo $cmd
if [ "$DEBUG" = "true" ]
then
	echo $cmd
fi
$cmd
