#!/bin/sh
TAUROOT=/home/suravee/tau2-ktau
CONFIG_ARCH=i386_linux

PERFDMF_HOME=${TAUROOT}/tools/src/dms
PERFDMF_JAR=${TAUROOT}/${CONFIG_ARCH}/lib/dms.jar
XMLSAX_JAR=${TAUROOT}/${CONFIG_ARCH}/lib/xerces.jar
JARGS_JAR=${TAUROOT}/${CONFIG_ARCH}/lib/jargs.jar
CONFIG_FILE=${HOME}/.ParaProf/perfdmf.cfg

if [ "x$TAUROOT" = "x" ] 
    then
    echo "TAUROOT not set.  Please run 'configure' in the tau root directory."
    exit
fi

if [ ! -r ${PERFDMF_JAR} ]
    then
    echo ""
    echo "$PERFDMF_JAR not found."
    echo "Please run 'make' in the directory ${PERFDMF_HOME}."
    echo ""
    exit 
fi

if [ ! -r ${XMLSAX_JAR} ]
    then
    echo ""
    echo "$XMLSAX_JAR not found."
    echo ""
    exit
fi

if [ ! -r ${JARGS_JAR} ]
    then
    echo ""
    echo "$JARGS_JAR not found."
    echo ""
    exit
fi

if [ ! -r ${HOME}/.ParaProf ]
    then
    mkdir ${HOME}/.ParaProf
fi

java -cp ${PERFDMF_JAR}:${XMLSAX_JAR}:${JARGS_JAR} edu.uoregon.tau.dms.loader.Configure -t ${TAUROOT} -a ${CONFIG_ARCH} -g ${CONFIG_FILE}


JDBC_JAR=`grep jdbc_db_jarfile ${CONFIG_FILE} | sed s/jdbc_db_jarfile://`

if [ ! -r ${JDBC_JAR} ]; then
    echo ""
    echo "$JDBC_JAR not found."
    echo ""
    
    
    JDBC_ROOT=`basename ${JDBC_JAR}`
    
    if [ x${JDBC_ROOT} == "xojdbc14.jar" ]; then
	echo ""
	echo "Sorry, can't automatically download drivers for Oracle"
	echo "Please acquire them manually"
	exit
    else

	echo "JDBC drivers are not distributed with TAU.  You should acquire the JDBC driver"
	echo "that corresponds to the database you are connecting to.  TAU can now attempt "
	echo "to download a JDBC driver that will *probably* work."  
	echo ""
	
	
	    # -- set up portable echo command
	case "`echo 'x\c'`" in
	    'x\c')  echo="echo -n"  nnl= ;;     #BSD
	    x)      echo="echo"     nnl="\c";;  #SysV
	    *)      echo 'Cannot setup echo. What weird machine do you have?' 1>2&
		exit 1;;
	esac
	
	while [ "x$RESPONSE" = "x" ]; do
	    $echo "Would you like to attempt to automatically download a JDBC driver? (y/n) ${nnl}"
	    read RESPONSE
	    case $RESPONSE in
		[yY]|[Yy][Ee][Ss]) RESPONSE=y ;;
		[nN]|[Nn][Oo]) RESPONSE=n ;;
	    esac
	    
	done
	
	
	if [ "${RESPONSE}" == "y" ]; then 
	    
	    WGET=`which wget 2>/dev/null`
	    
	    if [ ! -r "$WGET" ]; then
		echo ""
		echo "Couldn't find wget, you'll have to manually download your JDBC driver"
		exit 1
	    fi
	    
	    if [ x${JDBC_ROOT} == "xmysql.jar" ]; then
		wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-3.0.14-production.tar.gz/from/http://mysql.247available.com/
		gunzip mysql-connector-java-3.0.14-production.tar.gz
		tar -Oxf mysql-connector-java-3.0.14-production.tar mysql-connector-java-3.0.14-production/mysql-connector-java-3.0.14-production-bin.jar > ${JDBC_JAR}
		rm mysql-connector-java-3.0.14-production.tar
	    fi
	    
	    if [ x${JDBC_ROOT} == "xpostgresql.jar" ]; then
		wget http://jdbc.postgresql.org/download/pg74.214.jdbc2.jar
		mv pg74.214.jdbc2.jar ${JDBC_JAR}
	    fi
	    
	    
	    if [ ! -r ${JDBC_JAR} ]; then
		echo ""
		echo "Still couldn't find $JDBC_JAR, please acquire your JDBC driver manually"
		echo ""
		
		exit
	    fi
	else
	    exit
	fi
    fi
fi

java -cp ${PERFDMF_JAR}:${XMLSAX_JAR}:${JARGS_JAR}:${JDBC_JAR} edu.uoregon.tau.dms.loader.ConfigureTest -t ${TAUROOT} -a ${CONFIG_ARCH} -g ${CONFIG_FILE}
