#!/bin/sh
#
# Grid Engine Portal installation script
#
#___INFO__MARK_BEGIN__
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
#___INFO__MARK_END__
#
#set -x

# Reset PATH to a safe value  
#
PATH=/bin:/usr/bin:/usr/ucb

# Easy way to prevent clearing of screen
#
CLEAR=clear
#CLEAR=:


# set to false if you don't want this script uses i18n functions
#
SGE_INFOTEXT_MAX_COLUMN=500; export SGE_INFOTEXT_MAX_COLUMN

#-------------------------------------------------------------------------
# USEFUL LOCAL SHELL PROCEDURES

#-------------------------------------------------------------------------
# ErrUsage: print usage string, exit
#
ErrUsage()
{
   myname=`basename $0`
   $ECHO >&2
   $INFOTEXT -e "Usage: %s {-gp -help}\n" \
                " -gp         install the Grid Engine Portal servlets\n" \
                " -help       show this output" $myname
   exit 1
}

#--------------------------------------------------------------------------
# InstallGridEnginePortal install the Grid Engine Portal glue
#
InstallGridEnginePortal()
{
   DDOMAIN=`domainname| sed 's/federation\.//'`
   DWS=https-`hostname`.$DDOMAIN

   $CLEAR
   $INFOTEXT "Copyright © 2002 Sun Microsystems, Inc.  All rights reserved."
   $ECHO
   $INFOTEXT -u "Installing Grid Engine Portal servlets"
   $INFOTEXT "We will ask for\n" \
             "- the O'Reilly servlet location\n" \
             "- the SunONE Portal Server root directory\n" \
             "- the Vnc root directory (optional)\n" \
             "- the Grid Engine Portal domain\n" \
             "- the S1WS host instance\n" \
             "- the S1WS host virtual server"

   $ECHO
   $INFOTEXT "The Grid Engine Portal is using the O'Reilly servlet cos.jar\n" \
             "The version we used for testing is available as\n" \
             "http://www.servlets.com/cos/cos-27May2002.zip\n" \
             "Please read the license conditions carefully and download\n" \
             "this file. Then proceed by unzipping it, e.g. like this\n" \
             "% cd /tmp\n" \
             "% unzip cos-27May2002.zip lib/cos.jar\n" \
             "This path must be entered here, e.g. /tmp/lib/cos.jar"

   $ECHO
   $INFOTEXT "The following default installation information is available"
   $INFOTEXT "O'Reilly servlet:          %s" "$OREILLY_SERVLET"
   $INFOTEXT "Grid Engine Portal root:   %s" "$GEP_ROOT"
   $INFOTEXT "Application home:          %s" "$APP_HOME"
   $INFOTEXT "S1PS install dir:          %s" "$IPS"
   $INFOTEXT "Grid Engine Portal domain: %s" "$IPS_DOMAIN"
   $INFOTEXT "S1WS host instance:        %s" "$IWS_HOST_INSTANCE"
   $INFOTEXT "S1WS host vs:              %s" "$IWS_HOST_VS"
   $INFOTEXT "VNC root directory:        %s" "$VNC_ROOT"
   $ECHO
   $INFOTEXT -n -ask y n -def y "Do you want to use these parameters (y/n) [y]>> "

   if [ $? = 0 ]; then
      done=true
   else   
      done=false
   fi
             
   while [ $done = false ]; do
      
      $ECHO
      dndone=false
      while [ $dndone = false ]; do
         $INFOTEXT -n "Please enter the O'Reilly servlet path >> "
         OREILLY_SERVLET=`Enter ""`
         if [ ! -f "$OREILLY_SERVLET" ]; then
            $INFOTEXT "%s not found" $OREILLY_SERVLET
         else   
            dndone=true
         fi
      done

      dndone=false
      while [ $dndone = false ]; do
         $INFOTEXT -n "Please enter the Application home path >> "
         APP_HOME=`Enter ""`
         if [ ! -d "$APP_HOME" ]; then
            $INFOTEXT "%s not found" $APP_HOME
         else   
            dndone=true
         fi
      done

      dndone=false
      while [ $dndone = false ]; do
         $INFOTEXT -n "Please enter the S1PS install directory ( /opt ) >> "
         IPS=`Enter ""`
         if [ ! -d "$IPS" ]; then
            $INFOTEXT "%s is not accessable" $IPS
         else   
            dndone=true
         fi
      done

      dndone=false
      while [ $dndone = false ]; do
         $INFOTEXT -n "Please enter the Grid Engine Portal domain ( $DDOMAIN ) >> "
         IPS_DOMAIN=`Enter ""`
         if [ "$IPS_DOMAIN" = "" ]; then
            $INFOTEXT "empty domain name"
         else   
            dndone=true
         fi
      done
      
      dndone=false
      while [ $dndone = false ]; do
         $INFOTEXT -n "Please enter the S1WS host instance ( $DWS ) >> "
         IWS_HOST_INSTANCE=`Enter ""`
         if [ "$IWS_HOST_INSTANCE" = "" ]; then
            $INFOTEXT "empty host instance"
         else   
            dndone=true
         fi
      done

      dndone=false
      while [ $dndone = false ]; do
         $INFOTEXT -n "Please enter the S1WS virtual server ( %s ) >> " $IWS_HOST_INSTANCE
         IWS_HOST_VS=`Enter ""`
         if [ "$IWS_HOST_VS" = "" ]; then
            $INFOTEXT "empty virtual server"
         else   
            dndone=true
         fi
      done

      $INFOTEXT -n -ask y n -def n "Do you want to use VNC (y/n) [n]>> "
      if [ $? = 0 ]; then
         dndone=false
         while [ $dndone = false ]; do
            $INFOTEXT -n "Please enter the VNC root directory >> "
            VNC_ROOT=`Enter ""`
            if [ ! -d "$VNC_ROOT" ]; then
               $INFOTEXT "%s is not accessable" $VNC_ROOT
            else   
               dndone=true
            fi
         done
      else
         VNC_ROOT=""
      fi



      $CLEAR
      $ECHO
      $INFOTEXT "You entered the following installation information"
      $INFOTEXT "O'Reilly servlet:          %s" "$OREILLY_SERVLET"
      $INFOTEXT "Grid Engine Portal root:   %s" "$GEP_ROOT"
      $INFOTEXT "Application home:          %s" "$APP_HOME"
      $INFOTEXT "S1PS install dir:          %s" "$IPS"
      $INFOTEXT "Grid Engine Portal domain: %s" "$IPS_DOMAIN"
      $INFOTEXT "S1WS host instance:        %s" "$IWS_HOST_INSTANCE"
      $INFOTEXT "S1WS host vs:              %s" "$IWS_HOST_VS"
      $INFOTEXT "VNC root directory:        %s" "$VNC_ROOT"
      $ECHO
      $INFOTEXT -n -ask y n -def y "Do you want to use these parameters (y/n) [y]>> "

      if [ $? = 0 ]; then
         done=true
      fi
   done

   $INFOTEXT "The Grid Engine Portal servlets will now be installed."
   done=false
   while [ $done = false ]; do
      $INFOTEXT -n -ask y n -def y "Do you want to proceed (y/n) [y]>> "
      if [ $? = 0 ]; then
   	 InstallServlet
         done=true
      else
      	$INFOTEXT "The installation has not been changed."
      fi
   done
   #   $INFOTEXT -n -ask y n -def y "Do you want to install example applications (y/n) [y]>> "
   # if [ $? = 0 ]; then
   #    InstallExampleApplications
   # fi
   SetPermissions
   $INFOTEXT -n -ask y n -def n "Do you want to install the GEPTableContainer (y/n) [n]>> "
   if [ $? = 0 ]; then
      InstallChannels
   fi
   $INFOTEXT "The installation is now complete."
}

InstallServlet()
{
   # important env vars
   # IWS_HOST_INSTANCE=https-balin.germany.sun.com
   # IWS_HOST_VS=https-balin.germany.sun.com
   IWS_SERVER_HOME=$IPS/SUNWam/servers
   export IWS_SERVER_HOME
   
   # copy O'Reilly servlet to WEB-INF/lib, generate war file and deploy it under $GEP_ROOT/deploy under URI /gridportal
   cp $OREILLY_SERVLET $GEP_ROOT/WEB-INF/lib

   # prepare the deployment descriptor and display it
   sed "s#GEP_ROOT#$GEP_ROOT#" $GEP_ROOT/WEB-INF/web.xml.template | \
   sed "s#APP_HOME#$APP_HOME#" | \
   sed "s#SGE_ROOT#$SGE_ROOT#" | \
   sed "s#SGE_CELL#$SGE_CELL#" | \
   sed "s#COMMD_PORT#$COMMD_PORT#" | \
   sed "s#VNC_ROOT#$VNC_ROOT#" > $GEP_ROOT/WEB-INF/web.xml

   # enter the Grid Engine and Xvnc info and change the deployment descriptor WEB-INF/web.xml
   $INFOTEXT -n -ask y n -def n "Do you want to edit the deployment descriptor (y/n) [n]>> "
   if [ $? = 0 ]; then
      if [ "$EDITOR" != "" ];then
         $EDITOR $GEP_ROOT/WEB-INF/web.xml
      else
         vi $GEP_ROOT/WEB-INF/web.xml
      fi
   fi

   # deploy the servlet 
   jar cvf gp.war WEB-INF 
   $IWS_SERVER_HOME/bin/https/httpadmin/bin/wdeploy delete -u /gridportal -i $IWS_HOST_INSTANCE -v $IWS_HOST_VS soft
   $IWS_SERVER_HOME/bin/https/httpadmin/bin/wdeploy deploy -u /gridportal -i $IWS_HOST_INSTANCE -v $IWS_HOST_VS -d `pwd`/deploy gp.war
}

# Set permissions
#
SetPermissions()
{
   chmod 755 bin WEB-INF channels apps
   chmod 555 bin/*
   chmod 755 WEB-INF/lib
   chmod 444 WEB-INF/lib/*
   chmod 777 workspace
}

# install channels 
#
InstallChannels()
{
   $ECHO
   dndone=false
   while [ $dndone = false ]; do
      $INFOTEXT "Please enter the amAdmin password >> "
      stty -echo
      ADMINPW=`Enter ""`
      stty echo
      if [ "$ADMINPW" = "" ]; then
         $INFOTEXT "empty password"
      else   
         dndone=true
      fi
   done

   $INFOTEXT -n "Adding the channels. This may take some seconds.  "
   # $IPS/SUNWps/bin/dpadmin list -u "uid=amAdmin,ou=People,$IPS_DOMAIN" -w $ADMINPW -d "$IPS_DOMAIN"
   $IPS/SUNWps/bin/dpadmin add -u "uid=amAdmin,ou=People,$IPS_DOMAIN" -w $ADMINPW -d "$IPS_DOMAIN" channels/GEPTableContainer.xml

   $INFOTEXT "Follow the instructions in the installation guide to add roles\nand to make the GEPTableContainer available."
}

# install example applications 
#
InstallExampleApplications()
{
   # cp $TEMPLATE_HOME/login_template.html /etc/opt/SUNWips/auth/default
   # (cd $IPS/SUNWips/public_html; tar xf $TEMPLATE_HOME/images.tar)
   # tar xf $TEMPLATE_HOME/apps.tar
   $INFOTEXT "The installation of applications has completed."
}

#--------------------------------------------------------------------------
# THE MAIN PROCEDURE
#--------------------------------------------------------------------------

#----------------------------------
# GET ARCH + ARCH SPECIFIC DEFAULTS
#

umask 022

if [ -z "$SGE_ROOT" -o ! -d "$SGE_ROOT" ]; then
   echo
   echo ERROR: Please set your \$SGE_ROOT environment variable
   echo and start this script again.
   echo
   exit 1
fi

if [ "$COMMD_PORT" = "" ]; then
   echo
   echo ERROR: Please set your \$COMMD_PORT environment variable
   echo and start this script again.
   echo
   exit 1
fi


if [ ! -f "$SGE_ROOT/util/arch" ]; then
   echo
   echo ERROR: The shell script \"$SGE_ROOT/util/arch\" does not exist.
   echo Please verify your distribution and restart this script.
   echo
   exit 1
fi

if [ ! -f $SGE_ROOT/util/arch_variables ]; then
   echo
   echo ERROR: Missing shell script \"$SGE_ROOT/util/arch_variables\".
   echo Please verify your distribution and restart this script.
   echo
   exit 1
fi

. $SGE_ROOT/util/arch_variables

if [ "$SGE_CELL" = "" ]; then
   SGE_CELL=default
fi

#-------------------------------------------------------------------------
# setup i18n

if [ "$GETTEXT" != "" -a "SGE_I18N" = true ]; then
   unset TEXTDOMAINDIR TEXTDOMAIN
   TEXTDOMAINDIR="`/bin/pwd`/locale"
   TEXTDOMAIN=gridengine
   translation=1
else
   translation=0
   unset LANG LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY
   unset LC_NUMERIC LC_TIME LANGUAGE
fi

# end of internationalization setup
#-------------------------------------------------------------------------

# Set variable $ADMINUSER
#
SetAdminUser

if [ "$LD_LIBRARY_PATH" != "" ]; then
   LD_LIBRARY_PATH=$SGE_ROOT/lib/$ARCH:$LD_LIBRARY_PATH 
else   
   LD_LIBRARY_PATH=$SGE_ROOT/lib/$ARCH
fi   
export LD_LIBRARY_PATH

V5BIN=$SGE_ROOT/bin/$ARCH
V5UTILBIN=$SGE_ROOT/utilbin/$ARCH


euid=`$V5UTILBIN/uidgid -euid`

#-----------------------------
# setup INFOTEXT
#-----------------------------
INFOTEXT=$V5UTILBIN/infotext
if [ ! -f $INFOTEXT ]; then
   $ECHO "can't find \"$INFOTEXT\""
   $ECHO "Installation failed."
   exit 1
fi

ME=`whoami`

if [ "$ME" = "" ]; then
   $INFOTEXT "Can't determine your username with \"%s\" command. Exit" whoami
   exit 1
fi

GEP_ROOT=`pwd`

if [ -f $GEP_ROOT/install.defaults ]; then
   . $GEP_ROOT/install.defaults
   INSTALL_DEFAULTS_PRESENT="true"
fi   

#-----------------------------
# CommandLine Argument Parsing
#
WHICH="undef"

ARGC=$#
while [ $ARGC != 0 ]; do
   case $1 in
   -gp)
      WHICH="gp"
      ;;
   -help)
      WHICH="help"
      ;;
   *)
      $ECHO "" >&2
      Translate "Error: Unknown option %s" 2 1 "$1"
      ErrUsage
      ;;
   esac
   shift
   ARGC=`expr $ARGC - 1`
done

if [ "$WHICH" = "undef" ]; then
   ErrUsage
fi

case $WHICH in
gp)
   InstallGridEnginePortal
   ;;
help)
    ErrUsage
    ;;
esac

exit 0
