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

. $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"

   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


   CHART_DIR=$ORGPWD/charts
   if [ ! -d "$CHART_DIR" ]; then
      makeDirectory $CHART_DIR
      if [ $? -ne 0 ]; then
         exit 1
      fi
   fi

   EXPORT_DIR=$ORGPWD/export
   if [ ! -d "$EXPORT_DIR" ]; then
      makeDirectory $EXPORT_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`
   
}

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

   for i in $ARCO_WRITE_USERS; do
      echo "            <applUser>$i</applUser>" >> /tmp/arco_write_users.txt
   done   
   sed -e '/ARCO_WRITE_USERS/r /tmp/arco_write_users.txt' \
       -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/config.xml.template > $ORGPWD/config.xml

   rm /tmp/arco_write_users.txt
}


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



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
$INFOTEXT "Register the N1 SGE reporting module in the webconsole"
smreg add -a $ORGPWD
smreg add -p -e arco_config_file=/usr/share/webconsole/reporting/config.xml
smreg add -p -e arco_logging_level=INFO
chown -R $ARCO_USER:$ARCO_GROUP /usr/share/webconsole/reporting
/usr/sadm/bin/smcwebserver start
