#!/bin/sh
#set -x
#
# sh script to help uninstall packages
#
# (c) Copyright 1994-2005 CA  and/or its subsidiaries. All Rights
# Reserved. Use by the United States Government is subject to
# RESTRICTED RIGHTS as set out in the license agreement.
# 
# Error Codes
# 1. not "root" user
# 2. BAB_HOME not set
# 3. unsupported platform
# 4. not shutdown BrightStor
#
#SET_BOLD="tput bold 2>/dev/null"
#SET_NORM="tput rmso 2>/dev/null"
SET_BOLD=""
SET_NORM=""

[ `echo "./c"` = './c' ] && echo () { builtin echo -e "$@"; } 
CAS_HOME=$BAB_HOME
OS=`uname -s`
#setting about ksh and bash
if [ "$OS" = "Linux" ]
then
    SET='declare -a'
    
    ECHO='echo -e '
elif [ "$OS" = "FreeBSD" ]
then
    SET='set -a'
    ECHO='echo '
else
    SET='set -A'
    
    ECHO='echo '
fi
DEBIAN=0
GREP=egrep
GREP_S=grep
   	
DIALTMPFILE="/tmp/.BrightStorInstallDial.$$"

# Package variable name
#
MGR_PKG=BABmgr
SVR_PKG=BABsvr
CMG_PKG=BABcmagt
AGT_PKG=BABagntux
DRO_PKG=BABdr
NAS_PKG=BABnas
NLO_PKG=BABstklib
SAN_PKG=BABsan
ORA_PKG=BABora
IBM_PKG=BABibmlib
INF_PKG=BABinfmx
SAP_PKG=BABsap
BAIM_PKG=BABbaim
SYB_PKG=BABsybase
NOT_PKG=BABlotus
DB2_PKG=BABdb2
ING_PKG=BABing
APAGENT_PKG=BABapagent
MQAGENT_PKG=BABmysql
AVE_PKG=AVEngine
HAR_PKG=BABharagt
OES_PKG=BABoes
if [ "$OS" = "OSF1" ]; then
    SVR_PKG=BABSVR
    MGR_PKG=BABMGR
    CMG_PKG=BABCMAGT
    AGT_PKG=BABAGNTUX
    SAN_PKG=BABSAN
    NLO_PKG=BABSTKLIB
    ORA_PKG=BABORA12
    INF_PKG=BABINFMX
fi

# Product related variable
if [ "$OS" = "Linux" ] 
then
	PRODUCT_NAME="BrightStor ARCserve Backup for Linux"
else
	PRODUCT_NAME="BrightStor ARCserve Backup for UNIX"
fi	

# Product description
BASE_DESC="Server & Manager"
MGR_DESC=""
SVR_DESC=""
CMG_DESC="Common Agent"
AGT_DESC="Client Agent"
DRO_DESC="Disaster Recovery Option"
NAS_DESC="NDMP NAS Option"
NLO_DESC="Enterprise Option for Storage Tek ACSLS"
SAN_DESC="Storage Area Network (SAN) Option"
ORA_DESC="Agent for Oracle"
IBM_DESC="Enterprise Option for IBM 3494"
INF_DESC="Agent for Informix"
SAP_DESC="Enterprise Option for SAP R/3 for Oracle"
BAIM_DESC="Backup Agent Integration Module version 1.0"
SYB_DESC="Agent for Sybase"
NOT_DESC="Agent for Lotus Domino"
DB2_DESC="Enterprise Option for DB2 UDB"
ING_DESC="Enterprise Option for Advantage Ingres"
APAGENT_DESC="Agent for Apache Web Server"
MQAGENT_DESC="Agent for MySQL"
AVE_DESC="eTrust Antivirus Engine"
HAR_DESC="Agent for Harvest"
OES_DESC="Agent for Novell Open Enterprise Server"

UNINSTALL_ERR=0
UNINSTALL_CHOICE=

YES_FILE=/tmp/YES.pkgask.$$
ADMIN_FILE=/tmp/pkg.admin.$$
ENV_FILE=/etc/env.bab
ENV_FILE_HP=/etc/dt/config/Xsession.d/env.bab
LOG_FILE=/tmp/BrightStorUnInstall`date '+%m%d%y-%H%M'.log`
if [ -f /opt/CA/SharedComponents/iTechnology.location ]; then
	IGATEWAY_DIR=`cat /opt/CA/SharedComponents/iTechnology.location`
else
	IGATEWAY_DIR=/opt/CA/igateway
fi
if [ "$OS" = "Linux" ]
then
	ISPONSOR_REMOVE=$IGATEWAY_DIR/isp_remove.sh
else
	ISPONSOR_REMOVE=$IGATEWAY_DIR/isp_remove.ksh
fi
IGATEWAY_SCRIPT=$IGATEWAY_DIR/S99igateway

CAAGENT_SCRIPT=caagent

PWD=`pwd`
#MSG_FILE=$PWD/uninstall.msg
#. $MSG_FILE


#status of installed product
FLAG_INIT=0
FLAG_DETECTED=1
FLAG_SELECT=2
FLAG_UNINST=3
FLAG_UNINSTED=4
#
# once new component added,update max_id
#
max_id=22

SVR_ID=1
MGR_ID=2
CMG_ID=3
AGT_ID=4 
SAN_ID=5 
IBM_ID=6 
DRO_ID=7 
NAS_ID=8 
NLO_ID=9   
ORA_ID=10 
SYB_ID=11 
INF_ID=12 
SAP_ID=13 
NOT_ID=14  
DB2_ID=15 
ING_ID=16 
BAIM_ID=17    
APAGENT_ID=18 
MQAGENT_ID=19 
HAR_ID=20
AVE_ID=21  
OES_ID=22

#
menu_count=0
#choice_count=0

#define array
${SET} UNINST_ARR
${SET} MENU_ARR


#Objec id of agents
OBJ_ID_AGT=0
OBJ_ID_APAGENT=41
OBJ_ID_ING=19
OBJ_ID_NAS=34
OBJ_ID_MQAGENT=40
OBJ_ID_ORA=22
OBJ_ID_INF=32
OBJ_ID_NOT=14
OBJ_ID_SAP=20
OBJ_ID_SYB=30
OBJ_ID_DB2=37
OBJ_ID_HAR=47
OBJ_ID_OES=50

#
INPUT_ALL="all"
INPUT_QUIT="0"

#
LOCATION_CD=0
LOCATION_BASE=1
LOCATION_CAAGENT=2

#
AVE_RPM_PKG=1

get_real_dir()
{
	IN_DIR=$1
	OUT_DIR=""
	
	if [ "$IN_DIR" != "" ] ;then
		cd $IN_DIR 2>/dev/null
		OUT_DIR=`pwd`
	fi
	$ECHO "$OUT_DIR"	
}
check_location()
{
	IN_DIR=$1	
	
	if [ "$IN_DIR" = "" ] ;then
		return $LOCATION_CD	
	fi
	
	$ECHO $IN_DIR | $GREP "BABcmagt$" >/dev/null 2>&1	
	if [ $? -eq 0 ] 
	then
		return $LOCATION_CAAGENT
	else
		
		$ECHO $IN_DIR | $GREP "BrightStorARCserve/bin$" >/dev/null 2>&1
		if [ $? -eq 0 ] 
		then
			return $LOCATION_BASE
		else
			return $LOCATION_CD
		fi	
	fi

	
}
startDial()
{

   $ECHO "| \c"
   while [ 1 ]
   do
      sleep 1
      $ECHO "\b\b/ \c"
      sleep 1
      $ECHO "\b\b- \c"
      sleep 1
      $ECHO "\b\b\\ \c"
      sleep 1
      $ECHO "\b\b| \c"
      if [ -f ${DIALTMPFILE} ]
      then
          rm -f ${DIALTMPFILE}
          break
      fi
   done
}
stopDial ()
{

        touch ${DIALTMPFILE}

        sleep 1
        count=0
        rcvalue=1

        while [ ${count} -le 3 ]
        do
           if [ -f ${DIALTMPFILE} ]
                then
                        sleep 2
                else
                        rcvalue=0
                        break
                fi
                count=`expr ${count} + 1`
        done

        if [ ${rcvalue} -ne 0 ]
        then
        	if [ "$OS" != "Linux" ] 
        	then
                	kill -9 ${DIALPID} >/dev/null 2>&1
                fi
                rm -f ${DIALTMPFILE}
        fi
        
        
}
dialCleanup ()
{
        if [ "$OS" != "Linux" ] 
        then
   		kill -9 ${DIALPID} >/dev/null 2>&1
   	fi
        rm -f ${DIALTMPFILE}
}
to_upper()
{
   UPPER_STR=`echo $1 | tr "[:lower:]" "[:upper:]" 2>/dev/null`
   ${ECHO} $UPPER_STR
}

to_lower()
{
   LOWER_STR=`${ECHO} $1 | tr "[:upper:]" "[:lower:]" 2>/dev/null`
   ${ECHO} $LOWER_STR
}
check_uppercase()
{
	UPPER_FLAG=FALSE
	FILE=$1
	
	FILE_IN_UPPER=$(to_upper $FILE)
	
	if [ "$FILE" = "$FILE_IN_UPPER" ] 
	then
		UPPER_FLAG=TRUE
	else
		UPPER_FLAG=FALSE
	fi
}
create_tmp_nlsfile()
{
	if [ ! -d /tmp ]  
	then
		mkdir -m 666 -p /tmp 2>/dev/null
	fi
	
	if [ -f ${TMP_NLSFILE} ] 
	then
		rm -f ${TMP_NLSFILE} 2>/dev/null
	fi
	

	cp ${NLS_PATH}/nls.cfg ${TMP_NLSFILE}

}
remove_tmp_nlsfile()
{
	if [ -f ${TMP_NLSFILE} ]
	then
		rm -f ${TMP_NLSFILE}
	fi
}

#function get_config is required be called before this function for setting required variables
lang_menu()
{
	OLD_NLS_LANG=$NLS_LANG
	if [ "$LANG" = "" ] 
	then
		LANG=C
	fi
	
	NLS_LOCALE=$LANG
        NLS_LANG=`$NLS_UTILITY_FILE -c $LANG  $NLSMAPFILE 2>/dev/null`
        LC_CTYPE=`locale 2>/dev/null | grep LC_CTYPE | grep -v grep | cut -d"=" -f2| cut -d"\"" -f2`
	if [  "$NLS_LANG" = "C" -a "$LC_CTYPE" != "" ]
	then
	        LC_CTYPE_TYPE=`$NLS_UTILITY_FILE -c $LC_CTYPE  $NLSMAPFILE 2>/dev/null`
	        if [ "$LC_CTYPE_TYPE" != "$NLS_LANG" ]
	        then
	                NLS_LANG=$LC_CTYPE_TYPE
	                NLS_LOCALE=$LC_CTYPE
	        fi
	fi
	
        #If install script detects one of the Chinese/Japanese locales included in our map file then
        #we should switch to Chinese/Japanese by default without going through the language selection menu.
        if [ "$NLS_LANG" = "sc" -o "$NLS_LANG" = "tc" -o  "$NLS_LANG" = "ja_euc" -o "$NLS_LANG" = "ja_sjis" ]
        then
                :
        else
		create_tmp_nlsfile
		
		${NLS_LIST_FILE} ${C_UNINSTALL_MESSAGES_PATH} ${TMP_NLSFILE} ${NLS_UTILITY_FILE} ${NLS_PATH} ${NLS_LOCALE}  $NLSMAPFILE
		
		NLS_LANG=`$NLS_UTILITY_FILE -g $TMP_NLSFILE "CA_NLS_LANG" 2>/dev/null`
                NLS_LOCALE=`$NLS_UTILITY_FILE -gl $TMP_NLSFILE "CA_NLS_LANG" 2>/dev/null`
		
		remove_tmp_nlsfile
	fi
}
read_nls()
{

		if [ "$UPPER_FLAG" = "TRUE" ] 
		then
			ls ${PKG_LOCATION}/.BRIGHTSTORARCSERVE* >/dev/null 2>&1 
			if [ $? -eq 0 ] ;then
				NLS_PATH=${PKG_LOCATION}/NLS
				NLSMAPFILE=${NLS_PATH}/NLS.MAP
				if [ "$BAB_HOME" != "" -a -f "$BAB_HOME/lib/nls/nls.cfg" ];then
					NLSCFGFILE="$BAB_HOME/lib/nls/nls.cfg"
				elif [ -f "/opt/CA/BABcmagt/nls/nls.cfg" ] ;then
					 NLSCFGFILE="/opt/CA/BABcmagt/nls/nls.cfg"		
				else
					NLSCFGFILE=${NLS_PATH}/NLS.CFG
				fi
			else
			if [ -f  ${PKG_LOCATION}/../LIB/NLS/NLS.CFG ]  #server
			then
				NLS_PATH=${PKG_LOCATION}/../LIB/NLS
			else #common agent  
				NLS_PATH=${PKG_LOCATION}/NLS
			fi	  
			NLSCFGFILE=${NLS_PATH}/NLS.CFG
			NLSMAPFILE=${NLS_PATH}/NLS.MAP
			fi			

			
			if [ -f ${NLS_PATH}/NLS_LIST ]
			then
				NLS_LIST_FILE=${NLS_PATH}/NLS_LIST
			else
				NLS_LIST_FILE=${PKG_LOCATION}/NLS_LIST
			fi 
			
			if [ -f ${NLS_PATH}/NLS_LIST ]
			then						
				NLS_UTILITY_FILE=${NLS_PATH}/NLS_UTILITY
			else
				NLS_UTILITY_FILE=${PKG_LOCATION}/NLS_UTILITY
			fi
			
		else
			ls ${PKG_LOCATION}/.BrightStorARCserve* >/dev/null 2>&1 
			if [ $? -eq 0 ] ;then
				NLS_PATH=${PKG_LOCATION}/nls
				NLSMAPFILE=${NLS_PATH}/nls.map
				if [ "$BAB_HOME" != "" -a -f "$BAB_HOME/lib/nls/nls.cfg" ];then
					NLSCFGFILE="$BAB_HOME/lib/nls/nls.cfg"
				elif [ -f "/opt/CA/BABcmagt/nls/nls.cfg" ] ;then
					 NLSCFGFILE="/opt/CA/BABcmagt/nls/nls.cfg"		
				else
					NLSCFGFILE=${NLS_PATH}/nls.cfg
				fi
			else		
			if [ -f  ${PKG_LOCATION}/../lib/nls/nls.cfg ] 
			then
				NLS_PATH=${PKG_LOCATION}/../lib/nls
			else
				NLS_PATH=${PKG_LOCATION}/nls
			fi
			NLSCFGFILE=${NLS_PATH}/nls.cfg	
			NLSMAPFILE=${NLS_PATH}/nls.map
			fi	
						
			if [ -f ${NLS_PATH}/nls_list ]
			then
				NLS_LIST_FILE=${NLS_PATH}/nls_list
			else
				NLS_LIST_FILE=${PKG_LOCATION}/nls_list
			fi 
			
			if [ -f ${NLS_PATH}/nls_utility ]
			then						
				NLS_UTILITY_FILE=${NLS_PATH}/nls_utility
			else
				NLS_UTILITY_FILE=${PKG_LOCATION}/nls_utility
			fi					
		fi
	
		if [ -f $NLSCFGFILE -a -x $NLS_UTILITY_FILE ]
		then
	
			NLS_LANG=`$NLS_UTILITY_FILE -g $NLSCFGFILE "CA_NLS_LANG" 2>/dev/null`
		fi
			
}
set_msgpath()
{
		if [ "$NLS_LANG" = "" ] 
		then
			NLS_LANG=C
		fi
		
		if [ "$UPPER_FLAG" = "TRUE" ] 
		then
			UPPER_LANG=$(to_upper $NLS_LANG)
			UNINSTALL_MESSAGES_PATH=$NLS_PATH/${UPPER_LANG}/UNINSTALL.MSG
			C_UNINSTALL_MESSAGES_PATH=${NLS_PATH}/C/UNINSTALL.MSG			
                        if [ "$ENC_FLAG" = "UTF-8" ]
                        then
                                for i in DE FR IT SP PT
                                do
                                        if [ "${UPPER_LANG}" = "${i}" -a -d ${NLS_PATH}/${i}_UTF8 ] ;then
                                                UNINSTALL_MESSAGES_PATH=$NLS_PATH/${UPPER_LANG}_UTF8/UNINSTALL.MSG
                                        fi
                                done

                        fi
					
		else
			UNINSTALL_MESSAGES_PATH=$NLS_PATH/${NLS_LANG}/uninstall.msg
			C_UNINSTALL_MESSAGES_PATH=${NLS_PATH}/C/uninstall.msg
                      if [ "$ENC_FLAG" = "UTF-8" ]
                      then
                              for i in de fr it sp pt
                              do
                                      if [ "${NLS_LANG}" = "${i}" -a -d ${NLS_PATH}/${i}_utf8 ] ;then
                                              UNINSTALL_MESSAGES_PATH=$NLS_PATH/${NLS_LANG}_utf8/uninstall.msg
                                      fi
                              done

                      fi

		fi
}
get_config()
{
	if [ -f ${UNINSTALL_MESSAGES_PATH} ]
	then
		. ${UNINSTALL_MESSAGES_PATH}
	elif [ -f ${C_UNINSTALL_MESSAGES_PATH} ]
	then
		. ${C_UNINSTALL_MESSAGES_PATH}
		NLS_LANG="C"
		NLS_LOCALE="C"
	else
		${ECHO} "Can not locate the message file: ${UNINSTALL_MESSAGES_PATH}!"
		${ECHO} "Exiting..."
		exit 7
	fi

}

  		
Get_Pkg_Info_By_Id()
{
                                
    pkgid=$1

    case "$pkgid" in
    $SVR_ID)
    	pkg_name=$SVR_PKG
    	pkg_des=$SVR_DESC
    ;;
    $MGR_ID)
    	pkg_name=$MGR_PKG
    	pkg_des=$MGR_DESC
    ;; 
    $CMG_ID)
    	pkg_name=$CMG_PKG
    	pkg_des=$CMG_DESC
    ;; 
    $AGT_ID)
    	pkg_name=$AGT_PKG
    	pkg_des=$AGT_DESC
    ;; 
    $SAN_ID)
    	pkg_name=$SAN_PKG
    	pkg_des=$SAN_DESC
    ;; 
    $IBM_ID)
    	pkg_name=$IBM_PKG
    	pkg_des=$IBM_DESC
    ;; 
    $DRO_ID)
    	pkg_name=$DRO_PKG
    	pkg_des=$DRO_DESC
    ;; 
    $NAS_ID)
    	pkg_name=$NAS_PKG
    	pkg_des=$NAS_DESC
    ;; 
    $NLO_ID)
    	pkg_name=$NLO_PKG
    	pkg_des=$NLO_DESC
    ;; 
    $ORA_ID)
    	pkg_name=$ORA_PKG
    	pkg_des=$ORA_DESC
    ;; 
    $SYB_ID)
    	pkg_name=$SYB_PKG
    	pkg_des=$SYB_DESC
    ;;  
    $INF_ID)
    	pkg_name=$INF_PKG
    	pkg_des=$INF_DESC
    ;; 
    $SAP_ID)
    	pkg_name=$SAP_PKG    	
    	pkg_des=$SAP_DESC
    ;;     
    $NOT_ID)
    	pkg_name=$NOT_PKG
    	pkg_des=$NOT_DESC
    ;; 
    $DB2_ID)
    	pkg_name=$DB2_PKG
    	pkg_des=$DB2_DESC    
    ;;   
    $ING_ID)
    	pkg_name=$ING_PKG
    	pkg_des=$ING_DESC    
    ;;   
    $BAIM_ID)

    	pkg_name=$BAIM_PKG
    	pkg_des=$BAIM_DESC      
    ;;               
    $APAGENT_ID)
    	pkg_name=$APAGENT_PKG
    	pkg_des=$APAGENT_DESC    
    ;;   
    $MQAGENT_ID)
    	pkg_name=$MQAGENT_PKG
    	pkg_des=$MQAGENT_DESC    
    ;;   
    $HAR_ID)
    	pkg_name=$HAR_PKG
    	pkg_des=$HAR_DESC
    ;;  
    $AVE_ID)
    	pkg_name=$AVE_PKG
    	pkg_des=$AVE_DESC    
    ;;  
    $OES_ID)
    	pkg_name=$OES_PKG
    	pkg_des=$OES_DESC    
    ;;  
    *)
    	pkg_name=""
    	pkg_des=""                                         
    ;;
    
    esac
        
    if [ "$OS" = "Linux" -a $DEBIAN -eq 1 -a "$pkg_name" != "" ] 
    then
    	pkg_name=$(to_lower $pkg_name)
    fi

    
}
#
#kill igateway if it can't be down properly
#
kill_igateway()
{

   PS="ps -aef"

   BABOPEN="BrightStor"
   TMP_FILE="/tmp/.$BABOPEN.$$"
   PSCUT="-c9-14"



   PROC_LIST='igateway'
   $PS | $GREP $PROC_LIST | $GREP -v $GREP_S  > $TMP_FILE

   ${ECHO} ""
   for PROC in $PROC_LIST
   do
        K_PID=`cat $TMP_FILE | $GREP $PROC | cut $PSCUT`
        if [ "$K_PID" != "" ]; then
                #printf "killing process  " $PROC; ${ECHO} $K_PID
                kill -9 $K_PID
        fi
   done

   rm -f $TMP_FILE
}
kill_BAB()
{

   PS="ps -aef"

   BABOPEN="BrightStor"
   TMP_FILE="/tmp/.$BABOPEN.$$"
   PSCUT="-c9-14"

   BABSVRD_LIST='caservd|cadiscovd|cauthd|caloggerd|cadbd|camediad|caqd|cprocess'
   $PS | $GREP $BABSVRD_LIST | $GREP -v $GREP_S | $GREP "$CAS_HOME|caservd|cprocess" > $TMP_FILE

   for PROC in caservd cadiscovd cauthd caloggerd caqd cadbd camediad cprocess ca_log ca_qmgr
   do
        K_PID=`cat $TMP_FILE | $GREP $PROC | $GREP -v $GREP_S | cut $PSCUT`
        if [ "$K_PID" != "" ]; then
                #printf "killing process  " $PROC; ${ECHO} $K_PID
                kill -9 $K_PID
        fi
   done

   BABMGRD_LIST='cacommd|httpd'
   $PS | $GREP $BABMGRD_LIST | $GREP -v $GREP_S | $GREP "$CAS_HOME" > $TMP_FILE

   ${ECHO} ""
   for PROC in cacommd httpd
   do
        K_PID=`cat $TMP_FILE | $GREP $PROC | cut $PSCUT`
        if [ "$K_PID" != "" ]; then
                #printf "killing process  " $PROC; ${ECHO} $K_PID
                kill -9 $K_PID
        fi
   done

   rm -f $TMP_FILE
}
shutdown_BAB()
{

   if [ "$CAS_HOME" = "" ] 
   then
   	return
   fi
   
   BAB_LIST='caservd|cadiscovd|cauthd|caloggerd|cadbd|camediad|caqd'
   BAB_MIN=1
   BAB=$CAS_HOME/bin/bab

#  check if BrightStor has already been started properly

   #BAB_COUNT=`$BAB -show all 2>/dev/null | grep -v "Exec" | grep "Running" | wc | $AWK '{ printf("%d", $1); }'`
   #if [ $BAB_COUNT -ge $BAB_MIN ]
   $BAB -show all 2>/dev/null
   if [ $? -eq 0 ] 
   then
        ${ECHO}
        ${ECHO} "$UNINST_MSG_14"
        ANS=`ckyorn -d "y" -p "$UNINST_MSG_15 "`

        case $ANS in
          y|Y|yes|YES)
                $CAS_HOME/sbin/cstop
		if [ $? -ne 0 ] ;then
                	${ECHO}
                	${ECHO} "$UNINST_ERRMSG_5"
                	${ECHO} "$UNINST_ERRMSG_6"
                	${ECHO} "$UNINST_ERRMSG_7"
                	${ECHO}
                	${ECHO} "$UNINST_ERRMSG_2"
                	${ECHO}
                	exit 4
		fi
		
                sleep 10
                kill_BAB
                ;;
          *)
                ${ECHO}
                ${ECHO} "$UNINST_ERRMSG_5"
                ${ECHO} "$UNINST_ERRMSG_6"
                ${ECHO} "$UNINST_ERRMSG_7"
                ${ECHO}
                ${ECHO} "$UNINST_ERRMSG_2"
                ${ECHO}
                exit 4
                ;;
        esac
   else
        ${ECHO}
        ${ECHO} "$UNINST_MSG_16"
   fi
}
#
#stop server
#
Stop_Base()
{

	
	  if [ "$CAS_HOME" = "" ]; then
		${ECHO} "$UNINST_ERRMSG_1"
		${ECHO} "$UNINST_ERRMSG_2"
                ${ECHO}
		exit 2
	  fi
	
	  ${ECHO}
	  ${ECHO} "$UNINST_MSG_7"
	  ${ECHO}
	  ${ECHO} "$UNINST_MSG_8"
	  shutdown_BAB
	  ${ECHO}
	  ${ECHO} "$UNINST_MSG_9"
	  if [ -x $CAS_HOME/sbin/stopingres -a -d $CAS_HOME/dbase/ingres/ingres ];then
	    $CAS_HOME/sbin/stopingres
	    if [ $? -ne 0 ]
	    then
                ${ECHO}
                ${ECHO} "$UNINST_ERRMSG_2"
                ${ECHO}	    
		exit 5
	    fi
	  else
        	${ECHO}	  
	  	${ECHO} ${UNINST_MSG_22}
	  fi

	
}
#
#stop a agent
#
Stop_Agent()
{
	
	pkgid=$1
	Get_Obj_Id $pkgid
	if [ "$obj_id" != "" ] 
	then
		${CAAGENT_SCRIPT} disable $obj_id >/dev/null 2>&1
	fi	


	
}
#
#get agent object id
#
Get_Obj_Id()
{

    pkgid=$1
    case "$pkgid" in
    $AGT_ID)
    	obj_id=$OBJ_ID_AGT
    ;; 
    $NAS_ID)
    	obj_id=$OBJ_ID_NAS
    ;; 
    $ORA_ID)
    	obj_id=$OBJ_ID_ORA
    ;; 
    $SYB_ID)
    	obj_id=$OBJ_ID_SYB
    ;;  
    $INF_ID)
    	obj_id=$OBJ_ID_INF
    ;; 
    $SAP_ID)
    	obj_id=$OBJ_ID_SAP
    ;;     
    $NOT_ID)
    	obj_id=$OBJ_ID_NOT
    ;; 
    $DB2_ID)
    	obj_id=$OBJ_ID_DB2 
    ;;   
    $ING_ID)
    	obj_id=$OBJ_ID_ING   
    ;;                   
    $APAGENT_ID)
    	obj_id=$OBJ_ID_APAGENT   
    ;;   
    $MQAGENT_ID)
    	obj_id=$OBJ_ID_MQAGENT   
    ;;   
    $HAR_ID)
    	obj_id=$OBJ_ID_HAR 
    ;;
    $OES_ID)
    	obj_id=$OBJ_ID_OES
    ;;
    *)
    	pkg_name=""
    	pkg_des=""                                         
    ;;
    esac
}
# function to test if root is running this script
#
IS_ROOT ()
{
        ROOT=`id | cut -c5- | cut -d"(" -f1`
        test "$ROOT" = "0"
}

Check_Installed_pkg( )
{
		checked_pkg_name=$1
		rc=1
		if [ "$checked_pkg_name" = "" ]
		then
			return $rc
		fi
		
		if [ "$OS" = "AIX" ] 
		then
			$PKGINFO "${checked_pkg_name}.obj" >/dev/null 2>&1
			rc=$?

		elif [ "$OS" = "OSF1" ] 
		then
			if [ ! -f $TMPDIR/pkglist ]; then
				$PKGINFO | ${AWK} '/BAB/ && $2 == "installed" { print $1 }' > $TMPDIR/pkglist
			fi
			grep $checked_pkg_name $TMPDIR/pkglist > /dev/null 2>&1
			rc=$?
		elif [ "$OS" = "Linux" -a $DEBIAN -eq 1 ] 
		then
		
			check_result=`$PKGINFO $checked_pkg_name 2>/dev/null  | $GREP_S $checked_pkg_name`
			if [ "$check_result" != "" ] 
			then
				check_result=`${ECHO} $check_result | ${AWK} '{ print $1 }'`
				if [ "$check_result" != "pn" ] 
				then
					rc=0
				fi
			fi
		else
		
   			$PKGINFO ${checked_pkg_name} >/dev/null 2>&1
   			rc=$?
		fi

		if [ "$checked_pkg_name" = "$AVE_PKG" ] ;then
			if [ $rc -ne 0 ];then
				if [ "$CAIGLBL0000" != "" -a -f $CAIGLBL0000/ino/.BABave ] ;then
					rc=0	
				fi
				AVE_RPM_PKG=0;
			else
				AVE_RPM_PKG=1;
			fi
		fi 
		return $rc
}
Detect_pkg()
{
  
  ${ECHO}
  ${ECHO} "$UNINST_MSG_10"
  ${ECHO}
  
  Initialize_Menu_Arr

   pkg_index=0
   menu_index=0
   while [ $pkg_index -lt $max_id ] 
   do
	pkg_index=`expr $pkg_index + 1`
	
   	Get_Pkg_Info_By_Id $pkg_index
   	if [ "$pkg_name" != "" ]
   	then
		Check_Installed_pkg $pkg_name		
   		if [ $? -eq 0 ]
   		then
   			#echo "OK"
   			eval UNINST_ARR_${pkg_index}="$FLAG_DETECTED"
            eval tmp_svr=\$UNINST_ARR_${SVR_ID}
            eval tmp_mgr=\$UNINST_ARR_${MGR_ID}
            eval tmp_sap=\$UNINST_ARR_${SAP_ID}
   			if [ "$tmp_svr" = "$FLAG_DETECTED" -a $pkg_index -eq $MGR_ID ] 
   			then
   				:
   			elif  [ "$tmp_mgr" = "$FLAG_DETECTED" -a $pkg_index -eq $SVR_ID ]
   			then
   				:
   			elif  [ "$tmp_sap" = "$FLAG_DETECTED" -a $pkg_index -eq $BAIM_ID ]
   			then
   			 	:   			 	
   			else
   				menu_index=`expr $menu_index + 1`
   				eval MENU_ARR_${menu_index}=$pkg_index
   				
   			fi
   		fi   		
   		
   	fi 
   	
   done
   menu_count=$menu_index;    
}

Display_Package_Menu()
{
${ECHO} "#####################################################################"
${ECHO} "             $UNINST_MSG_19                                        "
${ECHO} "#####################################################################"
	menu_index=0
	while [ $menu_index -lt $menu_count ]
	do
		menu_index=`expr $menu_index + 1`
		eval tmp_menu=\$MENU_ARR_${menu_index}	
		if [ "$tmp_menu" = "$SVR_ID" -o "$tmp_menu" = "$MGR_ID" ]
		then
			pkg_des="$BASE_DESC"
			pkg_name="$SVR_PKG & $MGR_PKG"
		elif [ "$tmp_menu" = "$SAP_ID" -o "$tmp_menu" = "$BAIM_ID" ] 
		then
			pkg_des="$SAP_DESC"
			pkg_name="$SAP_PKG & $BAIM_PKG"			
		else
			eval Get_Pkg_Info_By_Id \$MENU_ARR_${menu_index}
		fi 
		printf " %+3s.$PRODUCT_NAME %-40s %-20s\n" "$menu_index" "$pkg_des" "($pkg_name)"
		
		
	done
	
${ECHO} 
printf " %+3s.$UNINST_MSG_20\n" "0"
${ECHO} "#####################################################################"
${ECHO} "$UNINST_MSG_11"
${ECHO} "$UNINST_MSG_12 \c"
read UNINSTALL_CHOICE
}


Initialize_Menu_Arr()
{
    
    menu_index=0

    while [ ${menu_index} -lt ${menu_count} ]
    do
       menu_index=`expr $menu_index + 1`    
       eval MENU_ARR_${menu_index}=0     
    done
    menu_count=0
}

Cancel_Select()
{
    
    pkg_index=0

    while [ ${pkg_index} -lt ${max_id} ]
    do
       pkg_index=`expr $pkg_index + 1`
       eval tmp=\$UNINST_ARR_${pkg_index}
       if [ "$tmp" = "$FLAG_SELECT" ]
       then
       	 eval UNINST_ARR_${pkg_index}=$FLAG_DETECTED
       fi
           
    done

}

Process_Package_Menu()
{
choice_err=1

###########################################
while [ $choice_err -eq 1 ]
do
    Display_Package_Menu

    Cancel_Select
    
    if [ "$UNINSTALL_CHOICE" = "$INPUT_QUIT" ]
    then
        ${ECHO}
        ${ECHO} "$UNINST_ERRMSG_2"
        ${ECHO}		
     	exit 0    
    elif [ "$UNINSTALL_CHOICE" = "$INPUT_ALL" ] 
    then
    	   choice_counter=0
    	   while [ $choice_counter -lt $menu_count ]
    	   do
    	   	choice_counter=`expr $choice_counter + 1`
   	        eval tmp2=\$MENU_ARR_${choice_counter}
                eval UNINST_ARR_${tmp2}=$FLAG_SELECT
   	        	   
    	   done
    	   choice_err=0
    elif [ "$UNINSTALL_CHOICE" = "" ]
    then
	   choice_err=1
           ${ECHO} "${UNINST_MSG_1}"
    else	
	    num_choices=`${ECHO} $UNINSTALL_CHOICE | $AWK -F, '{print NF}'`
	    choice_counter=0
	    choice_err=0
	
	    while [ $choice_counter -lt $num_choices ]
	    do
	    	choice_counter=`expr $choice_counter + 1 `
	        choice_id=`${ECHO} $UNINSTALL_CHOICE | cut -d',' -f$choice_counter`
	
	        expr "$choice_id" + 1 >/dev/null 2>&1
	        if [ $? -ge 2 ]
	        then
			choice_err=1
			${ECHO} "${UNINST_MSG_1}"
			break
	        fi
	
		if [ $choice_id -eq ${INPUT_QUIT} ]
		then
	                ${ECHO}
	                ${ECHO} "$UNINST_ERRMSG_2"
	                ${ECHO}		
	             	exit 0		
		elif [ $choice_id -gt $menu_count -o $choice_id -lt 0  ]
		then
			choice_err=1
	            	${ECHO} "${UNINST_MSG_1}"	
	            	break
		fi
		eval tmp3=\$MENU_ARR_${choice_id}
		eval UNINST_ARR_${tmp3}=$FLAG_SELECT
		
	        
	     done
     fi
     
     
done
###########################################
    
}

# Common routine for performing selection specific commands
#
Perform_Selection_Switch()
{

    command=$*

    pkg_index=0
#   if [ "$command" = "${ECHO}" ]; then
      ${ECHO}; ${ECHO} "$UNINST_MSG_5"; ${ECHO}
#    fi

#    if [ "$command" != "" ]; then  
	    while [ $pkg_index -lt $max_id ]
	    do
	    	pkg_index=`expr $pkg_index + 1 `
	      	eval tmp4=\$UNINST_ARR_${pkg_index}
	    	if [ "$tmp4" = "$FLAG_SELECT" ] 
	    	then
	    	if [ "$pkg_index" = "$SVR_ID" ]
	    	then
	    		pkg_des="$BASE_DESC"
			pkg_name="$SVR_PKG & $MGR_PKG"
		elif [ "$pkg_index" = "$SAP_ID" ]
		then
			pkg_des="$SAP_DESC"
	 		pkg_name="$SAP_PKG & $BAIM_PKG"			
	  	else
	  		Get_Pkg_Info_By_Id $pkg_index
	  	fi	        
	        printf "%-3s.%-40s %-20s\n"  "" "$pkg_des"  "($pkg_name)"
#	        $command ". $pkg_des ($pkg_name)" 
	        case "$pkg_index" in
	            $SVR_ID)	 
	            	    if [ -f $ISPONSOR_REMOVE  ]
	            	    then   
	            	        printf "%-4s%-40s %-20s\n"  "" "iSponsor"  "(iSponsor)"	                 
	                    fi
	                    dep_pkg_list=""
	                    for opt_index in $SAN_ID  $IBM_ID  $NAS_ID  $NLO_ID  $DRO_ID 
	                    do
	                    	eval tmp5=\$UNINST_ARR_${opt_index}
	                    	case "$tmp5" in
	                    	$FLAG_DETECTED|$FLAG_SELECT|$FLAG_UNINST)
	                    		Get_Pkg_Info_By_Id $opt_index
	                    		dep_pkg_list=${dep_pkg_list:+"$dep_pkg_list,"}
	                    		dep_pkg_list="$dep_pkg_list $pkg_name"
	                    		;;
	                    	esac
	                    done
	                    if [ "${dep_pkg_list}" != "" ]
	                    then
	                    	    $SET_BOLD
		                    $command "    ${UNINST_MSG_4}"                  
		                    $command "    ${dep_pkg_list}"	                    	
		                    $SET_NORM
	                    fi

	                ;;
	           $CMG_ID)
	                    dep_pkg_list=""
	                    for agt_index in $NAS_ID  $OES_ID $HAR_ID $ORA_ID $SYB_ID $INF_ID $SAP_ID $NOT_ID $DB2_ID $ING_ID $APAGENT_ID $MQAGENT_ID $AGT_ID
	                    do
	                    	eval tmp6=\$UNINST_ARR_${agt_index}
	                    	case "$tmp6" in
	                    	$FLAG_DETECTED|$FLAG_SELECT|$FLAG_UNINST)	                    	
	                    		Get_Pkg_Info_By_Id $agt_index
	                    		dep_pkg_list=${dep_pkg_list:+"$dep_pkg_list,"}
	                    		dep_pkg_list="$dep_pkg_list $pkg_name"
	                    		;;
	                    	esac
	                    done
	                    if [ "${dep_pkg_list}" != "" ]
	                    then
	                    	    $SET_BOLD
		                    $command "    ${UNINST_MSG_4}"                  
		                    $command "    ${dep_pkg_list}"	
		                    $SET_NORM                    	
	                    fi
	                ;;	                
	        esac
	        fi
	    done
#    fi
}
Save_Choice()
{
	pkg_index=0
	
	while [ $pkg_index -lt $max_id ]
	do
		pkg_index=`expr $pkg_index + 1`
		eval tmp7=\$UNINST_ARR_${pkg_index}
		if [ "$tmp7" = "$FLAG_SELECT" ]
		then 
			eval UNINST_ARR_${pkg_index}=$FLAG_UNINST	
		fi
			
	done
	eval tmp8=\$UNINST_ARR_${SVR_ID}
	if [ "$tmp8" = "$FLAG_UNINST" ] ;then
	
		for pkg_index in $MGR_ID $SAN_ID $IBM_ID $NAS_ID $NLO_ID $DRO_ID 
		do
			eval tmp9=\$UNINST_ARR_${pkg_index}
                        if [ "$tmp9" = "$FLAG_DETECTED" ] ;then
				eval UNINST_ARR_${pkg_index}=$FLAG_UNINST
			fi
		done  	
	fi

	eval tmp10=\$UNINST_ARR_${CMG_ID}
	if [ "$tmp10" = "$FLAG_UNINST" ] ;then
		for pkg_index in $NAS_ID $OES_ID $HAR_ID $ORA_ID $SYB_ID $INF_ID $SAP_ID $NOT_ID $DB2_ID $ING_ID $APAGENT_ID $MQAGENT_ID $AGT_ID 
		do
		        eval tmp11=\$UNINST_ARR_${pkg_index}
                        if [ "$tmp11" = "$FLAG_DETECTED" ] ;then
				eval UNINST_ARR_${pkg_index}=$FLAG_UNINST
			fi
		done  	
	fi
        eval tmp12=\$UNINST_ARR_${SAP_ID}
        eval tmp13=\$UNINST_ARR_${BAIM_ID}	
	if [ "$tmp12" = "$FLAG_UNINST" -o "$tmp13" = "$FLAG_UNINST" ] ;then
		eval UNINST_ARR_${BAIM_ID}=$FLAG_UNINST
		eval UNINST_ARR_${SAP_ID}=$FLAG_UNINST
	fi

	

}

Finalize_User_Packages()
{
Process_Package_Menu
Perform_Selection_Switch ${ECHO}

while [ true ]
do
  ${ECHO}
  ${ECHO} "${UNINST_MSG_6} \c"
  read y_or_n
        
  case $y_or_n in
    Y|y|Yes|yes)
    	Save_Choice
        break
        ;;
    N|n|No|no)
        Process_Package_Menu
        Perform_Selection_Switch ${ECHO}
        ;;
    Q|q|Quit|quit)
        ${ECHO}
        ${ECHO} "$UNINST_ERRMSG_2"
        ${ECHO}    
        exit 0
        ;;
    "")
    	Save_Choice
        break
        ;;
    *)
        ${ECHO} "${UNINST_MSG_1}"
        ;;
    esac
done
}

Modify_Profile()
{
CFGFILE=/etc/profile
CFGFILETMP=/etc/profile.tmp.$$

            cat $CFGFILE |$AWK 'BEGIN{flag=0;}{
                if (( $0 ~ /^#.*Modifications made by BrightStor ARCserve Backup/ ))
                {
                   flag=1
                }
                if (( $0 ~ /^#.*End Modifications made by BrightStor ARCserve Backup/ ))
                {
                   flag=0
                   continue
                }
                if ( flag == 1 )
                {
                   #DO NOTHING
                }
                else
                {
                   printf("%s\n",$0);
                }
            }' >>$CFGFILETMP

cp $CFGFILETMP $CFGFILE
rm -f $CFGFILETMP >/dev/null 2>&1
}
update_dspkg_lst()
{
	checked_pkg_name=$1
	if [ "$checked_pkg_name" = "" ]
	then
	        return
	fi
	if [ ! -r ${DSPKG_LST} ]; then
		return
	fi
	grep ${checked_pkg_name} ${DSPKG_LST} >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		return
	else
		grep -v ${checked_pkg_name} ${DSPKG_LST} > ${DSPKG_LST}.tmp
		cp ${DSPKG_LST}.tmp ${DSPKG_LST} >/dev/null 2>&1
		rm ${DSPKG_LST}.tmp >/dev/null 2>&1
	fi
}

Uninstall_pkg()
{
 
if [ "$OS" = "SunOS" ]; then
  rm -f ${ADMIN_FILE}
  rm -f ${YES_FILE}
  ${ECHO} "mail=" > ${ADMIN_FILE}
  ${ECHO} "partial=nocheck" >> ${ADMIN_FILE}
  ${ECHO} "runlevel=nocheck" >> ${ADMIN_FILE}
  ${ECHO} "idepend=nocheck" >> ${ADMIN_FILE}
  ${ECHO} "rdepend=nocheck" >> ${ADMIN_FILE}
  ${ECHO} "space=quit" >> ${ADMIN_FILE}
  ${ECHO} "setuid=nocheck" >> ${ADMIN_FILE}
  ${ECHO} "conflict=nocheck" >> ${ADMIN_FILE}
  ${ECHO} "action=nocheck" >> ${ADMIN_FILE}
  ${ECHO} "basedir=default" >> ${ADMIN_FILE}
  ${ECHO} "y" > ${YES_FILE}; ${ECHO} "y" >> ${YES_FILE}
fi

#stop base when option or base is uninstalled
#if [ "${UNINST_ARR[$DRO_ID]}" = "$FLAG_UNINST" -o "${UNINST_ARR[$NAS_ID]}" = "$FLAG_UNINST"  -o "${UNINST_ARR[$SAN_ID]}" = "$FLAG_UNINST" -o "${UNINST_ARR[$NLO_ID]}" = "$FLAG_UNINST" -o "${UNINST_ARR[$IBM_ID]}" = "$FLAG_UNINST" -o "${UNINST_ARR[$SVR_ID]}" = "$FLAG_UNINST" -o "${UNINST_ARR[$MGR_ID]}" = "$FLAG_UNINST" ]
#then
#  Stop_Base
#fi

#stop agents
for pkg_id in $NAS_ID $OES_ID $HAR_ID $ORA_ID $INF_ID $SYB_ID $SAP_ID $NOT_ID $DB2_ID $ING_ID  $APAGENT_ID $MQAGENT_ID $AGT_ID
do
	eval tmp14=\$UNINST_ARR_${pkg_id}
        if [ "$tmp14" = "$FLAG_UNINST" ] 
	then
		Stop_Agent $pkg_id
	fi
done

#stop common agent
eval tmp15=\$UNINST_ARR_${CMG_ID}

if [ "$tmp15" = "$FLAG_UNINST" ]
then 
	${CAAGENT_SCRIPT} stop
fi

option_list="$DRO_ID $NAS_ID $SAN_ID $NLO_ID $IBM_ID"
agent_list="$OES_ID $HAR_ID $ORA_ID $INF_ID $SYB_ID  $BAIM_ID $SAP_ID  $NOT_ID $DB2_ID $ING_ID $APAGENT_ID $MQAGENT_ID $AGT_ID "
misc_list="$AVE_ID"
base_list="$MGR_ID $SVR_ID"
caagent_list="$CMG_ID"

case "$LOCATION_FLAG" in 
$LOCATION_CAAGENT)
	eval tmp16=\$UNINST_ARR_${CMG_ID}
        if [ "$tmp16" = "$FLAG_UNINST" ] 
	then
		pkg_list="$misc_list $option_list $agent_list $base_list $caagent_list"
	else
		pkg_list="$misc_list $option_list $agent_list $caagent_list $base_list"
	fi
;;
*)
	pkg_list="$misc_list $option_list $agent_list $caagent_list $base_list"
;;
esac

for pkg_id in $pkg_list
do
	#get pakage name and description
	case "$pkg_id" in 
	$SVR_ID|$MGR_ID)
		pkg_des="$BASE_DESC"
	 	pkg_name="$SVR_PKG & $MGR_PKG"
	;;
	$BAIM_ID|$SAP_ID) 
		pkg_des="$SAP_DESC"
	 	pkg_name="$SAP_PKG & $BAIM_PKG"		
	;;
	*)
	 	Get_Pkg_Info_By_Id $pkg_id	 
	;;
	esac
	

	
	eval tmp17=\$UNINST_ARR_${pkg_id}
	if [ "$tmp17" = "$FLAG_UNINST" ]
	then 
		printf "%-4s%-40s %-20s ==> [ ${UNINST_MSG_23} ]" "" "$pkg_des" "($pkg_name)"
		printf "[ %s ]%-40s %-20s ==> [ ${UNINST_MSG_23} ]\n" `date '+%m/%d/%y-%H:%M:%S'` "$pkg_des" "($pkg_name)" >> ${LOG_FILE}
		startDial &
		if [ "$OS" != "Linux" ]
		then
			DIALPID=$!
		fi	
		case "$pkg_id" in 
		$SVR_ID|$MGR_ID)
			  #remove iSponsor
			  if [ -x "$IGATEWAY_SCRIPT" ] 
			  then
			    	$IGATEWAY_SCRIPT stop >> ${LOG_FILE} 2>&1
			  fi
			  
			  
			  TIME_OUT=10 
			  WAIT_COUNT=0	
			  IGATEWAY_PID=`ps -ef | $GREP_S igateway | $GREP_S -v grep | $GREP_S -v igatewayd | $GREP_S -v S99igateway | $GREP_S -v /opt/ | $GREP_S -v for | $GREP_S -v gmake | $AWK '{print $2}' `	
			  while [ "$IGATEWAY_PID" != "" -a $WAIT_COUNT -lt $TIME_OUT ] 
			  do 
			  	IGATEWAY_PID=`ps -ef | $GREP_S igateway | $GREP_S -v grep | $GREP_S -v igatewayd | $GREP_S -v S99igateway | $GREP_S -v /opt/ | $GREP_S -v for | $GREP_S -v gmake | $AWK '{print $2}' `
			  	WAIT_COUNT=`expr ${WAIT_COUNT} + 1`
			  	sleep 1
			  done 
			  
			  #rm -rf $IGATEWAY_DIR/BABiSponsor* >> ${LOG_FILE} 2>&1 
				if [ -d $IGATEWAY_DIR ]; then
					rm -rf $IGATEWAY_DIR/BAB* >> ${LOG_FILE} 2>&1
				fi
			  
			  if [ -x "$IGATEWAY_SCRIPT" ] 
			  then
			    	$IGATEWAY_SCRIPT start >> ${LOG_FILE} 2>&1
			  fi
			  
			  #remove profile
			  Modify_Profile
			  if [ -f $ENV_FILE ]; then
			    rm -f $ENV_FILE >> ${LOG_FILE} 2>&1
			  fi
			  if [ -f $ENV_FILE_HP ]; then
			    rm -f $ENV_FILE_HP >> ${LOG_FILE} 2>&1
			  fi
			  
			  #remove package
			  if [ "$OS" = "SunOS" ]; then
			    $PKGRM -a ${ADMIN_FILE} $MGR_PKG < ${YES_FILE} >> ${LOG_FILE} 2>&1
			    $PKGRM -a ${ADMIN_FILE} $SVR_PKG < ${YES_FILE} >> ${LOG_FILE} 2>&1
			  else
			    $PKGRM $MGR_PKG >> ${LOG_FILE} 2>&1
			    $PKGRM $SVR_PKG >> ${LOG_FILE} 2>&1
			  fi
			  rc=$?
			  if [ -h /opt/CA/BrightStorARCserve ]; then
			    rm -f /opt/CA/BrightStorARCserve >> ${LOG_FILE} 2>&1
			  fi
			  UNINST_ARR[$SVR_ID]=$FLAG_UNINSTED
			  UNINST_ARR[$MGR_ID]=$FLAG_UNINSTED
			  
		;;
		$BAIM_ID|$SAP_ID)    
			  if [ "$OS" = "SunOS" ]; then
			    $PKGRM -a ${ADMIN_FILE} $BAIM_PKG < ${YES_FILE} >> ${LOG_FILE} 2>&1
			    $PKGRM -a ${ADMIN_FILE} $SAP_PKG < ${YES_FILE} >> ${LOG_FILE} 2>&1   
			  else
			    $PKGRM $BAIM_PKG >> ${LOG_FILE} 2>&1
			    $PKGRM $SAP_PKG >> ${LOG_FILE} 2>&1    
			  fi
			  rc=$?
			  update_dspkg_lst $BAIM_PKG
			  update_dspkg_lst $SAP_PKG
			  if [ -h /opt/CA/BABsapagt ]; then
			    rm -f /opt/CA/BABsapagt >> ${LOG_FILE} 2>&1
			  fi
			  UNINST_ARR[$BAIM_ID]=$FLAG_UNINSTED
			  UNINST_ARR[$SAP_ID]=$FLAG_UNINSTED		  
			  	
		;;
		*)
			  if [ "$OS" = "SunOS" ]; then
			    $PKGRM -a ${ADMIN_FILE} $pkg_name < ${YES_FILE} >> ${LOG_FILE} 2>&1
			  else
				if [ "$pkg_id" = "$AVE_ID" ];then
					if [  $AVE_RPM_PKG -eq 1 ];then
			    			$PKGRM $pkg_name >> ${LOG_FILE} 2>&1
					else
						if [ "$CAIGLBL0000" != "" -a -x $CAIGLBL0000/ino/scripts/deinstall ] ;then
							 $CAIGLBL0000/ino/scripts/deinstall >> ${LOG_FILE} 2>&1
							rm -f  $CAIGLBL0000/ino/.BABave > /dev/null 2>&1
						fi
					fi
				else
					if [ "$OS" = "FreeBSD" -a "$pkg_id" = "$AGT_ID" ]; then
				            UAGENT_OLD_PKG_DIR=`pkg_info -qp $AGT_PKG | head -n 1 | cut -d' ' -f2`
				            UAGENT_OLD_PKG_DIR=${UAGENT_OLD_PKG_DIR}/BABuagent	   
					fi

					if [ "$OS" = "FreeBSD" -a "$PWD" = "/opt/CA/BABcmagt" ]; then
			                                cd ..
				             fi
					
					$PKGRM $pkg_name >> ${LOG_FILE} 2>&1
				fi
			  fi  
			  rc=$?	
			  update_dspkg_lst $pkg_name
			  case "$pkg_id" in
			    $CMG_ID)
			       if [ "$OS" = "FreeBSD" ]; then
			          rm -rf /opt/CA/BABcmagt >> ${LOG_FILE} 2>&1
			       fi
			       ;;
			    $AGT_ID)
			       if [ "$OS" = "FreeBSD" ]; then
			         rm -rf ${UAGENT_OLD_PKG_DIR} >> ${LOG_FILE} 2>&1
			       fi
			       ;; 
			    $ORA_ID)
			  	if [ -h /opt/CA/BABoraagt ]; then
			  	  rm -f /opt/CA/BABoraagt >> ${LOG_FILE} 2>&1
			  	fi
			  	;;
			    $SYB_ID) 
			  	if [ -h /opt/CA/BABsybagt ]; then
			  	  rm -f /opt/CA/BABsybagt >> ${LOG_FILE} 2>&1
			  	fi	
				;;
			    $INF_ID)
			  	if [ -h /opt/CA/BABinfmx ]; then
			  	  rm -f /opt/CA/BABinfmx >> ${LOG_FILE} 2>&1
			  	fi
			  	;;
			    $NOT_ID)
			  	if [ -h /opt/CA/BABlotusagt ]; then
			  	  rm -f /opt/CA/BABlotusagt >> ${LOG_FILE} 2>&1
			  	fi
			  	;;
			    $DB2_ID)
			  	if [ -h /opt/CA/BABdb2agt ]; then
			  	  rm -f /opt/CA/BABdb2agt >> ${LOG_FILE} 2>&1
			  	fi
			  	;;
			    $ING_ID)
			  	if [ -h /opt/CA/BABingagt ]; then
			  	  rm -f /opt/CA/BABingagt >> ${LOG_FILE} 2>&1
			  	fi
			  	;;
			    $APAGENT_ID)
			  	if [ -h /opt/CA/BABapagent ]; then
			  	  rm -f /opt/CA/BABapagent >> ${LOG_FILE} 2>&1
			  	fi
			  	;;
			    $MQAGENT_ID)
			  	if [ -h /opt/CA/BABmysql ]; then
			  	  rm -f /opt/CA/BABmysql >> ${LOG_FILE} 2>&1
			  	fi
			  	;;
			    $HAR_ID)
			  	if [ -h /opt/CA/BABharagt ]; then
			  	  rm -f /opt/CA/BABharagt >> ${LOG_FILE} 2>&1
			  	fi
			  	;;
			    $OES_ID)
			  	if [ -h /opt/CA/BABoesagt ]; then
			  	  rm -f /opt/CA/BABoesagt >> ${LOG_FILE} 2>&1
			  	fi
			  	;;
			esac
			  eval UNINST_ARR_${pkg_id}=$FLAG_UNINSTED		  	 
		;;
		esac	 
	  

		 stopDial
		 $ECHO "$BK_LINE_FLAG\c"
		 if [ $rc -eq 0 ] ;then
		 	printf "%-4s%-40s %-20s ==> [ ${UNINST_MSG_24} ]\n" "" "$pkg_des" "($pkg_name)"
		 	printf "[ %s ] %-40s %-20s ==> [ ${UNINST_MSG_24} ]\n" `date '+%m/%d/%y-%H:%M:%S'` "$pkg_des" "($pkg_name)" >> ${LOG_FILE}
		 else
		 	printf "%-4s%-40s %-20s ==> [ ${UNINST_MSG_25} ]\n" "" "$pkg_des" "($pkg_name)"
		 	printf "[ %s ] %-40s %-20s ==> [ ${UNINST_MSG_25} ]\n" `date '+%m/%d/%y-%H:%M:%S'` "$pkg_des" "($pkg_name)" >> ${LOG_FILE}
		 fi
         fi
	
done

if [ "$OS" = "SunOS" ]; then
  rm -f ${YES_FILE}
  rm -f ${ADMIN_FILE}
fi

dialCleanup

}
CHECK_PLATFORM()
{
        OS=`uname -s`
        PLATFORM="unsupported"
        case $OS in
        SunOS)
                REL=`uname -r`
                case $REL in
                5.[6789]*|5.1[0]*)
                        PLATFORM=sun
                        PKGINFO="pkginfo"
                        PKGRM="pkgrm"
                        ;;
                esac
                AWK=nawk
                ;;
        HP-UX)
                HW=`uname -r`
                case $HW in
                *.11.*|*.10.*) 
                        PLATFORM=hp
                        PKGINFO="swlist"
                        PKGRM="swremove"
                        ;;
                *)
                        ;;
                esac
                AWK=awk
                ;;
        AIX)
                PLATFORM=aix
                PKGINFO="lslpp -f "
                PKGRM="installp -u"
		AWK=awk
                ;;
        OSF1)
                PLATFORM=osf
                PKGINFO="setld -i"
                PKGRM="setld -d"
                AWK=awk
                ;;
        Linux)
        	DEBIAN=0
        	if [ -d /var/lib/dpkg -a -f /etc/debian_version ] 
        	then
        		DEBIAN=1
        	fi
        	
        	if [ $DEBIAN -eq 1 ] 
        	then
        		PLATFORM=debian
        		PKGINFO="dpkg -l"
        		PKGRM="dpkg --purge"
        	else        		
	                PLATFORM=linux
	                PKGINFO=" rpm -q "
	                PKGRM=" rpm -e "
	                
	        fi
	        AWK=awk
                ;;                
        FreeBSD)
                PLATFORM=freebsd
                PKGINFO="pkg_info "
                PKGRM="pkg_delete "
                AWK=nawk
                ;;
        esac


   
if [ "$PLATFORM" = "unsupported" ]; then
	${ECHO} "$UNINST_ERRMSG_9"
	${ECHO} "$UNINST_ERRMSG_10" 
	${ECHO}
	${ECHO} "$UNINST_ERRMSG_2"
	${ECHO}  
  	exit 3
fi
}
check_encoding()
{
      if [ "$OS" = "Linux" ]
      then
	KERNEL=`uname -r`
	ENC_FLAG="ISO8859"
	ENC_FLAG=`$NLS_UTILITY_FILE -e $KERNEL $NLSMAPFILE 2>/dev/null`
      fi
}
#########################################
#               Main
#########################################
#get present path
THISSCRIPT=`basename $0`
THISDIR=`dirname $0`
# tar file names of the different packages
#
HOMEDIR=`pwd`
TMPDIR=/tmp
ROOT_PATH=`${ECHO} $THISDIR | grep '^/'`
if [ "$ROOT_PATH" = "" ]; then
  THISDIR=$HOMEDIR/$THISDIR
fi
PKG_LOCATION="$THISDIR"
TMP_NLSFILE=/tmp/nls.cfg
NLS_LANG=C
NLS_INITIAL_LANG=""

BK_LINE_FLAG="\r"

#get location
REL_DIR=$(get_real_dir $PKG_LOCATION)
check_location $REL_DIR
LOCATION_FLAG=$?


#get locale

read_nls
check_encoding
set_msgpath    
#lang_menu
#set_msgpath
get_config
    
if IS_ROOT
then
        :
else
  ${ECHO} "$UNINST_ERRMSG_3"
  ${ECHO} "$UNINST_ERRMSG_4"
  ${ECHO}
  exit 1
fi

CHECK_PLATFORM

if [ "$OS" = "OSF1" ]; then
    rm -f $TMPDIR/pkglist 2> /dev/null
fi

Detect_pkg

Finalize_User_Packages

${ECHO} 
${ECHO} "${UNINST_MSG_17}: [ ${LOG_FILE} ]"
${ECHO} 
DSPKG_LST="/opt/CA/BABcmagt/dsCheckPkgs.lst"
Uninstall_pkg

if [ "$OS" = "OSF1" ]; then
    rm -f $TMPDIR/pkglist 2> /dev/null
fi

${ECHO}
${ECHO} ${UNINST_MSG_21}
${ECHO}
