#!/bin/sh

#
# @(#)uninstall wrapper
#
# Copyright 2003 Sun Microsystems, Inc. All rights reserved.
# 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
#
# This software is the confidential and proprietary information
# of Sun Microsystems, Inc. ("Confidential Information").  You
# shall not disclose such Confidential Information and shall use
# it only in accordance with the terms of the license agreement
# you entered into with Sun.
#

# Find the commands that live in different directories on different platforms
test -x /bin/ls		&& s1ls="/bin/ls"		|| s1ls="/usr/bin/ls"
test -x /bin/cp		&& s1cp="/bin/cp"		|| s1cp="/usr/bin/cp"
test -x /bin/rm		&& s1rm="/bin/rm"		|| s1rm="/usr/bin/rm"
test -x /bin/basename	&& s1basename="/bin/basename"	|| s1basename="/usr/bin/basename"
test -x /bin/dirname	&& s1dirname="/bin/dirname"	|| s1dirname="/usr/bin/dirname"
test -x /bin/expr	&& s1expr="/bin/expr"		|| s1expr="/usr/bin/expr"
test -x /bin/cut	&& s1cut="/bin/cut"		|| s1cut="/usr/bin/cut"
test -x /bin/uname	&& s1uname="/bin/uname"		|| s1uname="/usr/bin/uname"

# Detect the OS name.
OS=`$s1uname -s 2>/dev/null`
if [ -z "$OS" ]; then
  echo "Unable to detect current OS"
  echo "Please make sure that you have your path variable defined properly."
  exit 1
fi
if [ "$OS" = "HP-UX" ]; then
  lib_ext="sl"
else
  lib_ext="so"
fi
if [ -z "${lib_ext}" ]; then
  echo "Unable to determine the shared library extension."
  exit 1
fi

isRelativePath="`$s1dirname $0 | $s1cut -f 1 -d /`"
if [ "$isRelativePath" = "." -o "$isRelativePath" = ".." ]; then
	PRG="`pwd`/$0"
else
	PRG=$0
fi

progname=`$s1basename $PRG`

# Resolve symlinks, so that the pathname computations below find the
# directory structure they expect.
while [ -h "$PRG" ]; do
	# Get the target of the symlink.  N.B.:  We assume that neither the
	# link's value nor the pathname leading to it contains "-> ".
	ls=`$s1ls -ld "$PRG"`
	link=`$s1expr "$ls" : '^.*-> \(.*\)$'`
	# If the link is absolute, use it as is; otherwise, substitute it
	# into the leafname part of $PRG.
	if $s1expr "$link" : '^/' > /dev/null; then
		prg="$link"
	else
		prg="`$s1dirname $PRG`/$link"
	fi
	PRG=`type -p "$prg"` >/dev/null 2>&1
done

INSTALLHOME=`$s1dirname "$PRG"`

if [ "$1" = "-pkg" ]; then
  #Solaris pkg case
  shift
  libpath_prefix=/usr/lib/mps/secv1:/usr/lib/mps:/usr/lib/mps/sasl2
  $s1rm -rf ${INSTALLHOME}/https-*
  $s1rm -rf ${INSTALLHOME}/httpacl
  $s1rm -rf ${INSTALLHOME}/plugins
elif [ "$1" = "-rpm" ]; then
  #Linux rpm case
  shift
  libpath_prefix=/opt/sun/private/lib:/opt/sun/private/share/lib:/opt/sun/lib
  $s1rm -rf ${INSTALLHOME}/https-*
  $s1rm -rf ${INSTALLHOME}/httpacl
  $s1rm -rf ${INSTALLHOME}/plugins
  $s1rm -rf ${INSTALLHOME}/ns-icons
else
  #standalone case
  libpath_prefix=${INSTALLHOME}/setup/WebServer

  s1ws_uninst_libs="libsasl.${lib_ext} libnspr4.${lib_ext} libldap50.${lib_ext} libprldap50.${lib_ext} libplc4.${lib_ext} libplds4.${lib_ext} libicui18n.${lib_ext}.2 libicuuc.${lib_ext}.2 libicudata.${lib_ext}.2 libicui18n.${lib_ext} libicuuc.${lib_ext} libicudata.${lib_ext}"
  for each in $s1ws_uninst_libs
  do
  	if [ -f "${INSTALLHOME}/bin/https/lib/${each}" ]; then
        	$s1cp ${INSTALLHOME}/bin/https/lib/${each}  ${INSTALLHOME}/setup/WebServer
	fi
  done
fi

# Set LD_LIBRARY_PATH
libpath_end="${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
LD_LIBRARY_PATH="${libpath_prefix}${ldlibpath_end}"
export LD_LIBRARY_PATH

# Set SHLIB_PATH for HP-UX
libpath_end="${SHLIB_PATH:+:$SHLIB_PATH}"
SHLIB_PATH="${libpath_prefix}${ldlibpath_end}"
export SHLIB_PATH

# Set LIBPATH for AIX
libpath_end="${LIBPATH:+:$LIBPATH}"
LIBPATH="${libpath_prefix}${ldlibpath_end}"
export LIBPATH

echo $INSTALLHOME
prog="$INSTALLHOME/.${progname}"

retcode=1
# Run.
if [ -x "$prog" ]
then
    "$prog" "$@"
    retcode=$?
else
    echo >&2 "$0: `basename ${prog}` was not found in `$s1dirname ${prog}`"
    exit 1
fi

if [ $retcode -eq 0 ]; then
	$s1rm -rf ${INSTALLHOME}/setup
else
	for each in $s1ws_uninst_libs
	do
        $s1rm ${INSTALLHOME}/setup/WebServer/${each}
	done
fi

exit $retcode
