#!/bin/sh

#################################################################################
#                                                           
# Sun Java System Instant Messaging Initial Configuration   
#                                                           
# PROPRIETARY AND CONFIDENTIAL                              
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.  U.S. 
# Government Rights - Commercial software.  Government users are subject 
# to the Sun Microsystems, Inc. standard license agreement and applicable 
# provisions of the FAR and its supplements.  Use is subject to license 
# terms.  Sun,  Sun Microsystems,  the Sun logo,  Java,  Solaris and 
# Sun[tm] ONE are trademarks or registered trademarks of Sun Microsystems, 
# Inc. in the U.S. and other countries.  All SPARC trademarks are used 
# under license and are trademarks or registered trademarks of SPARC 
# International, Inc. in the U.S. and other countries.  UNIX is a 
# registered trademark in the U.S. and other countries, exclusively 
# licensed through X/Open Company, Ltd.
#
# Copyright 2004 Sun Microsystems, Inc. Tous droits 
# rservs.L'utilisation est soumise aux termes du contrat de licence.Sun, 
# Sun Microsystems,  le logo Sun,  Java,  Solaris et  Sun[tm] ONE sont 
# des marques de fabrique ou des marques dposes de Sun Microsystems, 
# Inc. aux Etats-Unis et dans d'autres pays.Toutes les marques SPARC sont 
# utilises sous licence et sont des marques de fabrique ou des marques 
# dposes de SPARC International, Inc. aux Etats-Unis et dans d'autres 
# pays.UNIX est une marque dpose aux Etats-Unis et dans d'autres pays et 
# licencie exlusivement par X/Open Company, Ltd.
#################################################################################

#################################################################################
# Initial setup
#################################################################################

ECHO='/bin/echo'
MYNAME=`/bin/basename $0`
MYLOCATION=`$ECHO $0 | /bin/sed "s%/${MYNAME}\$%%"`
THISHOST=`/bin/hostname`
OSARCH=`/bin/uname -s`
XHOST='xhost'
XACCESSENABLED='0'
RM='/bin/rm -f'
REMOTEHOST=''


#################################################################################
# If we are not in the installation directory (e.g. the user has invoked this
# script with a path name such as /tmp/stuff/setup and is not in that directory)
# then cd into that directory so the setup will still work
#################################################################################

if test -d "${MYLOCATION}";
then
  cd ${MYLOCATION}
fi

##############
# fix for bug id 4884130,make sure that the . is included in the PATH
PATH=$PATH:.
export PATH


#################################################################################
# Setup variables based on platform specific naming conventions
#################################################################################

if test "${OSARCH}" = "SunOS";
then
  REMOTEHOST=`/bin/who am i | /bin/awk '-F(' '{print substr($2, 1, length($2)-1)}' | /bin/sed 's/:.*//'`
  JAVA_PLAT_DIR="sparc"
  XHOST="/usr/openwin/bin/xhost"
  NODISPLAY=`$ECHO "$*" | fgrep -c -e '-nodisplay'`
  JAVAVERSION=`$ECHO "$*" | fgrep -c -e '-javaversion'`
  PE=/usr/ucb/printenv
  ${RM} /tmp/SolarisNativeToolkit
fi
if test "${OSARCH}" = "Linux";
then
  REMOTEHOST=`/usr/bin/who am i | /bin/awk '-F(' '{print substr($2, 1, length($2)-1)}' | /bin/sed 's/:.*//'`
  XHOST="/usr/bin/X11/xhost"
  NODISPLAY=`echo "$*" | fgrep -s -c -e '-nodisplay'`
  JAVAVERSION=`echo "$*" | fgrep -s -c -e '-javaversion'`
  PE=/usr/bin/printenv
  ${RM} /tmp/LinuxNativeToolkit
  if ! [ -a /usr/bin/cat ]; then
    ln -s /bin/cat /usr/bin/cat
  fi
fi
if test "${OSARCH}" = "HP-UX";
then
  REMOTEHOST=`/bin/who -m -R | /bin/awk '-F(' '{print substr($2, 1, length($2)-1)}' | /bin/sed 's/\..*//'`
  JAVA_PLAT_DIR="PA_RISC2.0"
  XHOST="/usr/bin/X11/xhost"
  NODISPLAY=`echo "$*" | grep -c -e '-nodisplay'`
  JAVAVERSION=`echo "$*" | grep -c -e '-javaversion'`
  ${RM} /tmp/HpuxNativeToolkit
fi
if test "${OSARCH}" = "IRIX";
then
  REMOTEHOST=`/bin/who -m -R | /bin/awk '-F(' '{print substr($2, 1, length($2)-1)}' | /bin/sed 's/\..*//'`
  JAVA_PLAT_DIR="sgi"
  XHOST="/usr/bin/X11/xhost"
  NODISPLAY=`echo "$*" | grep -c -e '-nodisplay'`
  JAVAVERSION=`echo "$*" | grep -c -e '-javaversion'`
  ${RM} /tmp/IrixNativeToolkit
fi

#################################################################################
#  Check java version: iPlanet Instant Messaging server requires 
#  java version 1.3.0+
#################################################################################
verifyJavaPkg() {

  if [ "$OSARCH" = "Linux" ]; then
    javaPkgname=j2re
  else
    javaPkgname=SUNWj3rt
  fi
  installJavaPackage=1
  javaCmd="/usr/j2se/bin/java"
  tmpFile="/tmp/javacmd.30"
  RM="/bin/rm -f"

  dryRun=$1

  jh=`${PE} JAVA_HOME`
  if [ "$jh" != "" ]
  then
    javaCmd=$jh/bin/java
  else
    if [ "$OSARCH" = "Linux" ]; then
      result=`rpm -q --all|egrep 'jdk|jre|j2se|j2re|j2sdk'|head -1`
      rc=$?
      if [ X$result = X"" ]; then
        rc=1
      fi
    else
      # Check for the java pkg SUNWj3rt first
      result=`pkginfo $javaPkgname 2>/dev/null 2>&1`
      rc=$?
    fi
  
    if test $rc -eq 0 ; then
      if [ "$OSARCH" = "Linux" ]; then
        javaCmd=`rpm -q -l $result|grep 'bin.java$'|head -1`
      else
        basedir=`pkgparam -v $javaPkgname | grep BASEDIR= | cut -f2 -d = | sed -e s/\'//g`
        javaCmd=$basedir/j2se/bin/java
      fi
    else
      if test ! -f "$javaCmd" ;
      then
        javaCmd=`/usr/bin/which java`
      else
        javaCmd=/usr/j2se/bin/java
      fi
    fi
    if test $dryRun = "sysinfo";
    then
  
      if test ! -f "$javaCmd" ;
      then
        echo "Cannot find java in the system."
        exit 4
      fi
      return 0
    fi
  fi

  if test ! -f "$javaCmd" ;
  then
    installJavaPackage=1
  else
    rc=`$javaCmd -version 2>/dev/null 2>&1`
    if test $? -ne 0 ;
    then
      echo "Cannot run java ($javaCmd). Please fix this problem and run setup again"
      echo
      exit 3
    fi

    javaFullVersion=`($javaCmd -version 2>&1) | grep version | cut -d\" -f2`
    version=`($javaCmd -version 2>&1) | grep version | cut -d\" -f2 | cut -d\. -f1-2`
    majorversion=`($javaCmd -version 2>&1) | grep version | cut -d\" -f2 | cut -d\. -f1`
    minorversion=`($javaCmd -version 2>&1) | grep version | cut -d\" -f2 | cut -d\. -f2`

    if  test $majorversion -gt 0 ;
    then
      if test $minorversion -gt 2 ;
      then
        installJavaPackage=0
      else
        installJavaPackage=1
      fi
    else
      installJavaPackage=1
    fi
  fi

  ${RM} $tmpFile

  if test $installJavaPackage -eq 0 ;
  then
    echo "Found java ($javaCmd) version ($javaFullVersion) in the system."
    echo $javaCmd > $tmpFile
    IIM_JAVACMD=$javaCmd
    export IIM_JAVACMD
    return 0
  else
    echo Cannot locate Java, please install and run again
    exit 1
  fi

  return 0
}

verifyNsprPkg(){

  nsprPkgName=SUNWpr
  desc="Netscape Portable Runtime"

  result=`pkginfo $nsprPkgName 2>/dev/null 2>&1`
  rc=$?


  if test $rc -eq 0 ;
  then
       nsprVersion=`pkgparam -v $nsprPkgName | grep SUNW_PRODVERS= | cut -f2 -d = | sed -e s/\'//g`
      echo "The $desc $nsprPkgName $nsprVersion package is found on the system"
  else 
      echo "Missing package $nsprPkgName, please install and try again."
      exit 1
  fi
}

verifyNssPkg(){
  nssPkgName=SUNWtls
  desc="Network Security Services"
  result=`pkginfo $nssPkgName 2>/dev/null 2>&1`
  rc=$?


  if test $rc -eq 0 ;
  then
       nssVersion=`pkgparam -v $nssPkgName | grep SUNW_PRODVERS= | cut -f2 -d = | sed -e s/\'//g`
      echo "The $desc $nssPkgName $nssVersion package is found on the system"
  else 
      echo "Cannot find $desc $nssPkgName solaris package" 
      echo "Please install and try again"
      exit 1
  fi

}

verifyJssPkg(){
  jssPkgName=SUNWjss
  desc="Network Security Services for Java (JSS)"
  result=`pkginfo $jssPkgName 2>/dev/null 2>&1`
  rc=$?


  if test $rc -eq 0 ;
  then
       jssVersion=`pkgparam -v $jssPkgName | grep SUNW_PRODVERS= | cut -f2 -d = | sed -e s/\'//g`
      echo "The $desc $jssPkgName $jssVersion package is found on the system"
  else 
      echo "Cannot find $desc $jssPkgName solaris package" 
      echo "Please install and try again."
      exit 1
  fi

}

#################################################################################
# Welcome/License Text
#################################################################################

echo 
echo '========================================================================='
echo 
echo "               Sun Java System Instant Messaging"
echo 
cat copyright
echo 
echo '========================================================================='
echo 


#################################################################################
# Verify we have adequate permissions to do the install (usually requires root
# privileges)
#################################################################################

if test "${JAVAVERSION}" = "0";
then

  echo 'Verifying permissions'

  iam=`/usr/bin/id|/usr/bin/awk '{print $1;}'`
  if [ "${iam}" != "uid=0(root)" ];
  then
    echo "${MYNAME}: Error: this command must be run as root; cannot configure product."
    exit 1
  fi

fi

if test "${JAVAVERSION}" != "0";
then
  echo 'Verifying java available'
  verifyJavaPkg sysinfo
  echo "Found $javaCmd in the system."
  $javaCmd -fullversion
  $javaCmd -version
  exit 0
fi

#################################################################################
# Verify that sufficient installation components are available to provide
# confidence that a full release of the product is available in the
# current directory in which this script is being run
#################################################################################

echo 'Verifying java available'

verifyJavaPkg installpkg

if test "${OSARCH}" != "Linux";
then

  $ECHO '\nVerifying Netscape Portable Runtime Interface available'
  verifyNsprPkg 

  $ECHO '\nVerifying Network Security Services available'
  verifyNssPkg 

  $ECHO '\nVerifying Network Security Services for Java available'
  verifyJssPkg

fi

$ECHO '\nVerifying installation components available'

#################################################################################
# Verify minimum required files present
#################################################################################

echo 'Verifying files available'

for FILE in ./config.class ./License.txt $javaCmd
do
  if test ! -f ${FILE};
  then
    echo "${MYNAME}: Error: required file missing (${FILE}); cannot configure product."
    exit 1
  fi
done

if test ! -x $javaCmd;
then
  echo "${MYNAME}: Error: $javaCmd is not executable; cannot configure product."
  exit 1
fi

#################################################################################
# Attempt to allow root to access X display
#################################################################################

# if the remotehost is "" then there is no remote host
# if a remote host is present and it is not this systems name
# warn the user


if test "${JAVAVERSION}" = "0" -a "${NODISPLAY}" = "0" -a "X${REMOTEHOST}" != "X" -a "X${REMOTEHOST}" != "X${THISHOST}";
then
  echo ''
  echo "${MYNAME}: warning: X server client access control must be disabled."
  echo ''
  echo 'This product installer requires access to the X windows display *on the'
  echo 'system running the X window server* that you are reading this text on.'
  echo ''
  echo "You are running this installer on host: ${THISHOST}"
  echo "You are remotely logged in to this system from host: ${REMOTEHOST}"
  echo ''
  echo 'It looks like you are remotely logged into this system (for example,'
  echo 'via telnet, rlogin, etc.)'
  echo ''
  echo 'If you are not logged in as "root", you will receive one of'
  echo 'the following errors:' 
  echo ''
  echo ' "connection refused by server"'
  echo ' "client is not authorized to connect"'
  echo ' "cannot access display"' 
  echo ''
  echo 'If this happens, type one of the following commands,'
  echo 'depending on where the "xhost" program is located on your system,'
  echo 'to disable the X server access control on host ${REMOTEHOST}."'
  echo ''
  echo "If host ${REMOTEHOST} is a Unix system, *as the user who logged into"
  echo 'the X window system* (e.g. not root via su command) you need to'
  echo 'disable the X windows access control by typing one of these commands:'
  echo ''
  if test -x ${XHOST};
  then
    echo "   ${XHOST} +"
  fi
  echo ''
  echo "If host ${REMOTEHOST} is not a Unix system, you must do whatever is"
  echo 'necessary to disable the X window access control mechanism on that'
  echo 'system.'
  if test "${DISPLAY}" = "";
  then
    echo ''
    echo "${MYNAME}: warning: The DISPLAY environment variable is not set."
    echo ''
    echo "Setting DISPLAY environment variable to ${REMOTEHOST}:0.0 so that the"
    echo "installation program will be displayed on the system you are viewing this text on."
    DISPLAY="${REMOTEHOST}:0.0"
    export DISPLAY
  fi
  echo ''
  # echo 'Type <return> or <enter> to continue with the installation: '
  # read ANSWER
fi

# Detect whether or not the x access control mechanism is disabled or enabled

if test "${OSARCH}" = "SunOS";
then
  XACCESSENABLED=`${XHOST} | fgrep -s -c -e 'access control disabled, clients can connect from any host'`
fi

if test "${OSARCH}" = "HP-UX";
then
  XACCESSENABLED=`${XHOST} | grep -c -e 'access control disabled, clients can connect from any host'`
fi
if test "${OSARCH}" = "IRIX";
then
  XACCESSENABLED=`${XHOST} | grep -c -e 'access control disabled, clients can connect from any host'`
fi

# if the x access control mechanism is enabled (that is, access is restricted) then attempt
# to disable it

if test "${JAVAVERSION}" = "0" -a "${NODISPLAY}" = "0" -a "${XACCESSENABLED}" = "0" -a -x ${XHOST};
then
  /bin/su $USER -c "${XHOST} +" 2>/dev/null 1>&2
  if test $? -ne 0;
  then
    echo ''
    echo "${MYNAME}: warning: cannot disable X server client access control."
    echo ''
    echo 'If a "connection refused by server" or "Client is not'
    echo 'authorized to connect" error occurs, as the user who logged'
    echo 'into the X window system, type one of these commands:'
    echo ''
    echo '  xhost +'
    echo "  ${XHOST} +"
    echo ''
    echo 'Type <return> or <enter> to continue with the installation: '
    read ANSWER
  fi
fi

#################################################################################
# If remote displaying on HP-UX warn user of potential problems
#################################################################################

if test "${JAVAVERSION}" = "0" -a "${OSARCH}" = "HP-UX" -a "${NODISPLAY}" = "0";
then
  ITSREMOTE=0
  if test -a "X${REMOTEHOST}" != "X" -a "X${REMOTEHOST}" != "X${THISHOST}";
  then
    ITSREMOTE=1
  fi
  if test "X${DISPLAY}" != "X"; 
  then
    ITSREMOTE=1
  fi

  if test "X${ITSREMOTE}" != "X0";
  then
    echo ''
    echo "${MYNAME}: warning: remote displaying to another system"
    echo ''
    echo "You are running this installer on host: ${THISHOST}"
    if test "X${REMOTEHOST}" != "X";
    then
      echo "You are remotely logged in to this system from host: ${REMOTEHOST}"
    fi
    echo ''
    echo 'It looks like you are remotely logged into this system (for example,'
    echo 'via telnet, rlogin, etc.)'
    echo ''
    echo 'If the system you are reading this on is a non-HPUX system, this installer'
    echo 'may have display problems and/or not function properly.'
    echo ''
    echo 'If this happens, use the display/keyboard attached to the system you are'
    echo "running this installer on (${THISHOST}) and rerun this installer."
    echo ''
    echo 'If you are unable to use the display/keyboard attached to the system'
    echo 'you are running this installer on, you may run this product installer'
    echo 'in text only (non graphical) mode now by typing this command:'
    echo ''
    echo "$0 -nodisplay"
    echo ''
    echo 'Type <return> or <enter> to continue with the installation: '
    read ANSWER
  fi
fi

#################################################################################
# Setup environment for java install wizard
#################################################################################

JAVA_HOME=$javaCmd; export JAVA_HOME
CLASSPATH="."; export CLASSPATH
JAVA_COMPILER=NONE; export JAVA_COMPILER

#################################################################################
# Invoke Install Wizard
#################################################################################

if test "${NODISPLAY}" = "0";
then
  echo 'Starting install wizard in graphical mode'
else
  echo 'Starting install wizard in Command Line Interactive (non-graphical) mode'
fi
$javaCmd -Djava.library.path=lib config $*

#################################################################################
# Check the error return codes from the installer:
#  0 - success
#  1 - could not initialize window system
#################################################################################

case $? in
0|73)

  echo "The configuration completed."
  exit 0
  ;;

1)
  echo ''
  echo '=============================================================================='
  if test "${NODISPLAY}" = "0";
  then
    echo "${MYNAME}: The configuration failed with possible display access problems."
    echo ''
    echo 'This may be due to this product installer not being able to access the'
    echo 'X window display on this system. If this problem cannot be fixed, you'
    echo 'may run this product installer in text only (non graphical) mode by'
    echo 'typing this command:'
    echo ''
    echo "$0 -nodisplay"
  else
    echo "${MYNAME}: The configuration failed with errors."  
  fi
  echo ''
  echo '=============================================================================='
  exit 1
  ;;

*)
  echo "${MYNAME}: The configuration failed with errors."
  exit 1
  ;;
esac
