#!/sbin/sh
#########################################################################
# 									
#   File:	 decustomize
#   @(#) decustomize $Revision: 1.4 $ $Date: 93/02/26 09:07:39 $
#   Description: /etc/update decustomization script for HPXT2-ADMIN fileset
# 
#########################################################################
#

set -a

PATH="/bin:/usr/sbin:/usr/bin:/etc:/sbin:/usr/etc"
FILESET=HPXT2-ADMIN


OS=`uname -r | cut -d"." -f 2`
if [ "$OS" = "10" -o "$OS" = "11" ]
then
  OS="hp10"
  INSTALLDIR="`echo ${SW_LOCATION} | sed 's!/$!!'`"
  XTHOME=${INSTALLDIR}/xthome
  . /usr/lbin/sw/control_utils
  if [ "$INSTALLDIR" = "/opt/hpxt/enware2" ]
  then
    PANACOMDIR=/opt/hpxt
  else
    PANACOMDIR=$INSTALLDIR
  fi
else
  OS="hp"
  XTHOME="/usr/tftpdir/opt/hpxt/enware2"
fi

ADMINDIR=/opt/hpxt/enware2/lbin

if [ "$OS" = "hp10" ]
then
  if [ -f /usr/bin/dcnodes -o -f /usr/sbin/dcnodes ]
  then
    dcnodes -q
    if [ $? -eq 2 ]
    then
        exit 0
    fi
  fi
fi

if [ "$OS" = "hp" ]
then
  if [ $# -eq 2 ]
  then
    exit 0
  fi
fi

RELEASE=`uname -r | sed -e 's/[^0-9.]//g' -e 's/^\.//' | cut -d. -f1`

#
# remove some files that update is unaware of
#

case $RELEASE in
  10)
    DIR=`echo ${INSTALLDIR}/lbin | sed 's!//!/!g'`
    ;;
  *)
    DIR=/opt/hpxt/enware2/lbin
    ;;
esac

rm -rf $XTHOME.hpxt.*
rm -rf $XTHOME/ENWARE*


#--------- Remove eventual version links---------------#
#
rm -f $XTHOME.* > /dev/null 2>&1
rm -f /opt/hpxt/enware2.* > /dev/null 2>&1

#--------- Remove eventual version trigger file---------------#
#
rm -f /opt/hpxt/enware2/[mM]ulti*version > /dev/null 2>&1


FILES="GATEWAY SUBNETMASK XSESSIONCHOICE env.vars"
for FILE in FILES
do
  if [ -f $DIR/$FILE ]
  then
    rm ${DIR}/$FILE
  fi
done


#
# if HP-UX 9.x remove admin script from sam  "other util" menu
#


if [ $RELEASE = "09" -o $RELEASE = "9" ]
then
  SAMMENU=/usr/sam/custom/xtadm.ou
  rm -f $SAMMENU 2>&1
fi

#
# Remove sam Netstation descriptor file if there
#
rm -f /opt/hpxt/enware2/lbin/sam/C/xtadm.ou

#-------------------------------------------------------
# Remove info in /etc/exports and /etc/inetd.conf
# if ENWARE is not installed
#-------------------------------------------------------
#
if [ "$OS" = "hp10" -a ! -d /opt/hpxt/enware ]
then
  egrep "^$PANACOMDIR" /etc/exports > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    if [ ! -d /opt/hpxt/enware ]
    then
      TMPFILE="/tmp/exports.$$"
      sed "/[/]hpxt/d" /etc/exports > $TMPFILE
      if [ -s $TMPFILE ]
      then
        cp /etc/exports /tmp/exports.bu.$$
        echo "NOTE:    \"$PANACOMDIR\" line removed from /etc/exports"
        echo "         existing \"exports\" saved as /tmp/exports.bu.$$"
        cp $TMPFILE /etc/exports
        rm $TMPFILE
        exportfs -u $PANACOMDIR  > /dev/null 2>&1
        exportfs -a > /dev/null 2>&1
      fi
    fi
  fi

  egrep "$PANACOMDIR" /etc/inetd.conf > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    TMPFILE="/tmp/inetd.conf.$$"
    cat /etc/inetd.conf | sed "/^tftp/s!/opt/hpxt/enware2!!g" \
     | sed "/^tftp/s!$PANACOMDIR!!g" > $TMPFILE
    if [ -s $TMPFILE ]
    then
      cp /etc/inetd.conf /tmp/inetd.conf.bu.$$
      echo "NOTE:    Edited tftp entry in /etc/inetd.conf" >&2
      echo "         The $INSTALLDIR directory was removed from entry." >&2
      echo "         existing \"inetd.conf\" saved in /tmp/inetd.conf.bu.$$"
      cp $TMPFILE /etc/inetd.conf
      rm $TMPFILE
    fi
  fi

  SAMMENU=/usr/sam/lib/C/sam.cb
  egrep -i '^label.*ENWARE' $SAMMENU > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    TMPFILE="/tmp/sam.$$"
    sed '/^label.*HP Netstation Administration/,/command.*xtadm.sam/d' $SAMMENU > $TMPFILE
    if [ -s $TMPFILE ]
    then
      mv $SAMMENU $SAMMENU.bu.$$
      echo "NOTE:    Modifying sam menu .. removing entry for Netstation admin."
      echo "         Existing sam menu file saved as $SAMMENU.bu.$$"
      echo "         To restore old sam menu type:"
      echo "         \"cp $SAMMENU.bu.$$ $SAMMENU\""
      cp $TMPFILE $SAMMENU
    fi
    rm $TMPFILE
  fi

  SAMDIR=$INSTALLDIR/lbin/sam
  if [ -d $SAMDIR ]
  then
    /usr/sam/bin/samreg -u	$SAMDIR
    rm -rf 			$SAMDIR
  fi

fi

#------------------------------------------------------------#
# If ENWARE is installed:
# Restore app-defaults files for ENWARE 
# since it was merged with Netstaion app-defaults directory
#------------------------------------------------------------#
#
$ADMINDIR/xtutil_merge_app-defaults restore


#--------------------------------------------------------------------------##
# remove xtadm symlinks if present
#--------------------------------------------------------------------------##
rm -f /opt/hpxt/enware2/bin/xtadm

rm -f /opt/hpxt/enware2/config/sample.cfg.orig.*
rm -f /opt/hpxt/enware2/config/default.cfg.orig.*
rm -f /opt/hpxt/enware2/xthome/bin/directives.orig.*

#--------------------------------------------------------------------------##
# remove directories (if empty) and links 
# (do this in postremove script for 10.0)
#--------------------------------------------------------------------------##
#
if [ "$OS" != "hp10" ]
then
  find /opt/hpxt/enware2 -depth -type l -exec rm {} \;
  find /opt/hpxt/enware2 -depth -type d -exec rmdir {} \;  > /dev/null 2>&1
fi


exit 0
