#!/bin/ksh
# SIMS remove script
# @(#)uninstall	1.74	00/01/10 SMI
#

##trap 'finally' 0 1 2 3 4 6 7 8 9 10  12 15       

TR="/bin/tr"
LS="/usr/bin/ls"
SED="/usr/bin/sed"
SORT="/usr/bin/sort"
RMDIR="/usr/bin/rmdir"
PATCHRM="/usr/sbin/patchrm"
BASENAME="/usr/bin/basename"

binName=`basename $0`

_adminFile=/tmp/sims_remove_admin

ARCH=`LC_ALL=C; uname -p`
gettext=/usr/bin/gettext
if [ `/bin/pkginfo SUNWimcom 2>/dev/null | grep -ci SUNWimcom` != "0" ]; then
  BASEDIR=`pkginfo -r SUNWimcom`
  if [ "${BASEDIR}" = "/" ]; then
    BASEDIR=""
  fi
else
  BASEDIR=""
fi

TEXTDOMAIN=init
TEXTDOMAINDIR=${BASEDIR}/opt/SUNWmail/admin/lib/locale
export TEXTDOMAIN
export TEXTDOMAINDIR

# i18n
if [ ! -z "$LC_MESSAGES" ]; then
  LANG=$LC_MESSAGES
fi

if [ -z "$LANG" ]; then
  LANG="C"
fi

if [ ! -d ${TEXTDOMAINDIR}/${LANG} ]; then

  # Strip off the dialect component

  TMPLANG=`echo $LANG | cut -f1 -d.`
  if [ ! -d ${TEXTDOMAINDIR}/${TMPLANG} ]; then

    # Strip off the regional component

    TMPLANG=`echo $TMPLANG | cut -f1 -d_`
    if [ ! -d ${TEXTDOMAINDIR}/${TMPLANG} ]; then

      # Language not supported, default to C

      TMPLANG="C"
    fi
  fi
  LANG=$TMPLANG
fi

export LANG

# major, minor version of Solaris
_OSREV=`/bin/uname -r`
_OSREV=${_OSREV:-5.0}
_OSMAJOR=`echo $_OSREV | cut -f1 -d.`
_OSMAJOR=${_OSMAJOR:-4}
_OSMINOR=`echo $_OSREV | cut -f2 -d.`
_OSMINOR=${_OSMINOR:-0}
_OSMINORMINOR=`echo $_OSREV | cut -f3 -d.`
_OSMINORMINOR=${_OSMINORMINOR:-0}

TAB='\t'
PIPE='|'
OPEN='('
CLOSE=')'
DOLLAR='$'
LBRACKET='<'
RBRACKET='>'
NEWLINE='\n'
BACKQUOTE='`'
AMPERSAND='&'
DOUBLEQUOTE='"'
FORWARDQUOTE="'"

initially()
{
  ## Preserve user's environment

  echo 'Preserving httpd.conf to /var/etc/http'

  if [ -f /etc/http/httpd.conf ]; then
    /usr/bin/mkdir -p /var/etc/http
    chmod 777 /var/etc /var/etc/http
    /usr/bin/cp  -r /etc/http/* /var/etc/http/
  fi
}

stopLit()
{

  if [ -f /etc/init.d/lic_mgr ]; then
    (/etc/init.d/lic_mgr stop << EOF
y
EOF
)
  fi

}


removePatchesToCorePackages()
{
  echo `$gettext "Removing Sun Internet Mail Server patches......"`
  echo ""
  ARCH=`uname -p`
  if [ "$ARCH" = "sparc" ]; then
    _plist="$SPARC_SIMS_PATCH ${_sparcWAPatch}"
  else
    _plist="$I386_SIMS_PATCH ${_i386WAPatch}"
  fi

  startdir=`pwd`
  cd /var/sadm/patch
  for patch in $_plist
  do
    if [ -d /var/sadm/patch/${patch}* ]; then
      patchdir=`${LS} -d ${patch}*`
      for patch in ${patchdir}
      do
        p_name=`${BASENAME} $patch`
        p_base=`echo ${p_name} | ${SED} -e 's/-..//'`
        p_lst=`${LS} -d ${p_base}* | ${SORT} -r`
        for p_name in ${p_lst}
        do
          eval echo `$gettext 'Removing patch ${FORWARDQUOTE}${p_name}${FORWARDQUOTE}'`
          if [ ${_OSMINOR} -ge 6 ]; then 
            ${PATCHRM} ${p_name}
          elif [ -f ${p_name}/backoutpatch ]; then
            ${p_name}/backoutpatch ${p_name}
          else
            ${RMDIR} ${p_name} 2> /dev/null
          fi
        done
      done
    fi
  done
  cd ${startdir}
}

isPkgListInstalled ()
{
  for _pkg in ${PkgList}; do
    /bin/pkginfo -q ${_pkg}
    if [ "$?" = "0" ]; then
      # Package is installed

      PkgList=
      return 1
    fi
  done
  PkgList=
  return 0
}

removePkgList ()
{
  revertedPkgs=
  for pkg in ${PkgList}; do
    revertedPkgs="$pkg $revertedPkgs"
  done

  PkgList=

  for pkg in ${revertedPkgs}; do
    if [ `/bin/pkginfo $pkg 2>/dev/null | grep -ci $pkg` != "0" ]; then
      eval echo `$gettext 'Removing $pkg...'`
      echo " "
      /usr/sbin/pkgrm -n -a $_adminFile $pkg

      # If for some reason we failed to remove, then try again

      if [ `/bin/pkginfo $pkg 2>/dev/null | grep -ci $pkg` != "0" ]; then
        /usr/sbin/pkgrm -n -a $_adminFile $pkg
      fi
      echo " "
    fi
  done
}

removeAllPackages()
{
  if [ ! -f $_adminFile ]; then
    echo "mail=
instance=nocheck
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nocheck
action=nocheck
basedir=default" > $_adminFile
  fi

  # Ask for SWS

  removeWebPackages=n
  if [ -f /etc/init.d/httpd ]; then
    textmsg=`$gettext "Remove Sun WebServer(tm) (n): "`
    removeWebPackages=`/bin/ckyorn -Q -d n -p "$textmsg" `
    (LC_ALL=C;export LC_ALL;removeWebPackages=`echo ${removeWebPackages} | ${TR} '[:upper:]' '[:lower:]' | cut -c1`)
  fi

  # Ask for JVM

  removeJVMPackages=n
  PkgList=$COREJAVAPACKAGES
  isPkgListInstalled
  if [ "$?" = "1" ]; then
    textmsg=`$gettext "Remove Java Virtual Machine (n): "`
    removeJVMPackages=`/bin/ckyorn -Q -d n -p "$textmsg" `
    (LC_ALL=C;export LC_ALL;removeJVMPackages=`echo ${removeJVMPackages} | ${TR} '[:upper:]' '[:lower:]' | cut -c1`)
  fi

  # Ask for HJ

  removeHJPackages=n
  PkgList=${HOTJAVAPACKAGES}
  isPkgListInstalled
  if [ "$?" = "1" ]; then
    textmsg=`$gettext "Remove HotJava(tm) (n): "`
    removeHJPackages=`/bin/ckyorn -Q -d n -p "$textmsg"`
    (LC_ALL=C;export LC_ALL;removeHJPackages=`echo ${removeHJPackages} | ${TR} '[:upper:]' '[:lower:]' | cut -c1`)
  fi

  # Ask for SDS

  removeSDSPackages=n
  PkgList=${DIRPACKAGES}
  isPkgListInstalled
  if [ "$?" = "1" ]; then
    textmsg=`$gettext "Remove Sun Directory Service Packages (n): "`
    removeSDSPackages=`/bin/ckyorn -Q -d n -p "$textmsg"`
    (LC_ALL=C;export LC_ALL;removeSDSPackages=`echo ${removeSDSPackages} | ${TR} '[:upper:]' '[:lower:]' | cut -c1`)
  fi

  removeSDSdatabase=n
  if [ "$removeSDSPackages" = "y" ]; then

    # Add any SunDS patches

    SPARC_SIMS_PATCH="$_sparcSunDsPatch $SPARC_SIMS_PATCH"
    I386_SIMS_PATCH="$_i386SunDsPatch $I386_SIMS_PATCH"
    
    textmsg=`$gettext "Remove Sun Directory Service Database (n): "`
    removeSDSdatabase=`/bin/ckyorn -Q -d n -p "$textmsg"`
    (LC_ALL=C;export LC_ALL;removeSDSdatabase=`echo ${removeSDSdatabase} | ${TR} '[:upper:]' '[:lower:]' | cut -c1`)
  else
    removeSDSdatabase=n
  fi
 
  if [ $DRAMATIC_REMOVE -eq 0 ]; then
    echo ""
    echo `$gettext "NOTE: The next option will remove all configuration information, including configuration files saved to permit upgrade.  It will also remove the message store database.  Please note that choosing yes to the next question restores your system to the state before you installed SIMS"`

    textmsg=`$gettext "Remove the message store database (n) : "`
    removeMSdatabase=`/bin/ckyorn -Q -d n -p "$textmsg"`
    (LC_ALL=C;export LC_ALL;removeMSdatabase=`echo ${removeMSdatabase} | ${TR} '[:upper:]' '[:lower:]' | cut -c1`)
  else
    removeMSdatabase=y
  fi

  textmsg=`$gettext "Are you sure you want to remove Sun Internet Mail Server(tm) (n): "`
  areYouSure=`/bin/ckyorn -d n -p "$textmsg"`
  (LC_ALL=C;export LC_ALL;areYouSure=`echo ${areYouSure} | ${TR} '[:upper:]' '[:lower:]' | cut -c1`)

  if [ "$areYouSure" != "y" ]; then
    echo `$gettext "Aborting uninstaller..."`
    return
  fi

  # First remove the patches to the SIMS packages

  removePatchesToCorePackages

  if [ "$removeSDSdatabase" = "y" ]; then
    # Determine the directory where slapd.conf will be preserved by SUNWsds's 
    # package remove
    _slapdBackupDir=
    /bin/pkginfo -q SUNWsds
    if [ "$?" = "0" ]; then
      _slapdBackupDir=`/bin/pkgparam SUNWsds BACKUPDIR`
    fi
  fi

  # If this is a HA system then unregister SIMS from the HA framework

  HA_SERVICE_NAME="Sun_Internet_Mail"
  HAREG="/opt/SUNWhadf/bin/hareg"
  if [ `/bin/pkginfo SUNWimha 2>/dev/null | grep -ci SUNWimha` != "0" ]; then
    if [ "`$HAREG | /usr/bin/grep -c $HA_SERVICE_NAME`" != "0" ]; then
      echo `$gettext "Unregistering SIMS HA..."`
      if [ "`$HAREG | /usr/bin/grep $HA_SERVICE_NAME | /usr/bin/awk '{print $2}'`" = "on" ]; then
        $HAREG -n $HA_SERVICE_NAME
      fi
      $HAREG -u $HA_SERVICE_NAME
    fi
  fi
     
  # and then remove the selected packages

  if [ "$removeSDSPackages" = "n" ]; then
    # Do not remove the Schema package
    PkgList=`echo ${ALLPACKAGES} | /usr/bin/sed -e 's/SUNWimsch//g' 2> /dev/null`
  else
    PkgList=${ALLPACKAGES}
  fi

  # Add any other existing Doc packages
  docPackages=
  alldocPackages=`/bin/pkginfo | /usr/bin/grep SUNWimol | /usr/bin/awk '{print $2}'`
  for _pkg in $alldocPackages; do
    # Look for MAILUSER in the pkginfo 
    if [ "`/bin/pkgparam $_pkg MAILUSER`" != "" ]; then
      # This is a SIMS doc package; add it to the list
      docPackages="${docPackages} $_pkg"
    fi
  done
  PkgList="${docPackages} ${PkgList}"

  removePkgList

  if [ "$removeSDSPackages" = "y" ]; then
    PkgList=${DIRPACKAGES}
    removePkgList

    if [ "$removeSDSdatabase" = "y" ]; then
      # now change the permission of slapd.conf as read only for everybody
      # so that the web install CGI scripts can read the file since they are
      # not run as root
      if [ -f $_slapdBackupDir/slapd.conf ]; then
        /bin/chmod ugo+r $_slapdBackupDir/slapd.conf
      fi
  
      echo `$gettext "Removing directory database..."`
      _dbToRemove="/var/opt/SUNWconn/ldap"
      for _db in $_dbToRemove; do
        if [ -d ${BASEDIR}$_db ]; then
          /bin/rm -rf ${BASEDIR}$_db
          if [ -f ${BASEDIR}$_db ]; then
            /usr/sbin/unlink  ${BASEDIR}$_db
          fi
        fi
      done
    fi
  fi

  if [ "$removeMSdatabase" = "y" ]; then
    # Remove MS databases  
    echo `$gettext "Removing message store database..."`
    _dbToRemove="/var/opt/SUNWmail/ims"
    for _db in $_dbToRemove; do
      if [ -d ${BASEDIR}$_db ]; then
        /bin/rm -rf ${BASEDIR}$_db
        if [ -f ${BASEDIR}$_db ]; then
          /usr/sbin/unlink  ${BASEDIR}$_db
        fi
      fi
    done
  fi

  # now cleanup the directories that are not cleaned up by pkgrm  
  echo `$gettext "Cleaning up temporary queues and temporary files..."`
  _dirToClean="/etc/opt/SUNWmail/imta /opt/SUNWmail /etc/opt/SUNWmail/admin /opt/SUNWwa"
  _filesToClean="/etc/opt/SUNWmail/slapd.ldif /etc/opt/SUNWmail/console.prf /etc/opt/SUNWmail/sims_setup.dat"

  for _files in $_filesToClean; do
    if [ -f ${BASEDIR}$_files ]; then
      /bin/rm -f ${BASEDIR}$_files
      if [ -f ${BASEDIR}$_files ]; then
        /usr/sbin/unlink ${BASEDIR}$_files
      fi
    fi
  done

  for _dir in $_dirToClean; do
    if [ -d ${BASEDIR}$_dir ]; then
      /bin/rm -rf ${BASEDIR}$_dir
      if [ -f ${BASEDIR}$_dir ]; then
        /usr/sbin/unlink ${BASEDIR}$_dir
      fi
    fi
  done

  if [ "$removeSDSPackages" = "y" ]; then
    if [ -d ${BASEDIR}/opt/SUNWconn ]; then
      if [ -z "`LC_ALL=C; ls -af1 ${BASEDIR}/opt/SUNWconn | tail +3`" ]; then
        if [ -f $HAGET -a "$HA_MASTER" = "0" -a "$HA_INSTALL" = "1" ]; then
          if [ -L ${BASEDIR}/opt/SUNWconn ]; then
            /usr/sbin/unlink ${BASEDIR}/opt/SUNWconn
          fi
        else
          /bin/rmdir ${BASEDIR}/opt/SUNWconn/../SUNWconn
          if [ -L ${BASEDIR}/opt/SUNWconn ]; then
            /usr/sbin/unlink ${BASEDIR}/opt/SUNWconn
          fi
        fi
        eval echo `$gettext 'Removing directory ${FORWARDQUOTE}${BASEDIR}/opt/SUNWconn${FORWARDQUOTE}'`
      fi
    fi
  
    if [ -d ${BASEDIR}/var/opt/SUNWconn ]; then
      if [ -z "`LC_ALL=C; ls -af1 ${BASEDIR}/var/opt/SUNWconn | tail +3`" ]; then
        if [ -f $HAGET -a "$HA_MASTER" = "0" -a "$HA_INSTALL" = "1" ]; then
          /usr/sbin/unlink ${BASEDIR}/var/opt/SUNWconn
        else
          /bin/rmdir ${BASEDIR}/var/opt/SUNWconn/../SUNWconn
          /usr/sbin/unlink ${BASEDIR}/var/opt/SUNWconn
        fi
        eval echo `$gettext 'Removing directory ${FORWARDQUOTE}${BASEDIR}/var/opt/SUNWconn${FORWARDQUOTE}'`
      fi
    fi

    if [ -d ${BASEDIR}/etc/opt/SUNWconn ]; then
      if [ -z "`LC_ALL=C; ls -af1 ${BASEDIR}/etc/opt/SUNWconn | tail +3`" ]; then
        if [ -f $HAGET -a "$HA_MASTER" = "0" -a "$HA_INSTALL" = "1" ]; then
          /usr/sbin/unlink ${BASEDIR}/etc/opt/SUNWconn
        else
          /bin/rmdir ${BASEDIR}/etc/opt/SUNWconn/../SUNWconn
          /usr/sbin/unlink ${BASEDIR}/etc/opt/SUNWconn
        fi
        eval echo `$gettext 'Removing directory ${FORWARDQUOTE}${BASEDIR}/etc/opt/SUNWconn${FORWARDQUOTE}'`
      fi
    fi
  fi

  # done cleaning up all temporary file and directories left around

  if [ "$removeWebPackages" = "y" ]; then
    /usr/bin/test -L ${BASEDIR}/var/http/demo/public/sims
    if [ $? = 0 ]; then
      $gettext "Removing link '/var/http/demo/public/sims'"
      rm -f ${BASEDIR}/var/http/demo/public/sims
      if [ -f ${BASEDIR}/var/http/demo/public/sims ]; then
        /usr/sbin/unlink ${BASEDIR}/var/http/demo/public/sims
      fi
    fi
    /usr/bin/test -L ${BASEDIR}/var/http/demo/cgi-bin/sims
    if [ $? = 0 ]; then
      eval echo `$gettext 'Removing link ${FORWARDQUOTE}${BASEDIR}/var/http/demo/cgi-bin/sims${FORWARDQUOTE}'`
      rm -f ${BASEDIR}/var/http/demo/cgi-bin/sims
      if [ -f ${BASEDIR}/var/http/demo/cgi-bin/sims ]; then
        /usr/sbin/unlink ${BASEDIR}/var/http/demo/cgi-bin/sims
      fi
    fi

    if [ -f /etc/init.d/httpd ]; then
      echo `$gettext "Stopping Sun WebServer..."`
      /etc/init.d/httpd stop
    fi

    PkgList=${WEB_PACKAGES}
    removePkgList

    if [ -d /etc/http.bak ]; then
      rm -rf /etc/http.bak
      if [ -f /etc/http.bak ]; then
        /usr/sbin/unlink  /etc/http.bak
      fi
    fi

    if [ -d ${BASEDIR}/var/http/demo/cgi-bin ]; then
      $gettext "Removing directory '/var/http/demo/cgi-bin'"
      rm -rf ${BASEDIR}/var/http/demo/cgi-bin
      if [ -f ${BASEDIR}/var/http/demo/cgi-bin ]; then
        /usr/sbin/unlink ${BASEDIR}/var/http/demo/cgi-bin
      fi
    fi

    if [ -d ${BASEDIR}/var/http/demo/public ]; then
      $gettext "Removing directory '/var/http/demo/public'"
      rm -rf ${BASEDIR}/var/http/demo/public
      if [ -f ${BASEDIR}/var/http/demo/public ]; then
        /usr/sbin/unlink ${BASEDIR}/var/http/demo/public
      fi
    fi

    if [ -d ${BASEDIR}/var/http/demo/sws-icons ]; then
      $gettext "Removing directory '/var/http/demo/sws-icons'"
      rm -rf ${BASEDIR}/var/http/demo/sws-icons
      if [ -f ${BASEDIR}/var/http/demo/sws-icons ]; then
        /usr/sbin/unlink ${BASEDIR}/var/http/demo/sws-icons
      fi
    fi
  fi

  if [ "$removeJVMPackages" = "y" ]; then
    PkgList=${COREJAVAPACKAGES}
    removePkgList
  fi

  if [ "$removeHJPackages" = "y" ]; then
    PkgList=${HOTJAVAPACKAGES}
    removePkgList
  fi

  # Do additional work if -d option was selected.

  if [ "$DRAMATIC_REMOVE" = "1" ]; then
    if [ -f $HAGET -a "$HA_INSTALL" = "1" -a "$HA_MASTER" = "0" ]; then
      _removeDirs="/etc/opt/SUNWmail /var/opt/SUNWmail /var/opt/SUNWwa /opt/SUNWwa"

      if [ "$removeSDSdatabase" = "y" ]; then
        _removeDirs="${_removeDirs} /etc/opt/SUNWconn/ldap"
      fi
    else
      _removeDirs="/etc/opt/SUNWmail /var/opt/SUNWmail /var/opt/SUNWwa /opt/SUNWwa"
      if [ "$removeSDSdatabase" = "y" ]; then
        _removeDirs="${_removeDirs} /etc/opt/SUNWconn/ldap /opt/SUNWconn/ldap"
      fi
    fi

    for _dir in ${_removeDirs}; do
      if [ -d "${BASEDIR}${_dir}" ]; then
        eval echo `$gettext 'Removing directory ${FORWARDQUOTE}${BASEDIR}${_dir}${FORWARDQUOTE}'`
        /bin/rm -rf ${BASEDIR}${_dir}
        if [ -f ${BASEDIR}${_dir} ]; then
          /usr/sbin/unlink ${BASEDIR}${_dir}
        fi
      fi
    done

    if [ -f ${BASEDIR}/var/log/simsauditlog ]; then
      rm -rf ${BASEDIR}/var/log/simsauditlog
    fi
  fi

  echo ""
  echo `$gettext "sendmail has been restored by SIMS uninstall."`
  echo ""
  echo "`$gettext 'Start sendmail by either starting it manually\nor by rebooting.'`"
}

Usage()
{
  eval echo `$gettext 'Usage: ${binName} -[c${PIPE}d] sims [ -f listfile ] '`
}

WHO=`/bin/id | /bin/sed "s/uid=[0-9]*(\([^ )]*\)).*/\1/"`
_pid=$$
_name=$0
_tmpname=.remove.$_pid
DRAMATIC_REMOVE=0

if [ $WHO != "root" ]; then
  eval echo `$gettext 'ERROR: You must be ${DOUBLEQUOTE}root${DOUBLEQUOTE} to run ${binName}'`
  exit 1
fi

HAGET=/opt/SUNWhadf/bin/haget

if [ -f $HAGET ]; then
  HAMastered=`${HAGET} -f mastered`

  if [ `/bin/pkginfo SUNWimcom 2>/dev/null | grep -ci SUNWimcom` != "0" ]; then
    HA_INSTALL=`LC_ALL=C; pkgparam SUNWimcom HA_INSTALL`
    HA_MASTERLHOST=`LC_ALL=C; pkgparam SUNWimcom HA_MASTERLHOST`
    HA_MASTER=`LC_ALL=C; pkgparam SUNWimcom HA_MASTER`
  else
    if [ `/bin/pkginfo SUNWimha 2>/dev/null | grep -ci SUNWimha` != "0" ]; then
      HA_INSTALL=`LC_ALL=C; pkgparam SUNWimha HA_INSTALL`
      HA_MASTERLHOST=`LC_ALL=C; pkgparam SUNWimha HA_MASTERLHOST`
      HA_MASTER=`LC_ALL=C; pkgparam SUNWimha HA_MASTER`
    else
      HA_INSTALL=0
      HA_MASTER=0
    fi
  fi

  if [ $HA_INSTALL = 1 ]; then
    HA_MASTERLHOST_Mastered=0
    for host in ${HAMastered}; do
      if [ "$host" = "$HA_MASTERLHOST" ]; then
        HA_MASTERLHOST_Mastered=1
        break
      fi
    done

    if [ $HA_MASTERLHOST_Mastered = 0 ]; then
      eval echo `$gettext 'ERROR: This host does NOT master the logical host ${FORWARDQUOTE}${HA_MASTERLHOST}${FORWARDQUOTE}${NEWLINE}onto which SIMS HA is installed.'`
      echo ""
      eval echo `$gettext 'Please configure this host to master the logical host ${FORWARDQUOTE}${HA_MASTERLHOST}${FORWARDQUOTE}${NEWLINE}before uninstalling.'`
      exit 1
    fi
  fi
fi

pkgListFile=

while getopts c:d:f: name
do
  case $name in 

  c) COMMANDARG="$OPTARG";;
  d) COMMANDARG="$OPTARG"
     DRAMATIC_REMOVE=1;;
  f) pkgListFile="$OPTARG";;
  ?) Usage
     exit 2;;

  esac
done

pkgListFileName=sims_pkglist
locationOne=/opt/SUNWmail/lib
locationTwo=/opt/SUNWmail/admin/lib
locationThree=/opt/SUNWimha/lib
 
if [ ! -r "${pkgListFile}" ]; then 
  if [ `/bin/pkginfo SUNWimcom 2>/dev/null | grep -ci SUNWimcom` != "0" ]; then
    baseDir=`/bin/pkgparam SUNWimcom BASEDIR`
  else
    if [ `/bin/pkginfo SUNWimha 2>/dev/null | grep -ci SUNWimha` != "0" ]; then
      baseDir=`/bin/pkgparam SUNWimha BASEDIR`
    fi
  fi

  # Look for file in baseDir and / (the default basedir)  
  for dir in $basedir /
  do
    if [ -r ${dir}${locationOne}/${pkgListFileName} ]; then
      pkgListFile=${dir}${locationOne}/${pkgListFileName}
    else
      if [ -r ${dir}${locationTwo}/${pkgListFileName} ]; then
        pkgListFile=${dir}${locationTwo}/${pkgListFileName}
      else
        if [ -r ${dir}${locationThree}/${pkgListFileName} ]; then
          pkgListFile=${dir}${locationThree}/${pkgListFileName}
        fi
      fi
    fi
  done
fi

if [ -z "$pkgListFile" ]; then
  echo 
  eval echo `$gettext 'Unable to find list file: ${pkgListFileName}. The file exists on the SIMS${NEWLINE}product CD in products/sims/tools/${pkgListFileName}. Copy this file to${NEWLINE}${locationOne} and try again.'`
  echo 
  exit 1
fi

case "$COMMANDARG" in

'sims')
  echo `$gettext "Uninstalling Sun Internet Mail Server v4.0..."`
  cp $_name /tmp/$_tmpname
  if [ "`echo ${pkgListFile} | cut -c1`" != "/" ]; then
    pkgListFile=`pwd`/${pkgListFile}
  fi

  cd /tmp
  if [ $DRAMATIC_REMOVE != "0" ]; then
    exec /tmp/$_tmpname -d doReMoVe -f ${pkgListFile}
  else
    exec /tmp/$_tmpname -c doReMoVe -f ${pkgListFile}
  fi
  exit 0
  ;;
'doReMoVe')
  # NEVER EVER USE THIS ARGUMENT DIRECTLY.  

  _secArg=`/bin/echo $0 | /bin/awk -F. '{print $2}'`
  if [ -z $_secArg ]; then
    echo `$gettext "You cannot use this option directly"`
    Usage
  else
    if [ -r "$pkgListFile" ]; then
      . $pkgListFile
    else
      echo 
      eval echo `$gettext 'Unable to read list file: ${pkgListFile}.'`
      echo 
      exit 1
    fi
    initially
    stopLit
    removeAllPackages
  fi

  # Clean up scratch files

  for file in ${_tmpname} uninstall ${pkgListFileName}
  do
    /bin/rm -f /tmp/$file
    if [ -f /tmp/$file ]; then
      /usr/sbin/unlink  /tmp/$file
    fi
  done
  ;;
*)
  Usage
  ;;

esac
