#! /bin/sh

#################################################################################
#                                                                               #
# Calendar Server Uninstall Wizard Software Uninstallation                      #
#                                                                               #
# PROPRIETARY AND CONFIDENTIAL                                                  #
#                                                                               #
# Copyright  2003 Sun Microsystems, Inc. All rights reserved.                   #
#                                                                               #
# Use of this product is subject to license terms. Federal Acquisitions:        #
# Commercial Software Government Users Subject to Standard License Terms        #
# and Conditions. Sun, Sun Microsystems, the Sun logo, iPlanet, and             #
# Sun ONE  are trademarks or registered trademarks of Sun Microsystems, Inc.    #
# in the United States and other countries.                                     #
#                                                                               #
#################################################################################

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

MYNAME=`/bin/basename $0`
MYLOCATION=`/bin/echo $0 | /bin/sed "s%/${MYNAME}\$%%"`
PRODUCT='Sun[tm] ONE Calendar Server'
RELEASE='2004Q2'
THISHOST=`/bin/hostname`
REMOTEHOST=''
OSARCH=`/bin/uname -s`
RM='/bin/rm -f'
JAVA_HOME_DIR='';

# TOOLS_DIR - location of supporting tools, e.g. inst.sh, chk4patch.sh
# PKGS_DIR - top level location of packages
# PATCHES_DIR - location of patches
INSTALL_DIR=
TOOLS_DIR=${INSTALL_DIR}/Tools
PKGS_DIR=${INSTALL_DIR}/Packages
PATCHES_DIR=${BASEDIR}/Patches

#################################################################################
# If we are not in the directory where this script is located (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

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

if test "${OSARCH}" = "SunOS";
then
  JAVA_HOME_DIR='/usr/j2se'
  JAVA_VM=${JAVA_HOME_DIR}'/bin/java'
  JAVA_VM_CMD="${JAVA_VM}" 

  NODISPLAY=`echo "$*" | fgrep -s -c -e '-nodisplay'`
  JAVAVERSION=`echo "$*" | fgrep -s -c -e '-javaversion'`
  ${RM} /tmp/SolarisNativeToolkit
  REMOTEHOST=''
  if test "${NODISPLAY}" = "0";
  then
    /usr/bin/tty -s
    if test $? -eq 0;
    then
      REMOTEHOST=`/bin/who am i | /bin/awk '-F(' '{print substr($2, 1, length($2)-1)}' | /bin/sed 's/:.*//'`
    fi      
  fi
fi

if test "${OSARCH}" = "HP-UX";
then
  JAVA_HOME_DIR='java/jre'
  JAVA_VM=`pwd`${JAVA_HOME_DIR}'/bin/java'
  JAVA_VM_CMD="${JAVA_VM} -Xmx256M"
  JAVA_PLAT_DIR='PA_RISC'
  NODISPLAY=`echo "$*" | grep -c -e '-nodisplay'`
  JAVAVERSION=`echo "$*" | grep -c -e '-javaversion'`
  ${RM} /tmp/HpuxNativeToolkit
  REMOTEHOST=''
  if test "${NODISPLAY}" = "0";
  then
    /usr/bin/tty -s
    if test $? -eq 0;
    then
      REMOTEHOST=`/bin/who -m -R | /bin/awk '-F(' '{print substr($2, 1, length($2)-1)}' | /bin/sed 's/\..*//'`
    fi      
  fi
fi

if test "${OSARCH}" = "AIX";
then
  JAVA_HOME_DIR='java/jre'
  JAVA_VM=`pwd`${JAVA_HOME_DIR}'/sh/java'
  JAVA_VM_CMD="${JAVA_VM}"
  JAVA_PLAT_DIR='classic'
  NODISPLAY=`echo "$*" | grep -c -e '-nodisplay'`
  JAVAVERSION=`echo "$*" | grep -c -e '-javaversion'`
  ${RM} /tmp/AixNativeToolkit
  REMOTEHOST=''
  if test "${NODISPLAY}" = "0";
  then
    /usr/bin/tty -s
    if test $? -eq 0;
    then
      REMOTEHOST=`/bin/who -m | /bin/awk '-F(' '{print substr($2, 1, length($2)-1)}' | /bin/sed 's/\..*//'`
    fi      
  fi
fi

JAVA_PLAT_DIR_PATH="${JAVA_HOME_DIR}/bin/java/jre/bin/${JAVA_PLAT_DIR}"

if test "${OSARCH}" = "SunOS";
then
  JAVA_PLAT_DIR_PATH=${JAVA_HOME_DIR}
fi

#################################################################################
# if -nodisplay is specified, unset DISPLAY environment variable so no attempt
# is ever made to access a remote X display, and set REMOTEHOST to "" so it does
# nto look like we are logged in from a remote host
#################################################################################

if test "X${DISPLAY}" != "X" -a "${NODISPLAY}" != "0";
then
  REMOTEHOST=""
  unset DISPLAY
fi

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

echo ''
echo '=============================================================================='
echo ''
echo '               Sun[tm] ONE Calendar Server Software Uninstallation'
echo "                   For ${PRODUCT} Release ${RELEASE}"
echo ''
echo 'Copyright 2003 Sun Microsystems, Inc. All rights reserved.'
echo ''
echo 'Use of this product is subject to license terms. Federal Acquisitions:'
echo 'Commercial Software Government Users Subject to Standard License Terms'
echo 'and Conditions. Sun, Sun Microsystems, the Sun logo, iPlanet, and'
echo 'Sun ONE  are trademarks or registered trademarks of Sun Microsystems, Inc.'
echo 'in the United States and other countries.'
echo ''
echo '=============================================================================='
echo ''

#################################################################################
# Verify that sufficient 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 uninstallation components available'

#################################################################################
# Verify directory structure present
#################################################################################

echo 'Verifying directories available'

for DIR in ${JAVA_HOME_DIR} ${JAVA_HOME_DIR}/bin ${JAVA_PLAT_DIR_PATH}
do
  if test ! -d ${DIR};
  then
    echo "${MYNAME}: Error: required directory missing (${DIR}); cannot uninstall product."
    exit 1
  fi
done

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

echo 'Verifying files available'

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

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

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

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 uninstall product."
  exit 1
fi

#################################################################################
# See if XKEYSYMDB is set and if so see if the file does exists; if not, output
# an error message
#################################################################################

# Only test if -javaversion is not specified AND -nodisplay is not
# specified AND if XKEYSYMDB is specified

if test "${JAVAVERSION}" = "0" -a "${NODISPLAY}" = "0" -a "X${XKEYSYMDB}" != "X";
then
  if test ! -f ${XKEYSYMDB};
  then
    echo ''
    echo "${MYNAME}: warning: the XKEYSYMDB environment variable is set but the file"
    echo "the variable references does not exist: ${XKEYSYMDB}"
    echo 'The default keyboard mappings will be used when running this uninstallation program.'
    echo 'This will not have any effect on the products that are uninstalled.'
    echo ''
    unset XKEYSYMDB
  fi
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

# Only test if AND -nodisplay is not specified AND displaying to a
# remote host and the remote host is not this system

if test "${NODISPLAY}" = "0" -a "X${REMOTEHOST}" != "X" -a "X${REMOTEHOST}" != "X${THISHOST}";
then
  echo ''
  echo 'This product uninstallation program 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 uninstallation program 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 may receive 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, you must modify the X access control on the system'
  echo "where the X server is running (${REMOTEHOST}) to allow root on this"
  echo "system (${THISHOST}) to make connections to the X server."
  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 'use the appropriate command to do this.'
  echo ''
  echo "If host ${REMOTEHOST} is not a Unix system, you must do whatever is"
  echo 'necessary to allow user root on this system access to the X server'.
  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 "uninstallation program will be displayed on the system you are viewing this text on."
    echo "If this is not the correct value to display on the remote host, please exit this"
    echo "uninstallation program, set the DISPLAY environment variable to the correct value,"
    echo "and restart this uninstallation program."
    DISPLAY="${REMOTEHOST}:0.0"
    export DISPLAY
  fi
  echo ''
  echo 'Type <return> or <enter> to continue with the uninstallation: '
  read ANSWER
fi

#################################################################################
# If remote displaying warn user of potential problems
# -nodisplay is not specified
#################################################################################

if test "${NODISPLAY}" = "0";
then
  ITSREMOTE=0
  if test "X${REMOTEHOST}" != "X" -a "X${REMOTEHOST}" != "X${THISHOST}";
  then
    ITSREMOTE=1
  fi
  DISPLAYHOST=`echo "${DISPLAY}" | /bin/sed 's/:.*//'`
  if test "X${DISPLAY}" != "X" -a "X${DISPLAYHOST}" != "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 uninstallation program on host: ${THISHOST}"
    echo "You are remote displaying this program to host: ${DISPLAYHOST}"
    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 not from the same manufacturer'
    echo 'running the same operating system as the one you are running this uninstallation'
    echo 'program on, then this uninstallation program may have display problems and/or not'
    echo 'function properly.'
    echo ''
    echo 'If this happens, use the display/keyboard attached to the system you are running'
    echo "this uninstallation program on (${THISHOST}) and rerun this uninstallation program."
    echo ''
    echo 'If you are unable to use the display/keyboard attached to the system'
    echo 'you are running this uninstallation program on, you may run this uninstallation program'
    echo 'in text only (non graphical) mode now by typing exit in this uninstallation program'
    echo 'and rerunning it with this command:'
    echo ''
    echo "$0 -nodisplay"
    echo ''
    echo 'Type <return> or <enter> to continue with the uninstallation: '
    read ANSWER
  fi
fi

#################################################################################
# Setup environment for java uninstall wizard
#################################################################################

JAVA_HOME=${JAVA_HOME_DIR}; export JAVA_HOME
CLASSPATH="`pwd`/classes:."; export CLASSPATH
JAVA_COMPILER=NONE; export JAVA_COMPILER

#################################################################################
# Invoke uninstall Wizard in GUI mode first
#################################################################################

if test "${NODISPLAY}" = "0";
then
  echo 'Starting uninstall wizard in graphical mode'
else
  echo 'Starting uninstall wizard in Command Line Interactive (non-graphical) mode'
fi

${JAVA_VM_CMD} uninstall_Sun_ONE_Calendar_Server $*

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

case $? in
0)
  echo "The uninstall wizard has finished."
  exit 0
  ;;

13)
  echo ''
  echo '=============================================================================='
  if test "${NODISPLAY}" = "0";
  then
    echo "${MYNAME}: The uninstallation failed with possible display access problems."
    echo ''
    echo 'This may be due to this product uninstallation program not being able to'
    echo 'access the X window display on this system.'
    echo ''
    echo 'If you want to run this uninstallation program in graphical mode, it is'
    echo 'suggested that you login as user root on the system where the product'
    echo 'is to be uninstalled, and then run the uninstallation program without remote'
    echo 'displaying it to another system.'
    echo ''
    echo 'If you cannot login as root but must instead login as a user other than'
    echo 'root and then use the "su" command to obtain super user privileges, then before'
    echo 'you run this uninstallation program you must first make sure that this systems X'
    echo 'server permissions will allow access by user root from this system.'
    echo ''
    echo 'If you must remote display this uninstallation program to another system'
    echo '(either by setting the DISPLAY environment variable or by providing'
    echo 'the -display command line option), you must first make sure that the'
    echo 'remote systems X server permissions will allow access by user root from'
    echo 'the system on which this uninstallation program is run.'
    echo ''
    echo 'If this problem cannot be fixed, you may run this uninstallation program in '
    echo 'text only (non graphical) mode by typing this command:'
    echo ''
    echo "$0 -nodisplay"
  else
    echo "${MYNAME}: The uninstallation failed with errors."  
  fi
  echo ''
  echo '=============================================================================='
  exit 1
  ;;

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