#!/bin/sh
#
# Grid 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 Portal servlets\n" \
                " -help       show this output" $myname
   exit 1
}

#--------------------------------------------------------------------------
# InstallServlets install the Grid Portal glue
#
InstallServlets()
{
   $CLEAR
   $INFOTEXT "Copyright © 2002 Sun Microsystems, Inc.  All rights reserved."
   $ECHO
   $INFOTEXT -u "Installing Grid Portal servlets"
   $INFOTEXT "We will ask for\n" \
             "- the O'Reilly servlet location\n" \
             "- the iPlanet Portal Server root directory\n" \
             "- the Vnc root directory (optional)\n" \
             "- the Grid Portal domain"
   $ECHO

   done=false
   while [ $done = false ]; do
      
      $INFOTEXT "The Grid 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
      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

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

      $ECHO
      $INFOTEXT -n -ask y n -def y "Do you want to use VNC >> "
      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


      $ECHO
      dndone=false
      while [ $dndone = false ]; do
         $INFOTEXT -n "Please enter the Grid Portal domain >> "
         IPS_DOMAIN=`Enter ""`
         if [ "$IPS_DOMAIN" = "" ]; then
            $INFOTEXT "empty domain name"
         else   
            dndone=true
         fi
      done
      
      $CLEAR
      $ECHO
      $INFOTEXT "You entered the following installation information"
      $INFOTEXT "O'Reilly servlet:      %s" "$OREILLY_SERVLET"
      $INFOTEXT "Grid Portal root:      %s" "$SGP_ROOT"
      $INFOTEXT "iPS install directory: %s" "$IPS"
      $INFOTEXT "VNC root directory:    %s" "$VNC_ROOT"
      $INFOTEXT "Grid Portal domain:    %s" "$IPS_DOMAIN"
      $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 Portal servlets will now be installed. iPS is automatically restarted."
   done=false
   while [ $done = false ]; do
      $INFOTEXT -n -ask y n -def y "Do you want to proceed (y/n) [y]>> "
      if [ $? = 0 ]; then
         done=true
      else
      	$INFOTEXT "The installation has not been changed."
      fi
   done
   StopiPS
   InstallServlet
   $INFOTEXT -n -ask y n -def y "Do you want to install example applications (y/n) [y]>> "
   if [ $? = 0 ]; then
      InstallExampleApplications
   fi
   SetPermissions
   StartiPS
   $INFOTEXT -n -ask y n -def n "Do you want to install channels (y/n) [n]>> "
   if [ $? = 0 ]; then
      InstallChannels
   fi
   $INFOTEXT "The installation has completed."
}

InstallServlet()
{
   ################################################################################
   TEMPLATE_HOME=$SGP_ROOT/templates                   #Directory where the example portal files reside
   APPS=$SGP_ROOT/apps		                              #Application Directory
   # The host name as per netscapes install
   SYSNAME=`ls $IPS/netscape/server4 | grep https | grep -v https-admserv | cut -d - -f 2-`
   # directory where servlets.properties and rules.properties live
   IPS_CONFIG=$IPS/netscape/server4/https-$SYSNAME/config

   cp $OREILLY_SERVLET $SGP_ROOT/lib

   # MODIFY THE FILE rules.properties
   if ( grep SunTCP $IPS_CONFIG/rules.properties > /dev/null )
   then
      echo "No change to $IPS_CONFIG/rules.properties"
   else
      cp $IPS_CONFIG/rules.properties $IPS_CONFIG/rules.properties.save
      echo "/SunTCP=SunTCP\n/AdminTCP=AdminTCP" >> $IPS_CONFIG/rules.properties
   fi

   # MODIFY THE FILE servlets.properties
   cp $IPS_CONFIG/servlets.properties $IPS_CONFIG/servlets.properties.`date '+%Y%m%d%H%M%S'`
   if ( grep "TCP" $IPS_CONFIG/servlets.properties > /dev/null ); then
      sed -e '/TCP/d' $IPS_CONFIG/servlets.properties > $IPS_CONFIG/servlets.template
   else
      cp $IPS_CONFIG/servlets.properties $IPS_CONFIG/servlets.template
   fi
   echo "servlet.SunTCP.code=com.sun.tcp.SunTCP\nservlet.SunTCP.classpath=$SGP_ROOT/lib/suntcp.jar:$SGP_ROOT/lib/cos.jar\nservlet.AdminTCP.code=com.sun.tcp.AdminTCP\nservlet.AdminTCP.classpath=$SGP_ROOT/lib/suntcp.jar:$SGP_ROOT/lib/cos.jar\nservlet.SunTCP.initArgs=sgp_root="$SGP_ROOT/","app_home=$APPS/",sge_root="$SGE_ROOT/",sge_cell="$SGE_CELL",commd_port=$COMMD_PORT,vncroot=$VNC_ROOT\nservlet.AdminTCP.initArgs=sgp_root="$SGP_ROOT/",app_home="$APPS/",sge_root="$SGE_ROOT/",sge_cell="$SGE_CELL",commd_port=$COMMD_PORT\n" >> $IPS_CONFIG/servlets.template
   mv $IPS_CONFIG/servlets.template $IPS_CONFIG/servlets.properties

}

# Set permissions
#
SetPermissions()
{
   chmod 755 bin lib templates apps
   chmod 555 bin/*
   chmod 444 lib/*
   chmod 777 workspace
}

# START iPS
#
StartiPS()
{
   /etc/init.d/ipsserver start 
   /etc/init.d/ipsgateway start 
}

# STOP iPS
#
StopiPS()
{
   /etc/init.d/ipsserver stop 
   /etc/init.d/ipsgateway stop 
}

# install channels 
#
InstallChannels()
{
   # VARIABLES (User Set)
   IPSADMIN=$IPS/SUNWips/bin/ipsadmin           #iPS ipsadmin command 

   cd templates/xml
   mkdir -p tmp

   # Create domain updates with the new Desktop Channel names
   cat suntcp.xml           | sed -e "s/suntcp/$IPS_DOMAIN/g" > tmp/domain.xml
   cat suntcp.AdminRole.xml | sed -e "s/suntcp/$IPS_DOMAIN/g" > tmp/AdminRole.domain.xml

   # Create the new channels from the templates
   sed -e "s/suntcp/$IPS_DOMAIN/g" suntcpProjectList.xml > tmp/PL.xml
   sed -e "s/suntcp/$IPS_DOMAIN/g" suntcpApplicationList.xml > tmp/AL.xml
   sed -e "s/suntcp/$IPS_DOMAIN/g" suntcpAdminApplicationList.xml > tmp/AAL.xml
   sed -e "s/suntcp/$IPS_DOMAIN/g" suntcpJobList.xml > tmp/JL.xml
   sed -e "s/suntcp/$IPS_DOMAIN/g" suntcpAdminJobControl.xml > tmp/AJC.xml

   # Update the domains with the new Desktop Channel names
   $IPSADMIN change domain $IPS_DOMAIN tmp/domain.xml
   $IPSADMIN change domain $IPS_DOMAIN/AdminRole tmp/AdminRole.domain.xml

   # Add channels to the Portal Server
   $IPSADMIN -import tmp/PL.xml
   $IPSADMIN -import tmp/AL.xml
   $IPSADMIN -import tmp/AAL.xml
   $IPSADMIN -import tmp/JL.xml
   $IPSADMIN -import tmp/AJC.xml
}

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

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

if [ $euid = 0 ]; then
   CALOCALTOP=/var/sgeCA/$CA_PORT/$SGE_CELL
   rootinstalls=true
else
   CALOCALTOP=/tmp/sgeCA/$CA_PORT/$SGE_CELL
   rootinstalls=false
fi

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

SGP_ROOT=`pwd`


#-----------------------------
# 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)
   InstallServlets
   ;;
help)
    ErrUsage
    ;;
esac

exit 0
