#!/bin/ksh
SVCTOOLS_HOME=/usr/opt/hp/svctools
export SVCTOOLS_HOME
WEBES_UPDATE_HOME=`/bin/dirname $0`
export WEBES_UPDATE_HOME
COMPAQ_SVCTOOLS_HOME=/usr/opt/compaq/svctools
export COMPAQ_SVCTOOLS_HOME
WEBESVER=432
export WEBESVER
THROUGH_SCRIPT=1
export THROUGH_SCRIPT
DESTA_CLI=/usr/sbin/desta
export DESTA_CLI
REGISTRY_HOME=${SVCTOOLS_HOME}/specific/desta/config; export REGISTRY_HOME

# These are used to restore existing startup symlinks
DESTA_SYMLINK="false"
CCAT_SYMLINK="false"

find_startup_symlinks()
{
  TEMP="`/sbin/ls /sbin/rc3.d/*desta`"
  [ -n "$TEMP" ] && DESTA_SYMLINK="true"

 if [ -f  /sbin/rc3.d/*ccat ]; then 
    TEMP="`/sbin/ls /sbin/rc3.d/*ccat`" 
    [ -n "$TEMP" ] && CCAT_SYMLINK="true"
 fi

}


restore_startup_symlinks()
{
  OldDir=`/usr/bin/pwd`
  if [ "$DESTA_SYMLINK" = true ]; then
     cd /sbin/rc3.d; /sbin/ln -sf ${SVCTOOLS_HOME}/common/bin/desta_startup ./S89desta
     cd /sbin/rc2.d; /sbin/ln -sf ${SVCTOOLS_HOME}/common/bin/desta_startup ./K01desta
  fi
  if [ "$CCAT_SYMLINK" = true ]; then
     cd /sbin/rc3.d; /sbin/ln -sf ${SVCTOOLS_HOME}/common/bin/wccat_startup ./S99ccat
  fi
  cd $OldDir
}

sed_temp1_registry ()
{
   if [ -f $1 ]; then
      ARGS=$#
      /usr/bin/sed "s/$2.*$//g" $1 | /sbin/grep -v '^ *$' > ./tempfile
      mv ./tempfile $1
      if [ "$ARGS" -eq 3 ]; then
        /usr/bin/echo "$2$3" >> $1
      fi
   fi
}

executeOnRemoteNodes()
{
    for NODE in $MBR_NODENAMES
    do
        if [ "$NODE" = "$FULL_HOSTNAME" ]; then
	    START_FLAG=`/sbin/grep "startOnBoot" $SVCTOOLS_HOME/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
	    if [ "$START_FLAG" = "true" ]; then
                nodeSpecificStuff
	    else
		echo "desta is not configured to start at boot time on $NODE"
	    fi
        else
	    START_FLAG=`rsh -l root $NODE /sbin/grep "startOnBoot" $SVCTOOLS_HOME/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
	    if [ "$START_FLAG" = "true" ]; then
               execute_on_NODE /usr/sbin/desta start
	    else
	       echo "desta is not configured to start at boot time on $NODE"
	    fi
        fi
    done
}

turnOffOnRemoteNodes ()
{
    for NODE in $MBR_NODENAMES
    do
        if [ "$NODE" = "$FULL_HOSTNAME" ]; then
	   echo "Switching off QSAP on $FULL_HOSTNAME..."
	   echo "....executing on $FULL_HOSTNAME"
	   /usr/sbin/desta qsap off silent
        else
	   echo "Switching off QSAP on $NODE..."
           execute_on_NODE /usr/sbin/desta qsap off silent
        fi
    done
}

enroll_with_desta ()
{
#create a ConfigDefaults* file in a node-common directory.

    /sbin/rm -f $SVCTOOLS_HOME/common/desta/config/ConfigDefaults*
    /usr/bin/touch $SVCTOOLS_HOME/common/desta/config/ConfigDefaults${NFNAME}.txt
    /sbin/chmod 744 $SVCTOOLS_HOME/common/desta/config/ConfigDefaults${NFNAME}.txt

    /usr/bin/echo "#
# ConfigDefaults${NFNAME}.txt
#
#       This file enables Automatic Call Handling Service notification (${NTYPE}).
#
com.compaq.svctools.desta.services.notification.${NSNAME}Notification
" >> $SVCTOOLS_HOME/common/desta/config/ConfigDefaults${NFNAME}.txt

    # Now execute the enrollment on all cluster nodes.

    CLU_DIR=/usr/cluster/members
    CFG_DIR=opt/hp/svctools/specific/desta/config
    ENROLL_LOG=$SVCTOOLS_HOME/specific/webes/logs/NotifyEnroll.log
    ENROLL_CMD="$DESTA_CLI exec com.compaq.svctools.desta.configuration.ChangeEnrollments -enroll ConfigDefaults${NFNAME}.txt"

    for NODE in $MBR_NODENAMES
    do
        if [ "$NODE" = "$FULL_HOSTNAME" ]
        then
            /usr/bin/echo "...executing on $FULL_HOSTNAME"
            sed_registry_file notification.Queried "YES"
            /sbin/rm -f $REGISTRY_HOME/ConfigDefaults${NFNAME}.txt
            /sbin/cp -p $SVCTOOLS_HOME/common/desta/config/ConfigDefaults${NFNAME}.txt $REGISTRY_HOME/ConfigDefaults${NFNAME}.txt
           /usr/bin/echo "`$ENROLL_CMD`" > $ENROLL_LOG 2>&1
        else
           # Get the member ID.
          MBR=`/usr/bin/egrep "${NODE}$" $SVCTOOLS_HOME/common/desta/config/CommonFiles/member_nodes | /usr/bin/cut -f 1 -d=`
           if [ -z "$MBR" ]; then
               /usr/bin/echo "setup_notification: unable to translate $NODE to cluster member ID"
               continue
           fi
            /usr/bin/echo "...executing on $NODE"
            sed_remote_registry_file $MBR notification.Queried "YES"
            /sbin/rm -f $CLU_DIR/$MBR/$CFG_DIR/ConfigDefaults${NFNAME}.txt
            /sbin/cp -p $SVCTOOLS_HOME/common/desta/config/ConfigDefaults${NFNAME}.txt $CLU_DIR/$MBR/$CFG_DIR/ConfigDefaults${NFNAME}.txt
            execute_on_NODE $ENROLL_CMD > $ENROLL_LOG 2>&1
        fi

        WILL=`/sbin/grep "will be" $ENROLL_LOG`
        GOOD=`/sbin/grep "successful" $ENROLL_LOG`
        /sbin/rm -f $ENROLL_LOG
        if [ -z "$WILL" -a -z "$GOOD" ]; then
            /usr/bin/echo ""
           /usr/bin/echo "Failed to enable selected notification method"
            /usr/bin/echo ""
            if [ "$NODE" = "$FULL_HOSTNAME" ]; then
                /sbin/rm -f $REGISTRY_HOME/ConfigDefaults${NFNAME}.txt
            else
                /sbin/rm -f $CLU_DIR/$MBR/$CFG_DIR/ConfigDefaults${NFNAME}.txt
            fi
            continue
        fi

        # Only set notification.Type if successfully enrolled.
        if [ "$NODE" = "$FULL_HOSTNAME" ]
        then
            sed_registry_file notification.Type "${NTYPE}"
            if [ "${NTYPE}" = "QSAP" ]; then
                sed_registry_file desta.QSAP "$QSAP_GATEWAY"
                sed_registry_file desta.QSAPport "$QSAP_PORT"
            fi
        else
           sed_remote_registry_file $MBR notification.Type "${NTYPE}"
            if [ "${NTYPE}" = "QSAP" ]; then
                sed_remote_registry_file $MBR desta.QSAP "$QSAP_GATEWAY"
                sed_remote_registry_file $MBR desta.QSAPport "$QSAP_PORT"
            fi
        fi
    done

    /sbin/rm -f $SVCTOOLS_HOME/common/desta/config/ConfigDefaults*
}

nodeSpecificStuff()
{
 if [ -n "$CA_INSTALLED" -o -n "$CCAT_INSTALLED" -o -n "$UNICEN_INSTALLED" ]
   then
    echo "Restarting Director on $FULL_HOSTNAME..."
    desta_process_start EXIT_CODE
    if [ "$EXIT_CODE" -ne 0 ]; then
       echo "Could not start Director on $FULL_HOSTNAME."
       exit 350
    fi
    echo "Waiting 60 seconds for Director to finish startup tasks..."
    /bin/sleep 60
 fi
}

get_installed_unicen ()
{
CUTNUM=7
BASE_PRODUCT="UNICEN[0-9][0-9][0-9]"
INSTALLED_UNICEN="`/sbin/ls ${INSTDIR}/usr/.smdb./${BASE_PRODUCT}*.lk 2> /dev/null | /usr/bin/cut -f 4 -d/ | /usr/bin/cut -c${CUTNUM}- | /usr/bin/cut -f 1 -d.`"
export INSTALLED_UNICEN
INSTALLED_UNICEN_DOT="`/usr/bin/echo $INSTALLED_UNICEN | /usr/bin/sed -n 's/\([0-9]\)\([0-9]\)\([0-9]\)/\1.\2.\3/p'`"
export INSTALLED_UNICEN_DOT
}

get_already_installed_product ()
{
   if [ "$1" = "WCC_INSTALLED" ]; then
      CUTNUM=10
      BASE_PRODUCT="WEBESBASE[0-9][0-9][0-9]"
      NEW_CUTNUM=10
      NEW_BASE_PRODUCT="WEBESBASE[0-9][0-9][0-9]"
   elif [ "$1" = "CCAT_INSTALLED" ]; then
      CUTNUM=9
      BASE_PRODUCT="CCATBASE[0-9][0-9][0-9]"
      NEW_CUTNUM=10
      NEW_BASE_PRODUCT="WEBESCCAT[0-9][0-9][0-9]"
   elif [ "$1" = "DSN_INSTALLED" ]; then
      CUTNUM=9
      BASE_PRODUCT="DSNABASE[0-9][0-9][0-9]"
      NEW_CUTNUM=9
      NEW_BASE_PRODUCT="DSNABASE[0-9][0-9][0-9]"
   else
      CUTNUM=7
      BASE_PRODUCT="CABASE[0-9][0-9][0-9]"
      NEW_CUTNUM=8
      NEW_BASE_PRODUCT="WEBESCA[0-9][0-9][0-9]"
   fi
      SEA_BASE_PRODUCT="WEBESSEA[0-9][0-9][0-9]"
      NEW_CUTNUM_SEA=9
   INSTALLED_PRODUCT="`/sbin/ls ${INSTDIR}/usr/.smdb./${BASE_PRODUCT}*.lk 2> /dev/null | /usr/bin/cut -f 4 -d/ | /usr/bin/cut -c${CUTNUM}- | /usr/bin/cut -f 1 -d.`"
   if [ -z "$INSTALLED_PRODUCT" ]; then
	INSTALLED_PRODUCT="`/sbin/ls ${INSTDIR}/usr/.smdb./${NEW_BASE_PRODUCT}*.lk 2> /dev/null | /usr/bin/cut -f 4 -d/ | /usr/bin/cut -c${NEW_CUTNUM}- | /usr/bin/cut -f 1 -d.`"
   fi
   if [ -z "$INSTALLED_PRODUCT" -a "$1" = "CA_INSTALLED" ]; then
	INSTALLED_PRODUCT="`/sbin/ls ${INSTDIR}/usr/.smdb./${SEA_BASE_PRODUCT}*.lk 2> /dev/null | /usr/bin/cut -f 4 -d/ | /usr/bin/cut -c${NEW_CUTNUM_SEA}- | /usr/bin/cut -f 1 -d.`"
   fi
   
eval $1=${INSTALLED_PRODUCT}
}

get_all_installed_WEBES_products ()
{
    get_already_installed_product WCC_INSTALLED ""; export WCC_INSTALLED
    get_already_installed_product CA_INSTALLED ""; export CA_INSTALLED
    get_already_installed_product CCAT_INSTALLED ""; export CCAT_INSTALLED
}

uninstall_webes ()
{
  # Find any symlinks in the system startup directories
  find_startup_symlinks

  IS_CLUSTER_MEMBER="false"
  if [ -f ${INSTDIR}/usr/sbin/clu_get_info ]; then
     ${INSTDIR}/usr/sbin/clu_get_info -q
     if [ "$?" = "0" ]; then
        IS_CLUSTER_MEMBER="true"
        CLU_DIR=${INSTDIR}/usr/cluster/members
        NODE_DIR=opt/hp/svctools
	NODE_DIR_COMPAQ=opt/compaq/svctools
     fi
  fi
  if [ -f ${COMPAQ_SVCTOOLS_HOME}/specific/desta/config/DESTA.REG ];then
      NOTIFICATION_FLAG=`/sbin/grep "notification.Queried" ${COMPAQ_SVCTOOLS_HOME}/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
      export NOTIFICATION_FLAG
      NOTIFICATION_TYPE=`/sbin/grep "notification.Type" ${COMPAQ_SVCTOOLS_HOME}/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
      export NOTIFICATION_TYPE
      WEBES_CFGSTATE=`/sbin/grep "webes.CfgState" ${COMPAQ_SVCTOOLS_HOME}/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
      export WEBES_CFGSTATE
      KIT_PATH=`/sbin/grep "kit.path" ${COMPAQ_SVCTOOLS_HOME}/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
      export KIT_PATH
  elif [ -f ${SVCTOOLS_HOME}/specific/desta/config/DESTA.REG ];then
      NOTIFICATION_FLAG=`/sbin/grep "notification.Queried" ${SVCTOOLS_HOME}/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
      export NOTIFICATION_FLAG
      NOTIFICATION_TYPE=`/sbin/grep "notification.Type" ${SVCTOOLS_HOME}/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
      export NOTIFICATION_TYPE
      WEBES_CFGSTATE=`/sbin/grep "webes.CfgState" ${SVCTOOLS_HOME}/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
      export WEBES_CFGSTATE
      KIT_PATH=`/sbin/grep "kit.path" ${SVCTOOLS_HOME}/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
      export KIT_PATH
  fi
  if [ "$NOTIFICATION_TYPE" = "SICL" ];then
     CONFIG_DEF_FILE=ConfigDefaultsACHS.txt
  elif [ "$NOTIFICATION_TYPE" = "QSAP" ];then
     CONFIG_DEF_FILE=ConfigDefaultsQSAP.txt
  elif [ "$NOTIFICATION_TYPE" = "QSAP SICL" -o "$NOTIFICATION_TYPE" = "SICL QSAP" ]; then
     CONFIG_DEF_FILE="ConfigDefaultsQSAP.txt ConfigDefaultsACHS.txt"
  fi

  filename="${COMPAQ_SVCTOOLS_HOME}/common/desta/config/CommonFiles/SystemState"
  if [ ! -f $filename ];then
     filename="${SVCTOOLS_HOME}/common/desta/config/CommonFiles/SystemState"
  fi
  if [ -f $filename ]; then
      stringname=ca.configure
      GR_ITEM=""
      GR_ITEM=`/sbin/grep "$stringname" $filename | /usr/bin/cut -f 2 -d=`
      if [ "$GR_ITEM" = "" ]; then
	    GR_ITEM="NOT_INSTALLED"
      fi
      if [ "$GR_ITEM" = "CONFIGURED" ]; then

	 if [ ! -e $SVCTOOLS_HOME ]; then
	       /usr/bin/mkdir -p $SVCTOOLS_HOME
	 fi

	 if [ -f "${COMPAQ_SVCTOOLS_HOME}/common/ca/config/ACHSInfo" ]; then
	    /usr/bin/cp ${COMPAQ_SVCTOOLS_HOME}/common/ca/config/ACHSInfo ${SVCTOOLS_HOME}
	 elif [ -f "${SVCTOOLS_HOME}/common/ca/config/ACHSInfo" ]; then
	    /usr/bin/cp ${SVCTOOLS_HOME}/common/ca/config/ACHSInfo ${SVCTOOLS_HOME}
	 fi
	 ENABLE_DESTA=1
         if [ "$IS_CLUSTER_MEMBER" = "true" ]; then
            TOUCH_DIR=`/sbin/ls -1 $CLU_DIR`
	    for DIR in ${TOUCH_DIR}; do
		if [ ! -e $CLU_DIR/$DIR/$NODE_DIR/specific/ca/save ]; then
                   /usr/bin/mkdir -p $CLU_DIR/$DIR/$NODE_DIR/specific/ca/save
	    	fi
	    done
            for DIR in ${TOUCH_DIR}; do
               /usr/bin/touch $CLU_DIR/$DIR/$NODE_DIR/specific/ca/save/ca_configured
	    if [ -f $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/desta/config/NotifyCA.txt ];then
               /usr/bin/cp $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/desta/config/NotifyCA.txt $CLU_DIR/$DIR/$NODE_DIR/specific/ca/save
	    elif [ -f $CLU_DIR/$DIR/$NODE_DIR/specific/desta/config/NotifyCA.txt ];then
	       /usr/bin/cp $CLU_DIR/$DIR/$NODE_DIR/specific/desta/config/NotifyCA.txt $CLU_DIR/$DIR/$NODE_DIR/specific/ca/save
	    fi
            done
          else
	    if [ ! -e $SVCTOOLS_HOME/specific/ca/save ]; then
	       /usr/bin/mkdir -p $SVCTOOLS_HOME/specific/ca/save
	    fi
            /usr/bin/touch $SVCTOOLS_HOME/specific/ca/save/ca_configured
	    if [ -f ${COMPAQ_SVCTOOLS_HOME}/specific/desta/config/NotifyCA.txt ]; then
	      /usr/bin/cp ${COMPAQ_SVCTOOLS_HOME}/specific/desta/config/NotifyCA.txt ${SVCTOOLS_HOME}/specific/ca/save
	    elif [ -f ${SVCTOOLS_HOME}/specific/desta/config/NotifyCA.txt ];then
	      /usr/bin/cp ${SVCTOOLS_HOME}/specific/desta/config/NotifyCA.txt ${SVCTOOLS_HOME}/specific/ca/save
	    fi
          fi
   fi
       stringname=ccat.configure
       GR_ITEM=""
       GR_ITEM=`/sbin/grep "$stringname" $filename | /usr/bin/cut -f 2 -d=`
       if [ "$GR_ITEM" = "" ]; then
	    GR_ITEM="NOT_INSTALLED"
       fi
       if [ "$GR_ITEM" = "CONFIGURED" ]; then
	  ENABLE_DESTA=1
          if [ "$IS_CLUSTER_MEMBER" = "true" ]; then
		if [ ! -e $CLU_DIR/member0/$NODE_DIR/specific/ccat/save ]; then
                   /usr/bin/mkdir -p $CLU_DIR/member0/$NODE_DIR/specific/ccat/save
	        fi
             TOUCH_DIR=`/sbin/ls -1 $CLU_DIR`
	     for DIR in ${TOUCH_DIR}; do
		if [ ! -e $CLU_DIR/$DIR/$NODE_DIR/specific/ccat/save ]; then
                   /usr/bin/mkdir -p $CLU_DIR/$DIR/$NODE_DIR/specific/ccat/save
	        fi
	     done
             for DIR in ${TOUCH_DIR}; do
                /usr/bin/touch $CLU_DIR/$DIR/$NODE_DIR/specific/ccat/save/ccat_configured
		if [ -f $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/desta/config/CCATNotify.txt ]; then
                   /usr/bin/cp $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/desta/config/CCATNotify.txt $CLU_DIR/$DIR/$NODE_DIR/specific/ccat/save
		elif [ -f $CLU_DIR/$DIR/$NODE_DIR/specific/desta/config/CCATNotify.txt ]; then
		  /usr/bin/cp $CLU_DIR/$DIR/$NODE_DIR/specific/desta/config/CCATNotify.txt $CLU_DIR/$DIR/$NODE_DIR/specific/ccat/save
		fi
             done
          else
	     if [ ! -e $SVCTOOLS_HOME/specific/ccat/save ]; then
	        /usr/bin/mkdir -p $SVCTOOLS_HOME/specific/ccat/save
	     fi
             /usr/bin/touch ${SVCTOOLS_HOME}/specific/ccat/save/ccat_configured
		if [ -f ${COMPAQ_SVCTOOLS_HOME}/specific/desta/config/CCATNotify.txt ]; then
	           /usr/bin/cp ${COMPAQ_SVCTOOLS_HOME}/specific/desta/config/CCATNotify.txt ${SVCTOOLS_HOME}/specific/ccat/save
		elif [ -f ${SVCTOOLS_HOME}/specific/desta/config/CCATNotify.txt ]; then
	           /usr/bin/cp ${SVCTOOLS_HOME}/specific/desta/config/CCATNotify.txt ${SVCTOOLS_HOME}/specific/ccat/save
		fi
          fi
        fi
        stringname=system.state
        GR_ITEM=""
        GR_ITEM=`/sbin/grep "$stringname" $filename | /usr/bin/cut -f 2 -d=`
        if [ "$GR_ITEM" = "" ]; then
	    GR_ITEM="NOT_INSTALLED"
        fi
        if [ "$GR_ITEM" = "CONFIGURED" ]; then
           if [ "$IS_CLUSTER_MEMBER" = "true" ]; then
              TOUCH_DIR=`/sbin/ls -1 $CLU_DIR`
	      for DIR in ${TOUCH_DIR}; do
	        if [ ! -e $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata ]; then
                   /usr/bin/mkdir -p $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata
                fi
              done
              for DIR in ${TOUCH_DIR}; do
                 /usr/bin/touch $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata/desta_configured
                 COP_FILE="profile.txt Configuration.dat DESTA.REG $CONFIG_DEF_FILE"
                 for FILE in ${COP_FILE}; do
                    if [ -f $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/desta/config/$FILE ]; then
			/usr/bin/cp $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/desta/config/$FILE $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata
		    elif [ -f $CLU_DIR/$DIR/$NODE_DIR/specific/desta/config/$FILE ]; then
			/usr/bin/cp $CLU_DIR/$DIR/$NODE_DIR/specific/desta/config/$FILE $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata
                    fi
                 done
                 if [ -f $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/desta/data/Obligation ]; then
                    /usr/bin/cp $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/desta/data/Obligation $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata
		 elif [ -f $CLU_DIR/$DIR/$NODE_DIR/specific/desta/data/Obligation ]; then
		    /usr/bin/cp $CLU_DIR/$DIR/$NODE_DIR/specific/desta/data/Obligation $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata
                 fi
              done
            else
               if [ ! -e ${SVCTOOLS_HOME}/specific/desta/userdata ]; then
                  /usr/bin/mkdir -p ${SVCTOOLS_HOME}/specific/desta/userdata
               fi
              /usr/bin/touch ${SVCTOOLS_HOME}/specific/desta/userdata/desta_configured
              COP_FILE="profile.txt Configuration.dat DESTA.REG $CONFIG_DEF_FILE"
              for FILE in ${COP_FILE}; do
                 if [ -f $COMPAQ_SVCTOOLS_HOME/specific/desta/config/$FILE ]; then
                    /usr/bin/cp $COMPAQ_SVCTOOLS_HOME/specific/desta/config/$FILE $SVCTOOLS_HOME/specific/desta/userdata
		 elif [ $SVCTOOLS_HOME/specific/desta/config/$FILE ]; then
		    /usr/bin/cp $SVCTOOLS_HOME/specific/desta/config/$FILE $SVCTOOLS_HOME/specific/desta/userdata
                 fi
              done
              if [ -f $COMPAQ_SVCTOOLS_HOME/specific/desta/data/Obligation ]; then
                 /usr/bin/cp $COMPAQ_SVCTOOLS_HOME/specific/desta/data/Obligation $SVCTOOLS_HOME/specific/desta/userdata
	      elif [ -f $SVCTOOLS_HOME/specific/desta/data/Obligation ]; then
		 /usr/bin/cp $SVCTOOLS_HOME/specific/desta/data/Obligation $SVCTOOLS_HOME/specific/desta/userdata
              fi
             fi
           fi
   fi

# Fix for PTR 91-22-1631

userInfo_filename="${COMPAQ_SVCTOOLS_HOME}/common/desta/config/CommonFiles/UserInformation"
  if [ ! -f $userInfo_filename ];then
     userInfo_filename="${SVCTOOLS_HOME}/common/desta/config/CommonFiles/UserInformation"
  fi

  if [ -f $userInfo_filename ]; then

           if [ ! -e $SVCTOOLS_HOME ]; then
	       /usr/bin/mkdir -p $SVCTOOLS_HOME
	   fi

	   if [ -f "${COMPAQ_SVCTOOLS_HOME}/common/desta/config/CommonFiles/UserInformation" ]; then
	      /usr/bin/cp ${COMPAQ_SVCTOOLS_HOME}/common/desta/config/CommonFiles/UserInformation ${SVCTOOLS_HOME}
	   elif [ -f "${SVCTOOLS_HOME}/common/desta/config/CommonFiles/UserInformation" ]; then
	     /usr/bin/cp ${SVCTOOLS_HOME}/common/desta/config/CommonFiles/UserInformation ${SVCTOOLS_HOME}
	   fi
   fi

# End of fix for PTR 91-22-1631

/usr/bin/echo ""
/usr/bin/echo " WEBESBASE${INSTALLEDVER} will be uninstalled "
/usr/bin/echo ""
   if [ "$NEED_TO_DISPLAY_RCM_MESSAGE" = "1" ]; then
      get_installed_unicen
       if [ -f /usr/.smdb./CT5BASE[0-9][0-9][0-9].lk ]; then
          if [ "${INSTALLED_UNICEN}" -lt "500" ]; then
           /sbin/cat << EOF

You will be prompted by RCM's CT5 Reader Tool to approve removal.
This is normal, please enter [y]es.

EOF
	  fi
       fi
   fi
   get_all_installed_WEBES_products
   #if [ "$CA_INSTALLED" = "410" ]; then
   #   UNINSTALL_ANY_CA=CABASE$CA_INSTALLED
   #elif [ "$CA_INSTALLED" = "411" ]; then
   #   UNINSTALL_ANY_CA=WEBESCA$CA_INSTALLED
   #elif [ "$CA_INSTALLED" = "420" ]; then
   #   UNINSTALL_ANY_CA=WEBESSEA$CA_INSTALLED

   if [ "$CA_INSTALLED" = "430" -o "$CA_INSTALLED" = "431" ]; then
      UNINSTALL_ANY_CA=WEBESSEA$CA_INSTALLED
   fi
   if [ "$CCAT_INSTALLED" = "500" ]; then
      UNINSTALL_ANY_CCAT=CCATBASE$CCAT_INSTALLED
   elif [ "$CCAT_INSTALLED" = "501" -o "$CCAT_INSTALLED" = "510" -o "$CCAT_INSTALLED" = "511" ]; then
      UNINSTALL_ANY_CCAT=WEBESCCAT$CCAT_INSTALLED
   fi
#echo "changing SVCTOOLS_HOME"
   if [ "$WCC_INSTALLED" -lt "420" ];then
      SVCTOOLS_HOME=/usr/opt/compaq/svctools
   fi
   if [ -n "$UNINSTALL_ANY_CCAT" ];then
        /usr/sbin/setld -d $UNINSTALL_ANY_CCAT
   fi
   if [ -n "$UNINSTALL_ANY_CA" ];then
        /usr/sbin/setld -d $UNINSTALL_ANY_CA
   fi
   /usr/sbin/setld -d WEBESBASE${INSTALLEDVER}

   if [ $? != 0 ]; then
      if [ "$IS_CLUSTER_MEMBER" = "true" ]; then
         TOUCH_DIR=`/sbin/ls -1 $CLU_DIR`
         for DIR in ${TOUCH_DIR}; do
             /usr/bin/rm -f $CLU_DIR/$DIR/$NODE_DIR/specific/ca/save/NotifyCA.txt
	     /usr/bin/rm -f $CLU_DIR/$DIR/$NODE_DIR/specific/ccat/save/CCATNotify.txt
	     /usr/bin/rm -f $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata/Obligation
	     /usr/bin/rm -f $CLU_DIR/$DIR/$NODE_DIR/specific/ca/save/ca_configured
	     /usr/bin/rm -f $CLU_DIR/$DIR/$NODE_DIR/specific/ccat/save/ccat_configured
	     /usr/bin/rm -f $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata/Configuration.dat
	     /usr/bin/rm -rf $CLU_DIR/$DIR/$NODE_DIR/specific/ca/save/auto
	 done
      else
         SVCTOOLS_HOME=/usr/opt/hp/svctools           
         /usr/bin/rm -f ${SVCTOOLS_HOME}/specific/ca/save/NotifyCA.txt
         /usr/bin/rm -f ${SVCTOOLS_HOME}/specific/ccat/save/CCATNotify.txt
         /usr/bin/rm -f ${SVCTOOLS_HOME}/specific/desta/userdata/Obligation
         /usr/bin/rm -f ${SVCTOOLS_HOME}/specific/ca/save/ca_configured
         /usr/bin/rm -f ${SVCTOOLS_HOME}/specific/ccat/save/ccat_configured
         /usr/bin/rm -f ${SVCTOOLS_HOME}/specific/desta/userdata/desta_configured
	 /usr/bin/rm -f $SVCTOOLS_HOME/specific/desta/userdata/Configuration.dat
	 /usr/bin/rm -rf ${SVCTOOLS_HOME}/specific/ca/save/auto
         /usr/bin/rm -f ${SVCTOOLS_HOME}/UserInformation
      fi	 
      exit 1
   fi    
SVCTOOLS_HOME=/usr/opt/hp/svctools
}

check_if_WEBES_installed ()
{
   INSTALLED=""
   INSTALLEDVER=""
   INSTALLEDDOTVER=""
   if [ -f ${INSTDIR}/usr/.smdb./WEBESBASE[0-9][0-9][0-9].lk ]
   then
      INSTALLED="true"
      INSTALLEDVER="`/sbin/ls ${INSTDIR}/usr/.smdb./WEBESBASE*.lk 2> /dev/null | /usr/bin/cut -f 4 -d\/ | /usr/bin/cut -f 1 -d\. | /usr/bin/cut -c10- `"
      INSTALLEDDOTVER="`/sbin/ls ${INSTDIR}/usr/.smdb./WEBESBASE*.lk 2> /dev/null | /usr/bin/cut -c13- | /usr/bin/cut -f 1 -d. | /usr/bin/cut -c10- | /usr/bin/sed -n 's/\([0-9]\)\([0-9]\)\([0-9]\)/\1.\2.\3/p'`"
   fi
   if [ -n "${INSTALLED}" ]; then
      if [ "${INSTALLEDVER}" -eq "${WEBESVER}" ]; then
	  /sbin/cat <<EOF
This procedure can be used to install/upgrade WEBES (${INSTALLEDDOTVER}). But
Web-Based Enterprise Service Suite (${INSTALLEDDOTVER}) is already installed.
Please use webes_install_update or setld command directly to reinstall/uninstall
any optional subset of WEBES ${INSTALLEDDOTVER}.
EOF
	exit 0
      fi
      if [ "${INSTALLEDVER}" -lt "${WEBESVER}" ]; then
        if [ "${INSTALLEDVER}" -eq "430" -o "${INSTALLEDVER}" -eq "431"  ]; then  
        #if [ "${INSTALLEDVER}" -eq "410" -o "${INSTALLEDVER}" -eq "411" -o "${INSTALLEDVER}" -eq "420" -o "${INSTALLEDVER}" -eq "430"  ]; then
	   /sbin/cat << EOF

Webes V${INSTALLEDVER} is currently installed.

This script will deinstall V${INSTALLEDVER} and migrate its configuration data
to the V4.3.2 installation.

EOF
           echo "Do you wish to upgrade to WEBES V4.3.2 ([y]/n)?:\c"
           read input
           if [ "$input" = "" ]; then
              uninstall_webes
           elif [ ! -z "$input" ]; then
              input=`echo $input | tr [A-Z]  [a-z]`
              if [ "$input" = "yes" -o "$input" = "y" ]; then
                uninstall_webes
              else
                exit 0
              fi
           fi
        else
	/sbin/cat <<EOF
An older version of the Web-Based Enterprise Service Suite (${INSTALLEDDOTVER})
is currently installed. That version must be deinstalled before 
this procedure can be executed.
Please use webes_install_update or setld command directly to uninstall WEBES.
EOF
                exit 1
        fi
      fi
      if [ -n "${INSTALLEDVER}" ]; then
        if [ "${INSTALLEDVER}" -eq "430" -o "${INSTALLEDVER}" -eq "431"  ]; then
       # if [ "${INSTALLEDVER}" -eq "410" -o "${INSTALLEDVER}" -eq "411" -o "${INSTALLEDVER}" -eq "420" -o "${INSTALLEDVER}" -eq "430" ]; then
                continue
        else
	        /sbin/cat <<EOF
A newer version of the Web-Based Enterprise Service Suite (${INSTALLEDDOTVER})
is currently installed. That version must be deinstalled before 
this procedure can be executed.
EOF
	        exit 1
        fi
      fi
  fi
}

###########################################################################
# Start of script execution

get_all_installed_WEBES_products
if [ -n "$WCC_INSTALLED" ]; then
    NEW_WCC=WEBESBASE432
    NEED_TO_DISPLAY_RCM_MESSAGE=1
fi
if [ -n "$CA_INSTALLED" ]; then
    NEW_CA=WEBESSEA432
fi
if [ -n "$CCAT_INSTALLED" ]; then
    NEW_CCAT=WEBESCCAT511
fi
   if [ "$NEED_TO_DISPLAY_RCM_MESSAGE" = "1" ]; then
      get_installed_unicen 
       if [ -f /usr/.smdb./CT5BASE[0-9][0-9][0-9].lk ]; then
          if [ "${INSTALLED_UNICEN}" -lt "500" ]; then
	   /sbin/cat << EOF

This WEBES kit does not include a compatible version of RCM. Should
you wish to install RCM, the latest kit may be downloaded from
http://www.support.compaq.com/svctools/rcm where full documentation is also
available.

EOF
          fi
       fi      
   fi

# This subroutine will check for installed Webes, then call uninstall_webes

check_if_WEBES_installed

# Now install the new version of Webes.

/usr/sbin/setld -l $WEBES_UPDATE_HOME $NEW_WCC $NEW_CA $NEW_CCAT

# Restore the symlinks in the system startup directories

restore_startup_symlinks

if [ "$ENABLE_DESTA" = "1" ]; then
   /sbin/ln -sf ${SVCTOOLS_HOME}/common/bin/desta /usr/sbin/desta
   if [ -f $SVCTOOLS_HOME/common/install/wcc_install_432.sh ]; then
      . $SVCTOOLS_HOME/common/install/wcc_install_432.sh
   else
      echo "could not find the required file"
      exit 0
   fi
   get_hostnames
   isClusterMember
   if [ -n "$IS_CLUSTER_MEMBER" ]; then
      get_registry_item common.ClusterID; MY_MEMBER_ID="$GR_ITEM"
      get_other_cluster_members
      get_cluster_hostnames
   else
      MY_MEMBER_ID="member0"
      MBR_NODENAMES=$FULL_HOSTNAME
      OTHER_MBRS_LIST=""
   fi
   if [ "$IS_CLUSTER_MEMBER" = "true" ]; then
      CLU_DIR=/usr/cluster/members
      NODE_DIR=opt/hp/svctools
      TOUCH_DIR=`/sbin/ls -1 $CLU_DIR`
      for DIR in ${TOUCH_DIR}; do
	 if [ -f $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata/DESTA.REG ]
         then
	   GR_ITEM=`/sbin/grep "webes.startOnBoot" $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata/DESTA.REG | /usr/bin/cut -f 2 -d=`
	   if [ "$GR_ITEM" = "" ]; then
	      continue
	   else
	      sed_temp_registry $CLU_DIR/$DIR/$NODE_DIR/specific/desta/config/DESTA.REG webes.startOnBoot "$GR_ITEM"
	   fi
	   GR_ITEM=`/sbin/grep "desta.QSAPport" $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata/DESTA.REG | /usr/bin/cut -f 2 -d=`
	   if [ "$GR_ITEM" = "" ]; then
	      continue
	   else
	      sed_temp_registry $CLU_DIR/$DIR/$NODE_DIR/specific/desta/config/DESTA.REG desta.QSAPport "$GR_ITEM"
	   fi
	   GR_ITEM=`/sbin/grep "desta.QSAP=" $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata/DESTA.REG | /usr/bin/cut -f 2 -d=`
	   if [ "$GR_ITEM" = "" ]; then
	      continue
	   else
	      sed_temp1_registry $CLU_DIR/$DIR/$NODE_DIR/specific/desta/config/DESTA.REG desta.QSAP= "$GR_ITEM"
	   fi
	   rm -f $CLU_DIR/$DIR/$NODE_DIR/specific/desta/userdata/DESTA.REG
	  fi
      done
      if [ -f $CLU_DIR/member0/$NODE_DIR/specific/desta/userdata/DESTA.REG ]
      then
	  rm -f $CLU_DIR/member0/$NODE_DIR/specific/desta/userdata/DESTA.REG
      fi
     if [ "$IS_CLUSTER_MEMBER" = "true" ]; then
      CLU_DIR=/usr/cluster/members
      NODE_DIR_COMPAQ=opt/compaq/svctools
      TOUCH_DIR=`/sbin/ls -1 $CLU_DIR`
      for DIR in ${TOUCH_DIR}; do
        if [ -f $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/ca/save/*.wup ]; then
         if [ -e $CLU_DIR/$DIR/$NODE_DIR/specific/ca/html/profiles ]
         then
           /usr/bin/cp -p $CLU_DIR/$DIR/$NODE_DIR_COMPAQ/specific/ca/save/*.wup $CLU_DIR/$DIR/$NODE_DIR/specific/ca/html/profiles  
         fi
        fi
      done
     fi
   else
     if [ -f /usr/opt/compaq/svctools/specific/ca/save/*.wup ]; then
      if [ -e $SVCTOOLS_HOME/specific/ca/html/profiles ]
      then
/usr/bin/cp -p /usr/opt/compaq/svctools/specific/ca/save/*.wup $SVCTOOLS_HOME/specific/ca/html/profiles
      fi
     fi
      if [ -f $SVCTOOLS_HOME/specific/desta/userdata/DESTA.REG ]
      then
	  GR_ITEM=`/sbin/grep "startOnBoot" $SVCTOOLS_HOME/specific/desta/userdata/DESTA.REG | /usr/bin/cut -f 2 -d=`
	  if [ "$GR_ITEM" = "" ]; then
	     continue
	  else
	     sed_temp_registry $SVCTOOLS_HOME/specific/desta/config/DESTA.REG webes.startOnBoot "$GR_ITEM"
	  fi
	   GR_ITEM=`/sbin/grep "desta.QSAPport" $SVCTOOLS_HOME/specific/desta/userdata/DESTA.REG | /usr/bin/cut -f 2 -d=`
	   if [ "$GR_ITEM" = "" ]; then
	      continue
	   else
	      sed_temp_registry $SVCTOOLS_HOME/specific/desta/config/DESTA.REG desta.QSAPport "$GR_ITEM"
	   fi
	   GR_ITEM=`/sbin/grep "desta.QSAP=" $SVCTOOLS_HOME/specific/desta/userdata/DESTA.REG | /usr/bin/cut -f 2 -d=`
	   if [ "$GR_ITEM" = "" ]; then
	      continue
	   else
	      sed_temp1_registry $SVCTOOLS_HOME/specific/desta/config/DESTA.REG desta.QSAP= "$GR_ITEM"
	   fi
	  rm -f $SVCTOOLS_HOME/specific/desta/userdata/DESTA.REG
      fi
   fi
    get_registry_item notification.Type; TYPE="$GR_ITEM"
    get_supported_ISEE
    if [ -n "$ISEE_INSTALLED" -a "$TYPE" = "QSAP" ]; then
        echo "...configuring available Service Provider Notification methods:"
	echo ""
        echo " Note: Instant Support Enterprise Edition (ISEE) is installed, andwill be used."
	echo "CSG was turned on earlier and will be turned off now"
	echo ""
	if [ -n "$IS_CLUSTER_MEMBER" ]; then
	   turnOffOnRemoteNodes
	else
	   echo "Switching off QSAP on $FULL_HOSTNAME..." 
	   /usr/sbin/desta qsap off silent
	fi
        NFNAME="ISEE"; NSNAME="ISEE"; NTYPE="ISEE"
        enroll_with_desta
    elif [ -n "$ISEE_INSTALLED" -a "$TYPE" = "SICL" ]; then
        echo "...configuring available Service Provider Notification methods:"
	echo ""
        echo " Note: Instant Support Enterprise Edition (ISEE) is installed, andwill be used."
	echo "SICL was turned on earlier and will be used along with ISEE"
	echo "So will get duplicate problem reports from SICL and ISEE"
	echo ""
        NFNAME="ISEE"; NSNAME="ISEE"; NTYPE="ISEE SICL"
	enroll_with_desta
    elif [ -n "$ISEE_INSTALLED" -a "$TYPE" = "NONE" ];then
        echo "...configuring available Service Provider Notification methods:"
	echo ""
        echo " Note: Instant Support Enterprise Edition (ISEE) is installed, andwill be used."
	echo ""
        NFNAME="ISEE"; NSNAME="ISEE"; NTYPE="ISEE"
        enroll_with_desta
    fi
   get_all_installed_products
   if [ -n "$IS_CLUSTER_MEMBER" ]; then
      executeOnRemoteNodes
   else
      START_FLAG=`/sbin/grep "startOnBoot" $SVCTOOLS_HOME/specific/desta/config/DESTA.REG | /usr/bin/cut -f 2 -d=`
      if [ "$START_FLAG" = "true" ]; then
          nodeSpecificStuff
      else
     	 echo "desta is not configured to start at boot time on this machine"
      fi
   fi
fi
