#!/bin/sh
#
# (c) 2004 Sun Microsystems, Inc. Use is subject to license terms.
#
#  Installation script for the Grid Engine reporting module
#
#  Scriptname: inst_reporting
#

ORGPWD=`pwd`
umask 022

cd ..

. ./util/arch_variables
. ./util/install_modules/inst_common.sh


EnterPW_oldmods=`stty -g`


BasicSettings
SetUpInfoText
SetAdminUser


. $ORGPWD/inst_util.sh

ARCO_USER=noaccess
ARCO_GROUP=noaccess

makeDirectory() {
  $INFOTEXT "Create directory $1"
  mkdir -p $1
  if [  $? -eq 0 ]; then
    chown $ARCO_USER:$ARCO_GROUP $1 
    chmod 755 $1
    return 0
  fi
  return 1
}


########################################################################
# Query all parameters for the installation
########################################################################
queryParams() {

   queryJavaHome

   SPOOL_DIR=/var/spool/arco

   $INFOTEXT -u "Spool directory"

   $INFOTEXT "\nIn the spool directory the N1 SGE reporting module will"
   $INFOTEXT "store all queries and results\n"

   
   dummy=$SPOOL_DIR
   $INFOTEXT -n "Please enter the path to the spool directory [$dummy] >> "
   SPOOL_DIR=`Enter $dummy`

   QUERY_DIR=$SPOOL_DIR/queries
   RESULT_DIR=$SPOOL_DIR/results

   ########################

   $INFOTEXT "\n"
   $INFOTEXT -u "\nDatabase Setup"

   # set the default database user
   DB_USER=arco_read
   while : 
   do
     $INFOTEXT -ask p o -def p -n \
               "\nEnter your database type ( o = Oracle, p = PostgreSQL ) [p] >> "
     if [ $? -eq 0 ]; then
         DB_DRIVER_JAR=$DBWRITER_PWD/lib/postgres-7.4.2.jar
         DB_TYPE="postgresql"
         queryPostgres
     else 
         DB_TYPE=oracle
         queryOracle
     fi

     searchJDBCDriverJar $DB_DRIVER $ORGPWD/WEB-INF/lib

     for i in  $ORGPWD/WEB-INF/lib/*.jar; do
         CP=$CP:$i
     done

     testDB
     if [ $? -eq 0 ]; then
        break
     else
        $INFOTEXT -ask y n -def y \
                  -n "Do you want to repeat database connection setup? (y/n) [y] >>" 
        if [ $? -ne 0 ]; then
           break
        fi
     fi
   done

   #######################

   $INFOTEXT -u "\nConfigure users with write access"

	ARCO_WRITE_USERS=$ADMINUSER
   while :
   do
       if [ "$ARCO_WRITE_USERS" != "" ]; then
          $INFOTEXT "\nUsers: $ARCO_WRITE_USERS"
       fi 
       $INFOTEXT -n "Enter a login name of a user (Press enter to finish) >> "
       dummy=`Enter`
       if [ "$dummy" = "" ]; then
          break
       fi
       if [ "$ARCO_WRITE_USERS" = "" ]; then
          ARCO_WRITE_USERS="$dummy"
       else
         ARCO_WRITE_USERS="$ARCO_WRITE_USERS $dummy"
       fi
   done
}

########################################################################
# Remove old instances of the N1 Sun Grid Engine reporting module 
########################################################################
removeOld() {

  names="reporting_ com.sun.grid.arco"

  for name in $names; do
    dummy=`smreg list -a | grep "$name" | awk '{print $1}'`
    if [ "$dummy" != "" ]; then
       app="$app $dummy"
    fi
  done

  if [ "$app" != "" ]; then

    $INFOTEXT -u "Found old versions of the N1 SGE reporting module"

    for i in $app; do
       $INFOTEXT -ask n y -def n -n \
                 "Should the application $i be deleted? (y/n) [n] >> "

       if [ $? -ne 0 ]; then

         smreg remove -a "$i"

       fi 

    done
 
  fi

}

createSpoolDir() {

   if [ ! -d "$SPOOL_DIR" ]; then
      $INFOTEXT -ask y n -def y -n \
                "\nDirectory $SPOOL_DIR does not exist, create it? (y/n) [y] >> "
      if [ $? -eq 0 ]; then
        makeDirectory $SPOOL_DIR
        if [ $? -eq 0 ]; then
           break
        else 
          exit 1
        fi
      else
        exit 1
      fi
   fi

   QUERY_DIR=$SPOOL_DIR/queries
   if [ ! -d "$QUERY_DIR" ]; then
      makeDirectory $QUERY_DIR
      if [ $? -ne 0 ]; then
         exit 1
      fi
      $INFOTEXT "Copy predefined queries into $QUERY_DIR"
      if [ $DB_DRIVER = "oracle.jdbc.driver.OracleDriver" ]; then
         cp $ORGPWD/database/example_queries/oracle/*.xml $QUERY_DIR
      else
         cp $ORGPWD/database/example_queries/postgres/*.xml $QUERY_DIR
      fi

      chown $ARCO_USER:$ARCO_GROUP $QUERY_DIR/*.xml 
   else
      $INFOTEXT "query directory $QUERY_DIR already exists, predefined queries will not be installed"
   fi

   RESULT_DIR=$SPOOL_DIR/results
   if [ ! -d "$RESULT_DIR" ]; then
      makeDirectory $RESULT_DIR
      if [ $? -ne 0 ]; then
         exit 1
      fi
   fi

}

encryptPW() {
 
  DB_PW=`echo $1 | $JAVA_HOME/bin/java -cp $ORGPWD/WEB-INF/lib/reporting.jar com.sun.grid.reporting.util.CryptoHandler`
   
}

getSWCVersion() {
	SWC_VERSION=`smreg list -a | grep com.sun.web.console| cut -f1,2 |cut -f2,2 -d _`
	
	SWC_MAJOR=`echo $SWC_VERSION | cut -f1,1 -d .`
	SWC_MINOR=`echo $SWC_VERSION | cut -f2,2 -d .`
   echo "$SWC_MAJOR.$SWC_MINOR"	
}

########################################################################
#
# create config.xml from
# config.xml.template
#
########################################################################
createConfigFile() {

   
   #  encrypt the password

   DB_PW=`echo $DB_PW | $JAVA_HOME/bin/java -cp $ORGPWD/WEB-INF/lib/reporting.jar com.sun.grid.reporting.util.CryptoHandler 2> /dev/null`

	TMP_USER_FILE=/tmp/user$$
	TMP_CONFIG_FILE=/tmp/config$$
	
	
   for i in $ARCO_WRITE_USERS; do
      echo "            <applUser>$i</applUser>" >> $TMP_USER_FILE
   done
	
	 sed -e "/ARCO_WRITE_USERS/r $TMP_USER_FILE" \
		  -e "/ARCO_WRITE_USERS/d" \
		  -e "s?DBNAME?$DB_NAME?g" \
		  -e "s?DBHOST?$DB_HOST?g" \
		  -e "s?DBPORT?$DB_PORT?g" \
		  -e "s?COMMON_PATH?$SPOOL_DIR?g" \
		  -e "s?DBUSER?$DB_USER?g" \
		  -e "s?DBSCHEMA?$DB_SCHEMA?g" \
		  -e "s?DBDRIVER?$DB_DRIVER?g" \
		  -e "s?DBTYPE?$DB_TYPE?g" \
		  -e "s?DBPWD?$DB_PW?g" $ORGPWD/util/config.xml.template > $TMP_CONFIG_FILE 			 			 

   ExecuteAsAdmin cp $TMP_CONFIG_FILE $ORGPWD/config.xml
   rm $TMP_USER_FILE $TMP_CONFIG_FILE
	
}

#
#  Create the file which depends on the version of SWC
#  Input  SWC_VERION
createSWCFiles() {

	# Create the correct app.xml (depends on SWC Version	
	SWC_VERSION=`getSWCVersion`
	
	case $SWC_VERSION in
	   "2.1")
		    ExecuteAsAdmin \
			     cp $ORGPWD/util/app-2.1.xml $ORGPWD/WEB-INF/app.xml;
		        INSTALL_DIR=/var/opt/webconsole/webapps/reporting;;
		"2.2")
		    ExecuteAsAdmin \
			     cp $ORGPWD/util/app-2.2.xml $ORGPWD/WEB-INF/app.xml;
				  INSTALL_DIR=/var/opt/webconsole/webapps/reporting;;
		"2.0")
		    ExecuteAsAdmin \
			     cp $ORGPWD/util/app-2.0.xml $ORGPWD/app.xml;
				  INSTALL_DIR=/usr/share/webconsole/reporting;;
		*)
		   $INFOTEXT "Unknown sun web console version $SWC_VERSION";
			exit 1;;
	esac	
}


#############################################################################
#  Main
#############################################################################

$INFOTEXT -u "\nWelcome to the  N1 SGE reporting module installation"

$INFOTEXT "The installation will take approximately 5 minutes\n"

$INFOTEXT -wait -n "Hit <RETURN> to continue >> "

# Determine the version of SWC 
SWC_VERSION=`getSWCVersion`

case $SWC_VERSION in 
  "2.2") ;;
  "2.1") ;;
  "2.0") ;;
  *)  
      $INFOTEXT "ARCo can not be installed on Sun Webconsole Version $SWC_VERION";
		exit 1;;
esac


while :
do
  queryParams

  $INFOTEXT -u "\nAll parameters are now collected"

  $INFOTEXT "       SPOOL_DIR=$SPOOL_DIR"
  $INFOTEXT "          DB_URL=$DB_URL"
  $INFOTEXT "         DB_USER=$DB_USER"
  $INFOTEXT "ARCO_WRITE_USERS=$ARCO_WRITE_USERS"

   $INFOTEXT -ask y n -def y -n "\nAre this settings correct? (y/n) [y] >> "
   if [ $? -eq 0 ]; then
       break;
   fi

done

/usr/sadm/bin/smcwebserver stop

removeOld
createSpoolDir
createConfigFile
createSWCFiles

$INFOTEXT "Register the N1 SGE reporting module in the webconsole"
smreg add -a $ORGPWD
smreg add -p -e arco_config_file=$INSTALL_DIR/config.xml
smreg add -p -e arco_logging_level=INFO

smcwebserver start
