#!/bin/sh

# $Id: perfexplorer_configure.skel,v 1.24 2010/06/08 21:51:57 wspear Exp $
# $Name:  $

rm -f .last_config
echo $* > .last_config
echo $* >> .all_configs

# Record all the arguments
arguments=$*

# Default compilers and options
tauroot=/home/users/sameer/fresh/f/head/release/tau2
architecture=default
taushell=sh
targetdir=/home/users/sameer/fresh/f/head/release/tau2
serverhostname=localhost
objectport=9999
registryport=1099
#configfile=$HOME/.ParaProf/perfdmf.cfg
configdir=$HOME/.ParaProf
tmpdir=/tmp
LIBDIR=${tauroot}/${architecture}/lib
PERFEXPLORER_JAR=${LIBDIR}/perfexplorer.jar
redirectfile=weka-redirect-3-6-1.html
redirect=http://www.cs.uoregon.edu/research/paracomp/tau/${redirectfile}

# Test for java 1.4+
JAVA_VERSION=`java -version 2>&1 | head -1 | cut -d '.' -f2`
if [  "x$JAVA_VERSION" = "x4" ] ; then
        echo ""
        echo "Java 1.5 or newer is required to run PerfExplorer."
        echo "Please update your Java SDK to a newer version to use PerfExplorer 2.0."
        echo "You will still be able to use PerfExplorer 1.0, from the TAU v2.17 release."
        echo ""
        if [ ! -r ${tauroot}/${architecture}/bin/bin-1.4/perfexplorer_configure ] ; then
                ${tauroot}/${architecture}/bin/configure-1.4
        fi
        ${tauroot}/${architecture}/bin/bin-1.4/perfexplorer_configure


        exit 0
fi

# PARSE COMMAND LINE SWITCHES
###############################################################
for arg in "$@";
do
  case $arg in

  -objectport=*)
    myarg=`echo $arg | sed 's/-objectport=//'`
    objectport=$myarg
    shift
    ;;

  -registryport=*)
    myarg=`echo $arg | sed 's/-registryport=//'`
    registryport=$myarg
    shift
    ;;

  -server=*)
    myarg=`echo $arg | sed 's/-server=//'`
    serverhostname=$myarg
    shift
    ;;

  -configfile=*)
    myarg=`echo $arg | sed 's/-configfile=//'`
    configfile=$myarg
    shift
    ;;

  -tmpdir=*)
    myarg=`echo $arg | sed 's/-tmpdir=//'`
    tmpdir=$myarg
    shift
    ;;

  -help)
    echo "PerfExplorer Configuration Utility "
    echo "***********************************************************************"
    echo "Usage: configure [OPTIONS]"
    echo "  where [OPTIONS] are:"
    #echo "-server=<hostname> ...... specify hostname for the perfexplorer server."
    #echo "-registryport=<number> ............. specify port for the rmi registry."
    #echo "-objectport=<number> ................ specify port for the rmi objects."
    echo "-configfile=<path to file> ............... specify PerfDMF config file."
    echo "-tmpdir=<dir> ....................... specify temporary file directory."
    exit
    ;;

  '')
    #echo "NULL switch!"
    # Required for HP/Compaq Tru64 machines.
    ;;

  *)
    echo "ERROR: Command line switch \`$arg' not recognized" 1>&2
    exit 1
    ;;
  esac
done

# -- 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

# check to see if PerfDMF has been executed
    
 $echo "What is the name of your PerfDMF Configuration: ${nnl}"
 read RESPONSE
  if [ ! "$RESPONSE" ]; then
    configfile=${configdir}/perfdmf.cfg
  else
    configfile=${configdir}/perfdmf.cfg."${RESPONSE}"
  fi
  if [ ! -r ${configfile} ]; then
    echo ""
    echo "PerfDMF Configfile named ${RESPONSE} not found."
    echo "Please run perfdmf_configure, if you haven't already done so."
    echo ""
    exit
  fi
RESPONSE=

JDBC_JAR=`grep jdbc_db_jarfile ${configfile} | sed s/jdbc_db_jarfile://`
JDBC_TYPE=`grep jdbc_db_type ${configfile} | sed s/jdbc_db_type://`

if [ ! "${JDBC_TYPE}" = "db2" ]; then
	if [ ! -r ${JDBC_JAR} ]
    	then
    	echo ""
    	echo "$JDBC_JAR not found."
		echo "Please run perfdmf_configure, if you haven't already done so."
    	echo ""
    	exit
	fi
fi




echo
echo "Configuring scripts to use the following values:"
echo "------------------------------------------------"
echo "tauroot = $tauroot"
echo "architecture = $architecture"
echo "taushell = $taushell"
echo "targetdir = $targetdir"
echo "server = $serverhostname"
echo "configfile = $configfile"
echo "tmpdir = $tmpdir"
echo

echo "TAU: installing tools in $targetdir"

# cat ${targetdir}/etc/perfexplorer.skel | 
# sed -e 's,@TAUROOT@,'$targetdir',' \
# -e 's,@TAUSHELL@,'$taushell',' \
# -e 's,@TAUARCH@,'$architecture',' \
# -e 's,@SERVER_HOSTNAME@,'$serverhostname',' \
# -e 's,@SERVER_OBJECT_PORT@,'$objectport',' \
# -e 's,@SERVER_RMIREGISTRY_PORT@,'$registryport',' \
# -e 's,@CONFIGFILE@,'$configfile',' \
#  > ${targetdir}/${architecture}/bin/perfexplorer
# chmod a+rx ${targetdir}/${architecture}/bin/perfexplorer

# cat ${targetdir}/etc/java.policy | 
# sed -e 's,@TMPDIR@,'$tmpdir',' \
# -e 's,@CONFIGDIR@,'$configdir',' \
# -e 's,@CONFIGDIR2@,'$configdir',' \
# -e 's,@TAULIBDIR@,'$LIBDIR',' \
# -e 's,@TAULIBDIR2@,'$LIBDIR',' \
#  > ${targetdir}/${architecture}/lib/java.policy

JAR_HOME=$HOME/.ParaProf

WEKA_JAR=$JAR_HOME/weka-3-6-1.jar
WEKA_ZIP_DIR=$JAR_HOME
    
if [ ! -r ${WEKA_JAR} ]; then
    echo ""
    echo "$WEKA_JAR not found."
    echo ""

	while [ "x$RESPONSE" = "x" ]; do
	    $echo "Would you like to attempt to automatically download the Weka jar file? (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 the Weka jar file."
		#echo "Please go here: "
		#exit 1
	    #fi
	    
		/bin/rm -f ${redirectfile}
		#wget http://www.cs.uoregon.edu/research/paracomp/tau/${redirectfile}
		java -cp ${LIBDIR}/tau-common.jar edu.uoregon.tau.common.Wget $redirect ${redirectfile} false
		URL=`grep URL ${redirectfile} | sed s/URL=//`
		FILE=`grep FILE ${redirectfile} | sed s/FILE=//`
		DIR=`grep DIR ${redirectfile} | sed s/DIR=//`
		/bin/rm -f ${redirectfile}
		echo "Getting $FILE... please be patient..."
		#wget "$URL"
		java -cp ${LIBDIR}/tau-common.jar edu.uoregon.tau.common.Wget "$URL" "$FILE" true
		mv "$FILE" ${WEKA_ZIP_DIR}/.
		cd ${WEKA_ZIP_DIR}/.
		java -cp ${LIBDIR}/tau-common.jar edu.uoregon.tau.common.Zip ${FILE}
		/bin/rm -f ${redirectfile}
		/bin/rm -f $FILE
		cp $DIR/weka.jar ./weka-3-6-1.jar
		/bin/rm -rf $DIR
	    
	    if [ ! -r ${WEKA_JAR} ]; then
		echo ""
		echo "Still couldn't find $WEKA_JAR, please acquire your Weka jar file manually"
		echo ""
		
		exit
	    fi
	else
		echo ""
		echo "Please acquire your Weka jar file manually from"
		echo "the following redirection link (recommended):"
                echo " ${redirect} "
                RESPONSE=""
                while [ "x$RESPONSE" = "x" ]; do
                   $echo "Have you already download the jar file? (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
                   $echo "Please give the full name to the jar file: ${nnl}"
                   read FILE
		   cp "$FILE" ${WEKA_ZIP_DIR}/.
		   cd ${WEKA_ZIP_DIR}/.
		   java -cp ${LIBDIR}/tau-common.jar edu.uoregon.tau.common.Zip weka-3-6-1.zip
		   mv weka-3-6-1/weka.jar ./weka-3-6-1.jar
                   /bin/rm -rf weka-3-6-1/
                   /bin/rm -rf weka-3-6-1.zip
                fi


	fi
fi

RESPONSE=
JBOSS_JAR=$JAR_HOME/drools-core-3.0.6.jar
JBOSS_TAR_DIR=$JAR_HOME
    
if [ ! -r ${JBOSS_JAR} ]; then
    echo ""
    echo "$JBOSS_JAR not found."
    echo ""

	while [ "x$RESPONSE" = "x" ]; do
	    $echo "Would you like to attempt to automatically download the required jar files? (y/n) ${nnl}"
	    read RESPONSE
	    case $RESPONSE in
		[yY]|[Yy][Ee][Ss]) RESPONSE=y ;;
		[nN]|[Nn][Oo]) RESPONSE=n ;;
	    esac
	done
	
	FILE=PE2_jars.tgz
	URL=http://www.cs.uoregon.edu/research/paracomp/tau/$FILE
	
	if [ "${RESPONSE}" = "y" ]; then 
		/bin/rm -f $FILE
		echo "Getting $FILE... please be patient..."
		#wget "$URL"
		java -cp ${LIBDIR}/tau-common.jar edu.uoregon.tau.common.Wget "$URL" "$FILE" true
		mv "$FILE" ${JBOSS_TAR_DIR}/.
		cd ${JBOSS_TAR_DIR}/.
		java -cp ${LIBDIR}/tau-common.jar edu.uoregon.tau.common.tar.Tar -xvzf ${FILE}
		/bin/rm -f $FILE
		mv jbossrules/*.jar .
		mv jbossrules/lib/*.jar .
		rm -rf jbossrules
	    
		if [ ! -r ${JBOSS_JAR} ]; then
		echo ""
		echo "Still couldn't find $JBOSS_JAR, please acquire JBoss Rules manually."
		echo ""
		
		exit
	    fi
	else
		echo ""
		echo "Please manually download the required jar files using the following link:"
		echo "--->	$URL"
		echo ""
            RESPONSE=
            while [ "x$RESPONSE" = "x" ]; do
              $echo "Have you already downloaded the file?[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 
              $echo "Please enter the location of the file: ${nnl}"
              read FILE
                cp "$FILE" ${JBOSS_TAR_DIR}/.
                cd ${JBOSS_TAR_DIR}/.
                java -cp ${LIBDIR}/tau-common.jar edu.uoregon.tau.common.tar.Tar -xvzf ${FILE}
                mv jbossrules/*.jar .
                mv jbossrules/lib/*.jar .
                rm -rf jbossrules
            else 
	      exit
            fi
	fi
fi

# now to test the DB and upload schema (if necessary)

java -Dderby.system.home=${configdir} -cp ${PERFEXPLORER_JAR}:${LIBDIR}/perfdmf.jar:${LIBDIR}/jargs.jar:${JDBC_JAR}:${LIBDIR}/tau-common.jar edu.uoregon.tau.perfexplorer.common.Configure -t ${tauroot}/etc -a ${architecture} -g ${configfile}



# bye bye
echo 
echo "Configuration complete!"
echo "   If you haven't already done so, "
echo "   Please add " $tauroot/$architecture/bin " to your path"

exit 0







