#!/bin/ksh
# SIMS remove script
# @(#)uninstall	1.47 04/23/99 SMI 
#

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

_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

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

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

# Aliases for special characters, used in eval statements and gettext
AMPERSAND='&'
DOLLAR='$'
NEWLINE='\n'
TAB='\t'
FORWARDQUOTE="'"
BACKQUOTE='`'
DOUBLEQUOTE='"'
LBRACKET='<'
RBRACKET='>'

OSREV=`LC_ALL=C; /bin/uname -r`
OSREV=${OSREV:-5.0}
OSMAJOR=`echo $OSREV | cut -f1 -d.`
OSMINOR=`echo $OSREV | cut -f2 -d.`

L10NPACKAGES=`$gettext 'L10NPACKAGES'`
L10NHOTJAVAPACKAGE=`$gettext 'L10NHOTJAVAPACKAGE'`
L10NMUST_REMOVE_PKGS=`$gettext 'L10NMUST_REMOVE_PKGS'`
L10N_HJ_SPARC_PATCH=`$gettext 'L10N_HJ_SPARC_PATCH'`
L10N_HJ_INTEL_PATCH=`$gettext 'L10N_HJ_INTEL_PATCH'`

if [ "$L10NPACKAGES" = "L10NPACKAGES" ]; then
  L10NPACKAGES=""
fi

if [ "$L10NHOTJAVAPACKAGE" = "L10NHOTJAVAPACKAGE" ]; then
  L10NHOTJAVAPACKAGE=""
fi

if [ "$L10NMUST_REMOVE_PKGS" = "L10NMUST_REMOVE_PKGS" ]; then
  L10NMUST_REMOVE_PKGS=""
fi

if [ "$L10N_HJ_SPARC_PATCH" = "L10N_HJ_SPARC_PATCH" ]; then
  L10N_HJ_SPARC_PATCH=""
fi

if [ "$L10N_HJ_INTEL_PATCH" = "L10N_HJ_INTEL_PATCH" ]; then
  L10N_HJ_INTEL_PATCH=""
fi

if [ $OSMAJOR -eq 5 -a $OSMINOR -lt 6 ]; then
  ICONVPACKAGES="SUNWciu8 SUNWhiu8 SUNWjiu8 SUNWjiu8j SUNWkiu8 SUNWtiu8 SUNW
uiu8"
fi

if [ $OSMAJOR -eq 5 -a $OSMINOR -eq 6 ]; then
  ICONVPACKAGES="SUNWjiu8j SUNWtiu8"
fi

if [ ${ARCH} = "sparc" ]; then
	L10N_HJ_PATCH=${L10N_HJ_SPARC_PATCH}
else
	L10N_HJ_PATCH=${L10N_HJ_INTEL_PATCH}
fi

# defines used in the functions..
#
MUST_REMOVE_PKGS="$L10NMUST_REMOVE_PKGS $ICONVPACKAGES SUNWimsdd SUNWimsdh SUNWimsdk SUNWwa SUNWjeev SUNWimcpr SUNWimcms SUNWimccc SUNWimc SUNWimha SUNWimmta SUNWbbmta SUNWimims SUNWimimu SUNWimgtw SUNWimadm SUNWimdoc SUNWimimm SUNWlit SUNWlicsw SUNWimcom SUNWsds" 
WEB_PACKAGES="SUNWhttpv SUNWhttpu SUNWhttpr"
HOTJAVA_PACKAGES="$L10NHOTJAVAPACKAGE SUNWdthj"
JVM_PACKAGES="SUNWjvdem SUNWjvdev SUNWjvman SUNWjvjit SUNWjvrt"

SPARC_SIMS_PATCH="105266 105449 106514"
I386_SIMS_PATCH="105272 105450 106515"

initially() {
## Do necessary to 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() {

(/etc/init.d/lic_mgr stop << EOF
y
EOF
)

}


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

for patch in $_plist; do
  if [ -d /var/sadm/patch/${patch}* ]; then
    patchlist=`ls -d /var/sadm/patch/${patch}*`
    for patchdir in $patchlist; do
      p_name=`/usr/bin/basename $patchdir`
      curdir=`pwd`
      cd $patchdir
      if [ -f backoutpatch ]; then
	eval echo `$gettext 'Removing patch ${FORWARDQUOTE}${p_name}${FORWARDQUOTE}'`
	./backoutpatch ${p_name}
      fi
      cd $curdir
    done
  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

  if [ -f /etc/init.d/httpd ]; then
    textmsg=`$gettext "Remove Sun WebServer(tm) (n): "`
    removeWebPackages=`/bin/ckyorn -Q -d n -p "$textmsg" `
  else
    removeWebPackages=n
  fi
  textmsg=`$gettext "Remove Java Virtual Machine (n): "`
  removeJVMPackages=`/bin/ckyorn -Q -d n -p "$textmsg" `
  textmsg=`$gettext "Remove HotJava(tm) (n): "`
  removeHJPackages=`/bin/ckyorn -Q -d n -p "$textmsg"`

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 and directory databases.  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 and directory databases (n) : "`
  docleanRemove=`/bin/ckyorn -Q -d n -p "$textmsg"`
else
  docleanRemove=y
fi

textmsg=`$gettext "Are you sure you want to remove Sun Internet Mail Server(tm) (n): "`
areYouSure=`/bin/ckyorn -d n -p "$textmsg"`

if [ "$areYouSure" != "y" -a  "$areYouSure" != "Y" -a \
		"$areYouSure" != "yes" -a  "$areYouSure" != "YES" ]; then

	echo `$gettext "Aborting uninstaller..."`
else
	# first remove the patches to the SIMS packages
	removePatchesToCorePackages

	# determine the directory where slapd.conf will be preserved by SUNWsds's 
	# package remove
        _slapdBackupDir=
        if [ `/bin/pkginfo SUNWsds 2>/dev/null | grep -ci SUNWsds` != "0" ]; then
	  _slapdBackupDir=`/bin/pkgparam SUNWsds BACKUPDIR`
        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
	for pkg in ${MUST_REMOVE_PKGS}; 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
					echo " "
        	fi
	done

	# 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

        if [ "$docleanRemove" != "y" -a  "$docleanRemove" != "Y" \
			-a "$docleanRemove" != "yes" -a  "$docleanRemove" != "YES" ]; then
		echo ""
	else 
		# remove all databases	
		echo `$gettext "Removing directory and message store databases..."`
		_dbToRemove="/var/opt/SUNWconn/ldap /var/opt/SUNWmail/ims"
		for _db in $_dbToRemove; do
			if [ -d ${BASEDIR}$_db ]; then
 				/bin/rm -rf ${BASEDIR}$_db
				/usr/sbin/unlink  ${BASEDIR}$_db
			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"
	_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
			/usr/sbin/unlink ${BASEDIR}$_files
		fi
	done

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

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

        if [ -d ${BASEDIR}/var/opt/SUNWconn -a -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

	# done cleaning up all temporary file and directories left around

    if [ "$removeWebPackages" != "y" -a  "$removeWebPackages" != "Y" \
			-a "$removeWebPackages" != "yes" -a  "$removeWebPackages" != "YES" ]; then
		echo ""
	else 
		/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
			/usr/sbin/unlink ${BASEDIR}/var/http/demo/public/sims
		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
			/usr/sbin/unlink ${BASEDIR}/var/http/demo/cgi-bin/sims
		fi

		if [ -f /etc/init.d/httpd ]; then
			echo `$gettext "Stopping Sun WebServer..."`
			/etc/init.d/httpd stop
		fi
		for pkg in ${WEB_PACKAGES}; do
        	if [ `/bin/pkginfo $pkg 2>/dev/null | grep -ci $pkg` != "0" ]; then
                /usr/sbin/pkgrm -n -a $_adminFile $pkg
        	fi
		done

		if [ -d /etc/http.bak ]; then
			rm -rf /etc/http.bak
			/usr/sbin/unlink  /etc/http.bak
		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
			/usr/sbin/unlink ${BASEDIR}/var/http/demo/cgi-bin
		fi

		if [ -d ${BASEDIR}/var/http/demo/public ]; then
			$gettext "Removing directory '/var/http/demo/public'"
			rm -rf ${BASEDIR}/var/http/demo/public
			/usr/sbin/unlink ${BASEDIR}/var/http/demo/public
		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
			/usr/sbin/unlink ${BASEDIR}/var/http/demo/sws-icons
		fi

    fi

    if [ "$removeJVMPackages" != "y" -a  "$removeJVMPackages" != "Y" \
			-a "$removeJVMPackages" != "yes" -a  "$removeJVMPackages" != "YES" ]; then
		echo ""
	else 
		for pkg in ${JVM_PACKAGES}; do
        	if [ `/bin/pkginfo $pkg 2>/dev/null | grep -ci $pkg` != "0" ]; then
                /usr/sbin/pkgrm -n -a $_adminFile $pkg
        	fi
		done
	fi

    if [ "$removeHJPackages" != "y" -a  "$removeHJPackages" != "Y" \
			-a "$removeHJPackages" != "yes" -a  "$removeHJPackages" != "YES" ]; then
		echo ""
	else 
		for pkg in ${HOTJAVA_PACKAGES}; do
        	if [ `/bin/pkginfo $pkg 2>/dev/null | grep -ci $pkg` != "0" ]; then
                /usr/sbin/pkgrm -n -a $_adminFile $pkg
        	fi
		done
		if [ ! -z "${L10N_HJ_PATCH}" ]; then
		  #
		  # Remove patches for packages that we deliver
		  #
		  startdir=`pwd`
		  cd /var/sadm/patch
		  for patch in "${L10N_HJ_PATCH}"
		  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
		          echo "Removing patch '${p_name}'"
		          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}
		fi
	fi

    if [ $DRAMATIC_REMOVE != 0 ]; then
        if [ -f $HAGET -a "$HA_INSTALL" = "1" -a "$HA_MASTER" = "0" ]; then
            _removeDirs="/etc/opt/SUNWmail /var/opt/SUNWmail /var/opt/SUNWwa /etc/opt/SUNWconn/ldap"
            /usr/sbin/unlink ${BASEDIR}/opt/SUNWconn
        else
            _removeDirs="/etc/opt/SUNWmail /var/opt/SUNWmail /var/opt/SUNWwa /etc/opt/SUNWconn/ldap /opt/SUNWconn/ldap"
        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}
		/usr/sbin/unlink ${BASEDIR}${_dir}
            fi
        done

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

        if [ -d ${BASEDIR}/etc/opt/SUNWconn -a -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

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

fi

}

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

if [ $WHO != "root" ]; then
	#L10N: to be evaluated by eval because of the way eval handles newlines.
	#L10N: The same follows for ${AMPERSAND} for "&", ${DOLLAR} for "$",
	#L10N: ${FORWARDQUOTE} for "'", ${BACKQUOTE} for "`", ${DOUBLEQUOTE} for
	#L10N: '"', ${LBRACKET} for "<", and ${RBRACKET} for ">".
	eval echo `$gettext 'ERROR: You must be ${DOUBLEQUOTE}root${DOUBLEQUOTE} to run $0'`
	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`
    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

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

	c) 	COMMANDARG="$OPTARG";;
	d)	COMMANDARG="$OPTARG"
		DRAMATIC_REMOVE=1;;
	?)  printf "Usage $0 -[c|d] sims\n"
		exit 2;;

	esac
done

case "$COMMANDARG" in

'sims')
	echo `$gettext "Uninstalling Sun Internet Mail Server v3.5..."`
	cp $_name /tmp/$_tmpname
	cd /tmp
	if [ $DRAMATIC_REMOVE != "0" ]; then
		exec /tmp/$_tmpname -d doReMoVe
	else
		exec /tmp/$_tmpname -c doReMoVe 
	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"`
		eval echo `$gettext 'Correct usage is ${DOUBLEQUOTE}$0 -[c|d] sims${ DOUBLEQUOTE}'`
	else
	        initially
		stopLit
		removeAllPackages
		exit 1
	fi
	/bin/rm -f /tmp/$_tmpname
	/usr/sbin/unlink  /tmp/$_tmpname
	;;
*)
	eval echo `$gettext 'Usage: $0 -[c|d] sims '`
	;;

esac
