#!/bin/ksh

# PTF Advance Edition Install

##########################################################################
# Patch for use with Red Hat Linux  Jul 28, 2001  - Red Hat, Inc.        #
#   David Sainty <dsainty@redhat.com>                                    #
##########################################################################

echo "WebSphere Application Server 3.5, Advanced Edition PTF 4"
echo "Please shut down the Application Server and any Webservers that might be running"
echo "If not the PTF may not be installed properly"
echo "If you want to install silently, please issue install.sh -silent"

SILENT_INSTALL="false"

OS_NAME=`uname`

IHS_INSTALL="true"
JDK_INSTALL="true"

if [[ "$OS_NAME" = "AIX" ]]  ; then
   IHS_DIR="/usr/HTTPServer"
   EXAMPLE_DOC_ROOT=$IHS_DIR/htdocs/en_US
elif [[ "$OS_NAME" = "SunOS" || "$OS_NAME" = "Solaris" ]] ; then
   IHS_DIR="/opt/IBMHTTPD"
   EXAMPLE_DOC_ROOT=$IHS_DIR/htdocs
elif [[ "$OS_NAME" = "HP-UX" ]]  ; then
   IHS_DIR="/opt/HTTPServer"
   EXAMPLE_DOC_ROOT=$IHS_DIR/htdocs
elif [[ "$OS_NAME" = "Linux" ]]  ; then
   IHS_DIR=""
   EXAMPLE_DOC_ROOT=""
   IHS_INSTALL="false"
   JDK_INSTALL="true"
else
   echo "Unspported platform: $OS_NAME"
   echo "Exiting ..."
   exit 1
fi

# Check for silent install
if [[ "$1" = "-silent" ]] ; then
   if [[ "$WAS_DIR" = "" ]] ; then
      echo ""
      echo "Please set all of the following environment variables."
      echo "WAS_DIR : Set it to the WebSphere root directory ( eg: /usr/WebSphere/AppServer )"
      echo "IHS_INSTALL : Set it to true if you want to install IHS PTF, else false"
      echo "DOC_ROOT : Set this to WebServer's Doc root only if you want to upgrade Samples"
      exit 1
   else 
      SILENT_INSTALL="true"
   fi   
fi

if [[ "$SILENT_INSTALL" = "true" ]] ; then
   echo "Installing PTF silently"
fi

DELTA_JAR="was35_adv_ptf_4.jar"
DELTA_LOG="was35_adv_ptf_4.log"
IHS_PTF="ihs_ptf_4.jar"
JAVA_PTF="jdk_ptf_4.jar"
BACKUP_JAR="was35_ptf_4_backup.jar"
JAVA_BACKUP_JAR="jdk_ptf_4_backup.jar"
IHS_BACKUP_JAR="ihs_ptf_4_backup.jar"
UINSTALL_FILE="uninstall_ptf_4.sh"
LOG_FILE="was35_ptf_4.log"
IHS_LOG_FILE="ihs_ptf_4.log"
JAVA_LOG_FILE="jdk_ptf_4.log"

##  echo "OS_NAME      = $OS_NAME"
##  echo "DELTA_JAR    = $DELTA_JAR"
##  echo "DELTA_LOG    = $DELTA_LOG"
##  echo "IHS_PTF      = $IHS_PTF"
##  echo "JAVA_PTF     = $JAVA_PTF"
##  echo "IHS_DIR      = $IHS_DIR"

 if [[ "$SILENT_INSTALL" = "false" ]] ; then
   while :
   do
   {
    echo "Please enter the WebSphere root directory"
    read WAS_DIR
    if [[ "${WAS_DIR}" = "" ]]
    then
       continue
    fi
      
    if [[ ! -d ${WAS_DIR} || ! -f ${WAS_DIR}/properties/com/ibm/websphere/product.xml  ]]
       then
         {
            echo
            echo "Unable to locate file: ${WAS_DIR}/properties/com/ibm/websphere/product.xml"
            echo 
            continue
         }
     fi

     break
    }
    done
 fi

# Installing JDK PTF
export JAVA_HOME=$WAS_DIR/java
if [[ "$OS_NAME" = "AIX" ]]  ; then
  export PATH=$JAVA_HOME/jre/sh:$JAVA_HOME/sh:$PATH
elif [[ "$OS_NAME" = "SunOS" || "$OS_NAME" = "Solaris" ]] ; then
  export PATH=$JAVA_HOME/bin:$PATH
elif [[ "$OS_NAME" = "HP-UX" ]]  ; then
  export PATH=$JAVA_HOME/bin:/usr/sbin:$PATH
elif [[ "$OS_NAME" = "Linux" ]]  ; then  
   #Make sure the link is in place.
   if [[ ! -L $WAS_DIR/java ]] ; then
      #If not, make it in place, JDK should be in two possible spots.
      if [[ -d $WAS_DIR/IBMJava2-122 ]] ; then
	ln -s /$WAS_DIR/IBMJava2-122/ $WAS_DIR/java
      elif [[ -d /opt/IBMJava2-122 ]] ; then
	ln -s /opt/IBMJava2-122/ $WAS_DIR/java
      else
	while :
	do
	{
        echo "Please enter your IBM-jdk directory."
	echo "Example: /opt/IBMJava2-122"
	  read JDK_DIR
	  if [[ ! -f $JDK_DIR/bin/java ]]
	  then
             echo "This does not appear to be a valid IBM-jdk."
             continue
	  fi
  
	  break
	}
	done
        ln -s $JDK_DIR $WAS_DIR/java
      fi
   fi 
  export PATH=$JAVA_HOME/bin:$PATH
fi

if [[ "$JDK_INSTALL" = "true" ]] ; then
  echo "Installing JDK PTF 4"
  JAVA_PTF_DIR=$WAS_DIR/java_ptf_4
  mkdir $JAVA_PTF_DIR

  if [[ "$OS_NAME" = "Linux" ]]  ; then
     cp -Rfd $JAVA_HOME/* $JAVA_PTF_DIR/
  else
     cp -R $JAVA_HOME/* $JAVA_PTF_DIR/
  fi

  java -jar $JAVA_PTF -TargetDir $JAVA_PTF_DIR -backupJar $WAS_DIR/$JAVA_BACKUP_JAR -log $WAS_DIR/logs/$JAVA_LOG_FILE -SkipVer
  if [[ "$OS_NAME" = "AIX" ]] ; then
      slibclean
  fi
  sleep 5
  rm -rf $JAVA_HOME/*

  if [[ "$OS_NAME" = "Linux" ]]  ; then
     cp -Rfd $JAVA_PTF_DIR/* $JAVA_HOME/
  else
     cp -R $JAVA_PTF_DIR/* $JAVA_HOME/
  fi

  rm -rf $JAVA_PTF_DIR
  if [[ "$OS_NAME" = "HP-UX" ]] ; then
     chmod -R 755 $JAVA_HOME/jre/lib/PA_RISC/*
     chmod -R 755 $JAVA_HOME/jre/lib/PA_RISC2.0/*
     chmod 755 $JAVA_HOME/jre/bin/.start_xfs
  elif [[ "$OS_NAME" = "SunOS" || "$OS_NAME" = "Solaris" ]] ; then
     chmod -R 755 $JAVA_HOME/jre/lib/sparc/*
  fi
fi

#Unzipping the IHS PTF
if [[ "$SILENT_INSTALL" = "false" ]] ; then
  if [[ "$IHS_INSTALL" = "true" ]] ; then
	while :
	do
	{
        echo "WARNING: If you install the IBM HTTP Server PTF, the back level of IHS may not work properly with the new level of GSkit."
	  echo "Please enter whether you want to install IHS WebServer PTF (y/n)"
	  read IHS_INSTALL
	  if [[ "${IHS_INSTALL}" = "" ]]
	  then
		continue
	  fi
  
	  if [[ "$IHS_INSTALL" != "y" && "$IHS_INSTALL" != "n" ]]
	  then
		 {
			echo
			echo "Please enter y for Yes or n for No"
			echo 
			continue
		 }
	  elif [[ "$IHS_INSTALL" = "y" ]] ; then
		IHS_INSTALL="true"
	  else
		IHS_INSTALL="false"
	  fi

	  break
	}
	done
  fi
fi

if [[ "$IHS_INSTALL" = "true" ]] ; then
   echo "Installing IHS PTF"
   java -jar $IHS_PTF -TargetDir $IHS_DIR -backupJar $WAS_DIR/$IHS_BACKUP_JAR -log $WAS_DIR/logs/$IHS_LOG_FILE -SkipVer
   echo "Installing the gskit package"
   if [[ "$OS_NAME" = "AIX" ]]  ; then
        echo "Installing new version of GSkit."
        installp -acgXd $IHS_DIR all
   elif [[ "$OS_NAME" = "SunOS" || "$OS_NAME" = "Solaris" ]] ; then
        echo "Installing new version of GSkit."
        pkgadd -a admin -d $IHS_DIR all
   elif [[ "$OS_NAME" = "HP-UX" ]] ; then
        echo "Uninstalling previous version of GSkit."
        /usr/sbin/swremove -x autoselect_dependents=false -x enforce_dependencies=false gsk4bas
        echo "Installing new version of GSkit."
        /usr/bin/chmod u+x $IHS_DIR/gsk4bas/catalog/gsk4bas/gsk4bas/postinstall
        /usr/bin/chmod u+x $IHS_DIR/gsk4bas/catalog/gsk4bas/gsk4bas/preremove
        /usr/sbin/swreg -l depot $IHS_DIR/gsk4bas
        /usr/sbin/swinstall -s $IHS_DIR/gsk4bas gsk4bas 
   fi  
fi

################################################################
# Addition by Red Hat, Inc.  David Sainty <dsainty@redhat.com> #
################################################################

export COMPAT_LIBDIR="\/usr\/s390-glibc21-linux\/lib"
echo "Adding RH patch to $JAVA_HOME/jre/bin/java"

mkdir $JAVA_HOME/jre/bin/rh-backup
cp -d $JAVA_HOME/jre/bin/{java,javaw,jvmtcf,keytool,policytool,rmid,rmiregistry} $JAVA_HOME/jre/bin/rh-backup/.
for loop in javaw jvmtcf keytool policytool rmid rmiregistry java
do
  sed "s/\(\$DEBUG_PROG\) \(\"\$prog\" \"\$\@\"\)/\1 $COMPAT_LIBDIR\/ld.so.1 --library-path $COMPAT_LIBDIR:\$LD_LIBRARY_PATH \2/" < $JAVA_HOME/jre/bin/${loop} > $JAVA_HOME/jre/bin/${loop}_new
  mv -f $JAVA_HOME/jre/bin/${loop}_new $JAVA_HOME/jre/bin/${loop}
  chmod 0755 $JAVA_HOME/jre/bin/${loop}
done

mkdir $JAVA_HOME/bin/rh-backup
cp -d $JAVA_HOME/bin/{appletviewer,extcheck,idlj,jar,jarsigner,javac,javadoc,javah,javap,jdb,native2ascii,rmic,serialver} $JAVA_HOME/bin/rh-backup/.
for loop in appletviewer extcheck idlj jar jarsigner javac javadoc javah javap jdb native2ascii rmic serialver
do
  sed "s/\(\$DEBUG_PROG\) \(\"\$prog\" \"\$\@\"\)/\1 $COMPAT_LIBDIR\/ld.so.1 --library-path $COMPAT_LIBDIR:\$LD_LIBRARY_PATH \2/" < $JAVA_HOME/bin/${loop} > $JAVA_HOME/bin/${loop}_new
  mv -f $JAVA_HOME/bin/${loop}_new $JAVA_HOME/bin/${loop}
  chmod 0755 $JAVA_HOME/bin/${loop}
done

echo ""

###################
# End of Addition #
###################

echo "Installing the WebSphere Application Server 3.5 PTF 4"

java -jar $DELTA_JAR -TargetDir $WAS_DIR -backupJar $WAS_DIR/$BACKUP_JAR -log $WAS_DIR/logs/$LOG_FILE


# Update the samples in the Webservers doc root
if [[ "$SILENT_INSTALL" = "false" ]] ; then
while :
do
{

     echo "In order to update the WebSphere Application Server Samples"
     echo "the currently configured WebServer's doc root must be specified" 
     echo "or press enter to skip upgrading the samples."
     echo "Eg. $EXAMPLE_DOC_ROOT"
     echo "Please enter your webserver's doc root path" 
     read DOC_ROOT
	if [[ "${DOC_ROOT}" = "" ]]
	then
	   break
	fi

	echo "Is this correct (y/n): $DOC_ROOT"
	read CONFIRM
	if [[ "$CONFIRM" != "y"  ]]
	then
		 {
			echo
			echo "Please enter y for Yes or n for No"
			echo 
			continue
		 }
	 fi
       if [[ ! -d $DOC_ROOT ]] ; then
          echo "No such directory: $DOC_ROOT"
          echo " " 
          continue 
       fi

	 break
}
done
else
   if [[ "$DOC_ROOT" != "" ]] ; then
     CONFIRM="y"
   fi 
fi

if [[ "$CONFIRM" = "y" ]] ; then
  if [[ "$OS_NAME" = "Linux" ]]  ; then
    cp -pRf $WAS_DIR/WSsamplesIDB $DOC_ROOT
    cp -pRf $WAS_DIR/WSsamples $DOC_ROOT
    cp -pRf $WAS_DIR/theme $DOC_ROOT
    chmod -R 755 $DOC_ROOT  
  else
    cp -pR $WAS_DIR/WSsamplesIDB $DOC_ROOT
    cp -pR $WAS_DIR/WSsamples $DOC_ROOT
    cp -pR $WAS_DIR/theme $DOC_ROOT
    chmod -R 755 $DOC_ROOT  
  fi
fi

#End install 
