#!/sbin/sh

#########################################################################
# 									
#   File:	 customize
#   @(#) customize $Revision: 1.4 $ $Date: 93/05/04 14:17:19 $
#   Description: /etc/update customization script for HPXT-SUPPL fileset
# 
#########################################################################
#

umask 022
set -a

PATH="/bin:/usr/bin:/etc:/usr/sbin:."
FILESET=HPXT-SUPPL
umask 22


RESTARTINETD="false"

BU=0			# keep track of how many backups we make

DIR700X=/usr/lib/X11/700X

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
  BOOTPD=/usr/lbin/bootpd
  TFTPD=/usr/lbin/tftpd
  INETD=/usr/sbin/inetd
  ADMINDIR=${INSTALLDIR}/lbin
else
  OS="hp"
  INSTALLDIR=/usr/tftpdir
  XTHOME=${INSTALLDIR}${DIR700X}
  BOOTPD=/etc/bootpd
  TFTPD=/etc/tftpd
  INETD=/etc/inetd
  ADMINDIR=/usr/bin/X11/700X/admin
fi

WSFONTDIR=/usr/lib/X11/fonts
if [ "$INSTALLDIR" = "/opt/hpxt/enware" ]
then
  PANACOMDIR=/opt/hpxt
else
  PANACOMDIR=$INSTALLDIR 
fi

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


  CONTROL_UTILS=/usr/lbin/sw/control_utils
  if [ ! -f $CONTROL_UTILS ]
  then
    echo "ERROR:   Cannot find $CONTROL_UTILS" 
    exit 1
  fi
  set +u
  . $CONTROL_UTILS
  set -u
  PATH=$PATH:${ADMINDIR}
else
  PATH=$PATH:${ADMINDIR}
fi


#
#------------------------------------------------------------------------#
# Create a file that can be sourced by other scripts
#------------------------------------------------------------------------#
#

echo "OS=$OS" > /tmp/env.vars
echo "XTHOME=$XTHOME" >> /tmp/env.vars
echo "INSTALLDIR=$INSTALLDIR" >> /tmp/env.vars
. ${ADMINDIR}/HPXTVERSION
NEWDIR=${XTHOME}.$HPXTVERSION
echo "HPXTVERSION=$HPXTVERSION" >> /tmp/env.vars
echo "NEWDIR=$NEWDIR" >> /tmp/env.vars
echo "ADMINDIR=$ADMINDIR" >> /tmp/env.vars
chmod 644 /tmp/env.vars
cp /tmp/env.vars ${ADMINDIR}/env.vars

xtutil_add-file ${ADMINDIR}/env.vars


if [ "$OS" = "hp" ]
then
  ARCH=$1
  ARCHEXT=""
  case $ARCH in
    HP-MC68*)
	if [ ! hp9000s300 ]
	then
	    ARCHEXT="+/$ARCH"
	fi
	;;
  esac
fi

#############################################
##
## owner_group
##
## Usage: owner_group <file>
##
## Get the owner and group of a file (in numeric form).
##
#############################################
owner_group()
{
    set -- `ls -ln "$1"`

    echo "$3" "$4"
}

#############################################
##
## chmog
##
## Usage: chmog <mode> <owner> <group> <file ...>
##
##     change mode, owner and group on the specified files
##     If mode is 0, the mode is left unchanged
##     if any value is the null string, that value is not changed.
##
#############################################
chmog()
{
	if [ $# -lt 4 ]; then
		echo "\
ERROR:   Internal error in customize script.
         Invalid call to function \"$0\"."  >&2
	 
		EXIT_VALUE=1;
		return 1
	fi

	mode="$1"
	owner="$2"
	group="$3"
	shift 3
	exitval=0

	if [ -n "$group" ]; then
	        chgrp $group $* || exitval=1
	fi

	if [ -n "$owner" ]; then
	        chown $owner $* || exitval=1
	fi

	if [ -n "$mode" -a "$mode" != 0 ]; then
	        chmod $mode  $* || exitval=1
	fi

	if [ $exitval -ne 0 ]; then
		echo "\
WARNING: Cannot set permissions for \"$*\"." >&2
	fi
}

#############################################
##
## cond_copy
##
## Usage: cond_copy <src> <dest>
##
## Copy $1 to $2 iff $2 doesn't exist.   The mode owner and group of
## the source are retained.
##
#############################################
cond_copy()
{
	if [ $# -ne 2 ]; then
		echo "\
ERROR:   Internal error in customize script.
         Invalid call to function \"$0\"."       >&2
	 
		EXIT_VALUE=1;
		return 1
	fi
	
	SRC=$1
	DEST=$2
	ownership="`owner_group $SRC`"

	# Check for existing destination file or directory
	[ -f $DEST ] && return 1
	[ -d $DEST ] && return 1

	if cp $SRC $DEST; then
		chmog 0 $ownership "$DEST"
		xtutil_add-file $DEST
	else
		echo "\
ERROR:   Cannot copy \"$SRC\" to \"$DEST\"." >&2

		EXIT_VALUE=1
		return 1
	fi

}



BOOTPLINE="bootps dgram	udp	wait	root	$BOOTPD	bootpd"

BOOTPS="bootps        67/udp                 # Bootstrap Protocol Server"
BOOTPC="bootpc        68/udp                 # Bootstrap Protocol Client"

#
# Test existence of a file in /etc --
# -- if the file not found, get a copy from /etc/newconfig or /usr/newconfig
# -- if the file still not found, return FAILED
#
Exists()
{
  Root=etc
  etc_new_file=${Root}/newconfig/${1}
  etc_file=/${Root}/${1}

  if [ $OS = "hp10" ]
  then
    etc_new_file=/usr/newconfig/$Root/${1}
  fi

  if [ ! -f $etc_file ]
  then
     if [ ! -f $etc_new_file ]
     then return 1
     fi
     if cp $etc_new_file $etc_file
     then return 0
     fi
     return 1
  fi
  return 0
}


#
#------------------------------------------------------------------------#
# Enable bootp service if not already enabled
#------------------------------------------------------------------------#
#

#
# check for inetd.conf and services
#
if Exists inetd.conf && Exists services
then
  :
else
   echo "ERROR:   Could not find /etc/inetd.conf or /etc/services" >&2
   echo "         ARPA fileset must be installed first. " >&2
   echo "         Update of BOOTP failed.               " >&2
   exit 1
fi

#
# First just attempt to remove an eventual comment
#
egrep '^bootps' /etc/inetd.conf > /dev/null
if [ $? -ne 0 ]
then
  egrep '^#bootps' /etc/inetd.conf > /dev/null
  if [ $? -eq 0 ]
  then
    BU=`expr $BU + 1`
    cp /etc/inetd.conf /tmp/inetd.conf.bu${BU}.$$
    (echo '/^#bootps/s/#bootps/bootps/'; echo 'w!') | ex - /etc/inetd.conf 
    if [ -s /etc/inetd.conf ]
    then
      echo "NOTE:    uncommented bootp entry in /etc/inetd.conf" >&2
      echo "         existing \"inetd.conf\" saved as /tmp/inetd.conf.bu${BU}.$$"
      RESTARTINETD="true"
    else
      BU=`expr $BU + 1`
      cp /tmp/inetd.conf.bu${BU}.$$ /etc/inetd.conf
      echo "NOTE:    Could not uncomment bootp entry in /etc/inetd.conf" >&2
      echo "         Please edit manually."
    fi
  fi
fi


#
# If still not there go ahead and append a line
#

if [ `grep -c '^bootps' /etc/inetd.conf` = 0 ]
then
  BU=`expr $BU + 1`
  cp /etc/inetd.conf /tmp/inetd.conf.bu${BU}.$$
  echo "$BOOTPLINE"    >> /etc/inetd.conf
  RESTARTINETD="true"
  echo "NOTE:    Appended a bootp entry to /etc/inetd.conf" >&2
  echo "         existing inetd.conf saved in /tmp/inetd.conf.bu${BU}.$$"
fi


#
# for services just append line if necessary
#

if [ "`grep -c '^bootps' /etc/services`" = "0" ]
then
  echo "$BOOTPS" >>/etc/services
  if [ "`grep -c '^bootpc' /etc/services`" = "0" ]
  then
    echo "$BOOTPC" >>/etc/services
  fi
fi

#
# create bootptab file if it doesn't already exist.
#

if [ ! -f /etc/bootptab ]
then
  if [ $OS = "hp10" ]
  then
   cp /usr/newconfig/etc/bootptab /etc/bootptab
  else
   cp /etc/newconfig${ARCHEXT}/bootptab /etc/bootptab
  fi
fi


#
#------------------------------------------------------------------------#
# if not already enabled
# - enable NFS services on this host
#------------------------------------------------------------------------#
#
NFSTHERE="false"
if [ $OS = "hp10" ]
then
  swlist NFS | grep 'NFS.SER' > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    echo "NOTE:    The NFS.SERVER fileset does not seem to be installed"
    echo "         X Station will probably have to boot using tftp."
  else
    NFSTHERE="true"
  fi
fi


if [ $OS = "hp10" -a "$NFSTHERE" = "true" ]
then

  # enable NFS server daemons
  #
  sed '/^NFS_SERVER=0/s/NFS_SERVER=0/NFS_SERVER=1/' \
	/etc/rc.config.d/nfsconf > /tmp/nfsconf.$$
  if [ -s /tmp/nfsconf.$$ ]
  then
    cmp -s /etc/rc.config.d/nfsconf /tmp/nfsconf.$$
    if [ $? -eq 1 ]
    then
      BU=`expr $BU + 1`
      cp /etc/rc.config.d/nfsconf /tmp/nfsconf.bu${BU}.$$
      cp /tmp/nfsconf.$$ /etc/rc.config.d/nfsconf
      rm /tmp/nfsconf.$$ 
      echo "NOTE:    enabling NFS server services in /etc/rc.config.d/nfsconf"
      echo "         existing \"nfsconf\" saved as /tmp/nfsconf.bu${BU}.$$"
    fi
  fi

  #
  # if necessary, enable mountd
  #

  egrep '^rpc.mountd' /etc/inetd.conf > /dev/null
  if [ $? -ne 0 ]
  then
    sed '/^START_MOUNTD=0/s/START_MOUNTD=0/START_MOUNTD=1/' \
	/etc/rc.config.d/nfsconf > /tmp/nfsconf.$$
    if [ -s /tmp/nfsconf.$$ ]
    then
      cmp -s /etc/rc.config.d/nfsconf /tmp/nfsconf.$$
      if [ $? -eq 1 ]
      then
        BU=`expr $BU + 1`
        cp /etc/rc.config.d/nfsconf /tmp/nfsconf.bu${BU}.$$
        cp /tmp/nfsconf.$$ /etc/rc.config.d/nfsconf
        rm /tmp/nfsconf.$$ 
        echo "NOTE:    Enabling rpc.mountd in /etc/rc.config.d/nfsconf"
        echo "         existing \"nfsconf\" saved in /tmp/nfsconf.bu${BU}.$$"
      fi
    fi
  fi


  #
  # if necessary, create NFS startup/shutdown links
  #

  NFS_STARTLINK="/sbin/rc3.d/S100nfs.server" 
  NFS_STOPLINK="/sbin/rc2.d/K900nfs.server" 
  NFS_SCRIPT="/sbin/init.d/nfs.server" 
  for LINK in $NFS_STARTLINK $NFS_STOPLINK
  do
    if [ ! -f "$LINK" ]
    then
      if [ -f "$NFS_SCRIPT" ]
      then
        ln "$NFS_SCRIPT" "$LINK"
        echo "NOTE:    Created NFS start/stop link $LINK."
      else
        echo "NOTE:    no \"$NFS_SCRIPT\", cannot create start/stop links."
      fi
    fi
  done


  #
  # if necessary, run the NFS startup script
  #

  ps -e | awk '$NF == "nfsd"' | grep nfsd > /dev/null 
  if [ $? -ne 0 ]
  then
    if [ -f "$NFS_SCRIPT" ]
    then
      who -r  | egrep -i 'run-level [34]' > /dev/null
      if [ $? -eq 0 ]
      then
        echo "NOTE:    executing \"$NFS_SCRIPT stop|start\""
        $NFS_SCRIPT stop
	sleep 2
        $NFS_SCRIPT start
        sleep 2
      fi
    else
      echo "NOTE:    no \"$NFS_SCRIPT\", cannot start NFS."
    fi
  fi


  #
  # if /etc/exports seems uncomplicated
  # update it with the SW installation directory
  # and WS font directory (only Asian env)

  if [ ! -d $WSFONTDIR/hp_japanese ]
  then
    DIRSTOEXPORT="$XTHOME"
  else
    DIRSTOEXPORT="$XTHOME $WSFONTDIR"
  fi
  for DIRTOEXPORT in $DIRSTOEXPORT
  do
    exportfs -i "$DIRTOEXPORT" > /tmp/reply.$$ 2>&1
  
    egrep 'parent.*dir.*already.*exported' /tmp/reply.$$ > /dev/null
    if [ $? -ne 0 ]
    then
      cat /tmp/reply.$$ | egrep '([a-z])|([A-Z])' > /dev/null
      if [ $? -ne 0 ]
      then
        if [ ! -f /etc/exports ]
        then
          touch /etc/exports
          chmod 644 /etc/exports
          echo "NOTE:    /etc/exports does not exist, created empty version."
        fi

        case $DIRTOEXPORT in
          $XTHOME)
            EXPORTLINE=$PANACOMDIR ;;
          $WSFONTDIR)
            EXPORTLINE=$WSFONTDIR ;;
        esac 
            
        egrep "^$EXPORTLINE" /etc/exports > /dev/null
        if [ $? -ne 0 ]
        then
          cp /etc/exports /tmp/exports.bu.$$
          cat /tmp/exports.bu.$$ | sed '/[/]enware/d' > /etc/exports
          echo "$EXPORTLINE" >> /etc/exports
          exportfs -u "$DIRTOEXPORT" 
          sleep 1
          exportfs -a
          echo "NOTE:    \"$EXPORTLINE\" appended to /etc/exports"
          echo "         existing \"exports\" saved as /tmp/exports.bu.$$"
        fi
      else
        echo "NOTE:    \"exportfs $DIRTOEXPORT\" produced:"
        echo "         `cat /tmp/reply.$$`"
      fi
    fi
    rm -f /tmp/reply.$$  > /dev/null 2>&1
  done
fi


#
#------------------------------------------------------------------------#
# Enable tftp service if not already enabled
#------------------------------------------------------------------------#
#

#
# First just try to remove comment if that seems to be the problem
#

egrep '^tftp' /etc/inetd.conf > /dev/null
if [ $? -ne 0 ]
then
  egrep '^#tftp' /etc/inetd.conf > /dev/null
  if [ $? -eq 0 ]
  then
    BU=`expr $BU + 1`
    cp /etc/inetd.conf /tmp/inetd.conf.bu${BU}.$$
    (echo '/^#tftp/s/#tftp/tftp/'; echo 'w!') | ex - /etc/inetd.conf 
    if [ -s /etc/inetd.conf ]
    then
      echo "NOTE:    uncommented tftp entry in /etc/inetd.conf" >&2
      echo "         existing inetd.conf saved as /tmp/inetd.conf.bu${BU}.$$"
      RESTARTINETD="true"
    else
      BU=`expr $BU + 1`
      cp /tmp/inetd.conf.bu${BU}.$$ /etc/inetd.conf
      echo "NOTE:    Could not uncomment bootp entry in /etc/inetd.conf" >&2
      echo "         Please edit manually."
    fi
  fi
fi

#
# If still not there - just append a line
#

if [ `grep -c '^tftp' /etc/inetd.conf` = 0 ]
then
  if [ $OS = "hp10" ]
  then
    TFTPLINE="tftp dgram udp	wait	root	$TFTPD 	tftpd $PANACOMDIR $DIR700X"
  else
    TFTPLINE="tftp dgram udp	wait	root	$TFTPD	tftpd"
  fi
    BU=`expr $BU + 1`
    cp /etc/inetd.conf /tmp/inetd.conf.bu${BU}.$$
    echo "$TFTPLINE"    >> /etc/inetd.conf
    echo "NOTE:    Appended a tftp entry to /etc/inetd.conf" >&2
    echo "         existing inetd.conf saved as /tmp/inetd.conf.bu${BU}.$$"
    RESTARTINETD="true"
else
  #
  # Make sure the 10.0 version mentions our installation directory
  #
  if [ $OS = "hp10" ]
  then
    cat /etc/inetd.conf | sed -n '/^tftp/,$p' | sed -n '1,/wait/p' \
	| egrep '[ 	]'$PANACOMDIR'(([\ 	])|($))' 2>&1 > /dev/null
    if [ $? -ne 0 ]
    then
      egrep '^tftp' /etc/inetd.conf | egrep "$DIR700X" > /dev/null
      if [ $? -eq 0 ]
      then
        sed "/^tftp/s![ 	]tftpd! tftpd $PANACOMDIR !" \
	  /etc/inetd.conf > /tmp/inetd.conf.$$
      else
        sed "/^tftp/s![ 	]tftpd! tftpd $PANACOMDIR $DIR700X !" \
	  /etc/inetd.conf > /tmp/inetd.conf.$$
      fi
      if [ -s /tmp/inetd.conf.$$ ]
      then
        BU=`expr $BU + 1`
        cp /etc/inetd.conf /tmp/inetd.conf.bu${BU}.$$
        cp /tmp/inetd.conf.$$ /etc/inetd.conf
        RESTARTINETD="true"
        echo "NOTE:    Edited tftp entry in /etc/inetd.conf" >&2
        echo "         The $PANACOMDIR directory was inserted in entry." >&2
        echo "         existing \"inetd.conf\" saved in /tmp/inetd.conf.bu${BU}.$$"
        RESTARTINETD="true"
      else
        echo "WARNING:  Update of /etc/inetd.conf failed. - Please edit"
        echo "          manuallly and add $PANACOMDIR to tftp path(s)."
        echo "          When done, \"inetd\" should be restarted."
      fi
      rm /tmp/inetd.conf.$$
    else
      echo "INFO:    existing tftp entry in /etc/inetd.conf contains" >&2
      echo "         correct path information for $PANACOMDIR." >&2
    fi
  fi
fi

#
# Update services if necessary
#

if [ "`grep -c '^tftp' /etc/services`" = "0" ]
then
  TFTPSLINE="tftp 69/udp		# Trivial File Transfer Protocol"
  echo "$TFTPSLINE" >> /etc/services
fi


#
#------------------------------------------------------------------------#
# Notify installer about inetd.sec
#------------------------------------------------------------------------#
#

if [ -f /usr/adm/inetd.sec ]
then
  if [ `grep -c '^tftp' /usr/adm/inetd.sec` = 0 ]
  then
    echo "NOTE:    No entry for tftp exists in /usr/adm/inetd.sec." >&2
    echo "         You may want to add an entry for 'tftp' to /usr/adm/inetd.sec." >&2
    echo "         For example:                     " >&2
    echo "         tftp allow 192.30.270.*          " >&2 
    echo "         See inetd.sec(4) for more detail." >&2
  fi
fi


#
#------------------------------------------------------------------------#
# make tftp user entry in /etc/passwd and /etc/group
#------------------------------------------------------------------------#
#
if [ "$OS" = "hp" ]
then
  if [ ! -f /etc/group -o ! -f /etc/passwd ]
  then
    echo "ERROR:   Cannot find /etc/group or /etc/passwd" >&2
    echo "         Update of TFTP failed.               " >&2
    exit 1
  fi


  #
  # get UID for tftp
  #
  tftp_uid=`awk -F: '/^tftp:/{print  $3 }' </etc/passwd`
  if [ "$tftp_uid" != "" ]
  then
    tftp_gid=`awk -F: '/^tftp:/{print  $4 }' </etc/passwd`
  else
    tftp_uid=510
  fi


  #
  # get GID for tftp user entry
  #
  if [ "$tftp_gid" = "" ] 
  then
    tftp_gid=1
  fi

  #
  # add tftp "account" to /etc/passwd
  #
  PASSWD="tftp:*:$tftp_uid:$tftp_gid:Trivial FTP user:/usr/tftpdir:/bin/false"
  EPASSWD="tftp:\*:$tftp_uid:$tftp_gid:Trivial FTP user:/usr/tftpdir:/bin/false"
  if [ `grep -c '^tftp:' /etc/passwd` = 0 ]
  then
    echo "$PASSWD" >>/etc/passwd
    echo "NOTICE:  User 'tftp' was added to /etc/passwd" 
  else
    if [ `grep -c "$EPASSWD" /etc/passwd` = 0 ]
    then
      cp /etc/passwd /etc/passwd.old.$$
      sed -e '/^tftp:/d' /etc/passwd > /etc/passwd.tmp
      if cp /etc/passwd.tmp /etc/passwd && rm /etc/passwd.tmp 2>/dev/null
      then
         echo "$PASSWD" >>/etc/passwd
         echo "WARNING: User 'tftp' entry in /etc/passwd was modified," 
         echo "         original /etc/passwd saved as /etc/passwd.orig.$$"
      else
         echo "ERROR:   Could not restore '/etc/passwd' file " >&2
         echo "         from '/etc/passwd.tmp'. " >&2
      fi
    fi
  fi


  if [ `awk -F: '{print $3}' /etc/passwd | egrep "^$tftp_uid" | wc -l` -gt 1 ]
  then
   echo "WARNING: User 'tftp' was added to /etc/passwd with " >&2
   echo "         the following UID and GID:" >&2
   echo "         tftp:*:$tftp_uid:$tftp_gid:TFTP daemon:$TFTPDIR:/bin/false  " >&2
   echo "         Duplicate UID ($tftp_uid) exists in /etc/passwd. It is" >&2
   echo "         necessary to edit /etc/passwd and set 'tftp' UID to" >&2
   echo "         a unique number." >&2
  fi


  #
  # Create directory for tftp user
  #
  mkdir /usr/tftpdir 2> /dev/null
  chown $tftp_uid /usr/tftpdir 2> /dev/null
  chgrp $tftp_gid /usr/tftpdir 2> /dev/null
  chmod 755 /usr/tftpdir 2> /dev/null



  ##########################################################
  # perform various clean-up tasks 
  ##########################################################

  #
  # warn if old version of tftpd existed
  #
  if [ -d /usr/local/tftpdir -o -f /usr/local/etc/tftpd ]
  then
    echo "WARNING: There appears to have been an un-supported version of tftpd" >&2
    echo "         on this system. If the old tftp home directory was not /usr/tftpdir," >&2
    echo "         you may need to move files from the old tftp home directory to" >&2
    echo "         /usr/tftpdir." >&2
  fi


  #
  # remove old 700X fileset
  #
  for FS in TFTP BOOTP
  do
    rm -rf /system${ARCHEXT}/$FS 2>/dev/null
    rm -f /etc/filesets${ARCHEXT}/$FS 2>/dev/null
  done
fi


#
#--------------------------------------------------------------------------#
# support of "versioning" as a menu item in xtadm
#--------------------------------------------------------------------------#
#
#
#
# The existence of the file $XTHOME/Multi_version or similar
# flags that installer wants versioning 
#
ls $DIR700X/*version 2>&1 | egrep -i 'multi[-_]version' > /dev/null 
if [ $? -eq 0 ]
then
  VERSIONING=yes
else
  VERSIONING=no
fi

if [ -d "$DIR700X" ]
then
  ODIR=`ls -la $DIR700X | awk '{print $NF}'`
else
  ODIR=none
fi

if [ ! -d "$ODIR" ]
then
  VERSIONING=no
fi

#
# Existence of link means that versioning stuff has been done
#
if [ -h $NEWDIR ]
then
  VERSIONING=no
fi

#
# Existence of modules.ld is taken as proof that old install there
#
if [ ! -f $DIR700X/bin/modules.ld ]
then
  VERSIONING=no
fi

#
# Existence of new modules.ld is taken as proof that this is
# a reinstallation
#
if [ -f $NEWDIR/bin/modules.ld ]
then
  VERSIONING=ignore
fi

# SW version sourced from $ADMINDIR/HPXTVERSION version file
#

#
#--------------------------------------------------------------------------#
# If user has not indicated that (s)he cares about versioning
# just merge the new version with eventual existing.
# If no existing version just rename new to $XTHOME
#--------------------------------------------------------------------------#
#

#
# the $XTHOME directories below are not versioned
#
EXCLUDED="fonts config etc"

if [ $VERSIONING = "no" ]
then
  if [ ! -h $NEWDIR ]
  then
    if [ -f "$ODIR/bin/modules.ld" ]
    then
      (
        # take directories in bites
        # so we need less disk space
        #
        cd $NEWDIR 
        DIRS="doc vuerx dt lib bin ."
        echo "INFO:   merging this version with existing version of ENWARE"
        for DIR in $DIRS
        do
          if [ -d $DIR ] 
          then
            find $DIR -depth -print | cpio -pdu $ODIR
            if [ $? -ne 0 ]
            then
              echo "ERROR:  merge with existing version of ENWARE failed "
              rm -rf $DIR
              exit 1
            fi
            if [ "$DIR" != "." ]
            then
              rm -rf $DIR
            fi
          fi
        done
        if [ "$OS" = "hp10" ]
        then
          echo "INFO:   Old ENWARE version can be removed using \"swremove\"."
        fi
        rm -f $ODIR/ENWARE.v.*
      )
      rm -rf $NEWDIR
      if [ "$ODIR" != "$XTHOME" ]
      then
        (
        cd $XTHOME 
        DIRS="fonts config etc"
        for DIR in $DIRS
        do
          if [ -d $DIR ] 
          then
            find $DIR -depth -print | cpio -pdu $ODIR
            if [ $? -ne 0 ]
            then
              echo "ERROR:  merge with existing version of ENWARE failed "
              rm -rf $DIR
              exit 1
            fi
            rm -rf $DIR
          fi
        done
        )
        rm -rf $XTHOME
        mv $ODIR $XTHOME
      fi
    else
      for EXDIR in $EXCLUDED
      do
        rm -rf $NEWDIR/$EXDIR
        mv     $XTHOME/$EXDIR $NEWDIR
      done
      rmdir $XTHOME
      mv $NEWDIR $XTHOME
    fi
    case $OS in
      hp10) (umask 02; ln -s $XTHOME $NEWDIR) ;;
      *)    ln -s /usr/lib/X11/700X $NEWDIR ;;
    esac
  fi
fi

#
#--------------------------------------------------------------------------#
# 1) set up a symlink so that individual packages can be reinstalled
# 2) remove old links if present
#--------------------------------------------------------------------------#
#

if [ $VERSIONING = "yes" ]
then
  NVER=`$ADMINDIR/xtutil_GetVersion $NEWDIR/bin`
  OVER=`$ADMINDIR/xtutil_GetVersion $ODIR/bin`

  if [ "$OVER" = "$NVER" ]
  then
    echo "INFO:    Seems to be a reinstallation of version $OVER of ENWARE"
    echo "         existing version of $ODIR not saved"
    for EXDIR in $EXCLUDED
    do
      rm -rf $NEWDIR/$EXDIR
      mv     $ODIR/$EXDIR $NEWDIR
    done
    rm -rf $ODIR
    mv  $NEWDIR $XTHOME
    case $OS in
      hp10) ln -s $XTHOME $NEWDIR ;;
      *)    ln -s /usr/lib/X11/700X $NEWDIR ;;
    esac
  else
    if [ "$ODIR" != "$XTHOME" ]
    then
      (
      cd $XTHOME 
      DIRS="fonts config etc"
      for DIR in $DIRS
      do
        if [ -d $DIR ] 
        then
          find $DIR -depth -print | cpio -pdu $ODIR
          if [ $? -ne 0 ]
          then
            echo "ERROR:  merge with existing version of ENWARE failed "
            rm -rf $DIR
            exit 1
          fi
          rm -rf $DIR
        fi
      done
      )
      rm -rf $XTHOME
      mv $ODIR $XTHOME
    fi
    rm -f $XTHOME.$OVER
    case $OS in
      hp10) ln -s $XTHOME $XTHOME.$OVER ;;
      *)    ln -s /usr/lib/X11/700X $XTHOME.$OVER ;;
    esac

    #-----------------------------------------------
    # Add NFS support for old version
    #-----------------------------------------------
    #
    mv /usr/lib/X11/700X.$OVER /usr/lib/X11/700X.$OVER.$$ > /dev/null 2>&1
    ln -s $XTHOME.$OVER /usr/lib/X11/700X.$OVER 

    #----------------------------------------------------------------#
    # Create links for directories that are not versioned
    #----------------------------------------------------------------#
    #
    for EXDIR in $EXCLUDED
    do
      rm -f $NEWDIR/$EXDIR
      case $OS in
        hp10) ln -s $XTHOME/$EXDIR           $NEWDIR/$EXDIR ;;
        *)    ln -s /usr/lib/X11/700X/$EXDIR $NEWDIR/$EXDIR ;;
      esac
    done
  fi
fi

# 
# Create link for NFS support
# 
rm -f ${DIR700X}.${HPXTVERSION}
ln -s $NEWDIR ${DIR700X}.${HPXTVERSION}


#
#---------------------------------------------------------------------------#
# make sym link to WS fonts if our own not installed
#---------------------------------------------------------------------------#
#  

if [ "$OS" = "hp10" ]
then
  XTFONTDIR=$NEWDIR/fonts

  if [ ! -d $XTFONTDIR ]
  then
    if [ -d $WSFONTDIR ]
    then
      if [ -h $XTFONTDIR ]
      then
        rm $XTFONTDIR
        xtutil_rm-file $XTFONTDIR
      fi
      ln -s $WSFONTDIR $XTFONTDIR
      xtutil_add-file $XTFONTDIR
    else
      echo
      echo "WARNING: The ENWARE fonts were not selected and no fonts seem to be"
      echo "         preset in WSFONTDIR. -- It is recommended that you re-run"
      echo "         the installation and select the ENWARE font filesets."
      echo
    fi
  fi

  FDIRS="misc iso_8859.1/75dpi iso_8859.1/100dpi hp_roman8/75dpi"
  for FDIR in ${FDIRS}
  do
    WSDIR=${WSFONTDIR}/${FDIR}
    XTDIR=${XTFONTDIR}/${FDIR}
    if [ ! -d ${XTDIR} ]
    then
      if [ -d ${WSDIR} ]
      then
	ln -s ${WSDIR} ${XTDIR}
        echo "INFO:    Created link to WS fonts in $WSDIR"
      else
        echo "NOTE:    No WS fonts $WSDIR, cannot create link"
      fi
    fi
  done
fi

#
#---------------------------------------------------------------------------#
# Remove previous admin script if there
#---------------------------------------------------------------------------#
#  
if [ "$OS" = "hp10" ]
then
  if [ -h /usr/bin/X11/700X ]
  then
    if [ -d /usr/bin/X11/700X/admin ]
    then
      (
      # Make sure we are not removing version  from this install
      #
      cd /usr/bin/X11/700X/admin
      if [ "`pwd`" != "$ADMINDIR" ]
      then
        rm ./xtadm
      fi
      )
    fi
  fi
fi

#
#---------------------------------------------------------------------------#
# make compatibility link for support of older boot ROMs
# that do not use bootp with new "bf" tag
#---------------------------------------------------------------------------#
#  

if [ "$OS" = "hp10" ]
then
  LINK=$DIR700X

  if [ -f $LINK -o -h $LINK ]
  then
    rm $LINK
    xtutil_rm-file $LINK
  fi

  if [ -d $LINK ]
  then
    mv $LINK $LINK.old.$$
    echo "NOTE:   saved $LINK as $LINK.old.$$"
  fi

  ln -s $XTHOME $LINK
  if [ $? -ne 0 ]
  then
    echo "NOTE:   Could not create compatibility link $LINK."
    echo "        If X Stations try to boot from $LINK, and the boot fails:"
    echo "        ON X Station: <Press F12 for 2 seconds>, <click Server>"
    echo "                      <enter a new Base Path, eg $XTHOME>"
   echo "                      <click OK><cycle power>"
  else
    xtutil_add-file $LINK
  fi

  LINK=/usr/bin/X11/700X

  if [ -f $LINK -o -h $LINK ]
  then
    rm $LINK
    xtutil_rm-file $LINK
  fi

  ln -s ${INSTALLDIR}/bin $LINK
  if [ $? -ne 0 ]
  then
    echo "NOTE:   Could not create compatibility link $LINK."
  else
    xtutil_add-file $LINK
  fi
fi

#
#----------------------------------------------------------------------#
# remove existing stuff in /usr/lib/X11/700X/*
# and replace it with a symbolic link to /usr/tftpdir/usr/lib/X11/700X
# (this is to clean-up old old stuff)
#----------------------------------------------------------------------#
#

if [ "$OS" = "hp" ]
then

  LIBDIR=$DIR700X
  TLIBDIR=/usr/tftpdir${DIR700X}

  rm -rf $LIBDIR

  ln -s $TLIBDIR $LIBDIR
fi

#
#---------------------------------------------------------------------------#
# save existing version of sys.vuerxrc
#---------------------------------------------------------------------------#
#

if [ "$OS" = "hp" ]
then
  SVUERXRC=/etc/newconfig/vuerx/sys.vuerxrc
fi

if [ "$OS" = "hp10" ]
then
  SVUERXRC=${INSTALLDIR}/newconfig/sys.vuerxrc
fi

DVUERXRC=${NEWDIR}/vuerx/sys.vuerxrc

if [ -f $SVUERXRC ]
then
  if [ ! -f $DVUERXRC ]
  then
    cp $SVUERXRC $DVUERXRC
    xtutil_add-file "$DVUERXRC" 
  else
    cmp -s $SVUERXRC $DVUERXRC
    if [ $? -ne 0 ]
    then
      cp $DVUERXRC $DVUERXRC.old.$$
      echo "INFO:    existing sys.vuerxrc saved as sys.vuerxrc.old.$$"
      cp $SVUERXRC $DVUERXRC
      xtutil_add-file $DVUERXRC.old.$$
    fi
  fi
fi


#
#----------------------------------------------------------------------#
# update modules.ld with extensions from this fileset
#----------------------------------------------------------------------#
#

SMODULES=$NEWDIR/bin/modules.suppl
MODULES=$NEWDIR/bin/modules.ld

if [ -f $MODULES ]
then
  cp $MODULES $MODULES.old.$$
  echo "INFO:    existing modules.ld saved as modules.ld.old.$$" 
  xtutil_add-file $MODULES.old.$$
fi

mv $SMODULES $MODULES
xtutil_add-file $MODULES
xtutil_rm-file  $SMODULES

#
#--------------------------------------------------------------------------#
# Copy sample files to downloadable files if they don't already exist
# This stops excessive "file not found" messages in the X terminal log
#--------------------------------------------------------------------------#
#

HPXT_DIR=$XTHOME
HPXT_ETC_DIR=$HPXT_DIR/etc
HPXT_CONFIG_DIR=$HPXT_DIR/config

# files in etc
for FILE in snmpdrc
do
    cond_copy $HPXT_ETC_DIR/sample.$FILE $HPXT_ETC_DIR/$FILE
done

# files in etc without sample files
for FILE in hosts authority
do
  if [ ! -f $HPXT_ETC_DIR/$FILE ]
  then
    touch $HPXT_ETC_DIR/$FILE

    xtutil_add-file $HPXT_ETC_DIR/$FILE

  fi
done

#
#------------------------------------------------------------------------#
# if HP-UX 10.x put admin script into sam
# below main menu
#------------------------------------------------------------------------#
#

if [ "$OS" = "hp10" ]
then
  SAMMENU=/usr/sam/lib/C/sam.cb
  SUBMENU=""
  UPDATESAM="true"

  # If there, remmove old irregular entry in /usr/sam/lib/C/sam
  #
  egrep '^[^#]' | egrep -i '^label.*ENWARE' $SAMMENU > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    egrep "xtadm.sam" $SAMMENU > /dev/null
    if [ $? -eq 0 ]
    then
      sed '/^label.*ENWARE/,/command.*xtadm.sam/d' $SAMMENU > $SAMMENU.new.$$
      if [ -s $SAMMENU.new.$$ ]
      then
        cp $SAMMENU.new.$$ $SAMMENU
        rm $SAMMENU.new.$$ 
      fi
    fi
  fi

  # Now, add admin script to sam
  #
  TMPSAM=$ADMINDIR/sam/C/xtadm.ou
  mkdir -p `dirname $TMPSAM`
  rm -f $TMPSAM
  echo "label \"ENWARE X-Station Administration\""           	>> $TMPSAM
  echo "icon_path \"/usr/vue/icons/Color/enware.l.pm\""      	>> $TMPSAM
  echo "command \"$ADMINDIR/xtadm.sam\""              		>> $TMPSAM
  chmod 644 $TMPSAM
  /usr/sam/bin/samreg $ADMINDIR/sam
fi


#
#------------------------------------------------------------------------#
# if HP-UX 9.x put admin script into sam
# below "other util" menu
#------------------------------------------------------------------------#
#

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

if [ $RELEASE = "09" -o $RELEASE = "9" ]
then

  SAMMENU=/usr/sam/custom/xtadm.ou
  SUBMENU=""

  if [ ! -f SAMMENU ]
  then
    echo "label \"X Station Administration\""                   >  $SAMMENU
    echo "command \"$ADMINDIR/xtadm\""            		>> $SAMMENU
    chmod 644 $SAMMENU
    echo "$SAMMENU" >> /etc/filesets/$FILESET 2>/dev/null
  fi
fi

#
#------------------------------------------------------------------------#
# if HP-UX 8.x also put admin script into sam
# below "other util" menu
#------------------------------------------------------------------------#
#

if [ $RELEASE = "08" -o $RELEASE = "8" ]
then

  SAMMENU=/usr/sam/config/other_utils
  SUBMENU="<Other Utilities>"

  grep "700.*xtadm" $SAMMENU > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    mv $SAMMENU $SAMMENU.$$.old
    echo "NOTE:    Existing sam menu file saved as $SAMMENU.$$.old"
    echo "         To restore old sam menu type:"
    echo "         \"cp $SAMMENU.$$.old $SAMMENU\""

    cp -f $SAMMENU.$$.old $SAMMENU
    echo " \"X Station Administration\" \"$ADMINDIR/xtadm\"" \
	>> $SAMMENU
  fi
fi

#
#------------------------------------------------------------------------#
# echo reminder to installer
#------------------------------------------------------------------------#
#

echo
echo "INFO:    For X Station admin use \"sam$SUBMENU\""
echo "         or \"$ADMINDIR/xtadm\""
echo


#
#------------------------------------------------------------------------#
# mail reminder to root
#------------------------------------------------------------------------#
#

ps -e | awk '$NF == "sendmail"' | grep sendmail > /dev/null
if [ $? -eq 0 ]
then
(
mail root <<EOF
Subject: X Station administration using "sam"
This message was mailed to you by an HP X Station customization script
to remind you that you can administer the HP Family of X Stations
from the sam $SUBMENU menu or by typing

 $ADMINDIR/xtadm 

at the UNIX prompt.
EOF
) > /dev/null 2>&1 &
fi


#
#------------------------------------------------------------------------#
# Update the system PATH and MANPATH
#------------------------------------------------------------------------#
#

if [ "$OS" = "hp10" ]
then
  mod_pathfile -a P ${INSTALLDIR}/bin
  mod_pathfile -a MP ${INSTALLDIR}/man
fi


#
#------------------------------------------------------------------------#
# Create sym link /opt/hpxt/enware if installing into different directory
#------------------------------------------------------------------------#
#

DEFDIR=/opt/hpxt/enware

if [ "$OS" = "hp10" ]
then
  if [ "$INSTALLDIR" != "$DEFDIR" ]
  then
    if [ -h "$DEFDIR" ]
    then
      rm $DEFDIR
    fi
    ln -s $INSTALLDIR $DEFDIR
    echo "NOTE:    Created symlink from $DEFDIR to $INSTALLDIR" 
  fi
fi

#
#------------------------------------------------------------------------#
# Create sym links for vue and  from bin/xtadm to lbin/xtadm 
#------------------------------------------------------------------------#
#

if [ "$OS" = "hp10" ]
then
  if [ -d /etc/vue/config/types/tools ]
  then
    DTOOLDIR=/etc/vue/config/types/tools/X_Terminals
    rm -rf ${DTOOLDIR} > /dev/null 2>&1
    ln -s ${INSTALLDIR}/usr/vue/config/types/tools/X_Terminals ${DTOOLDIR}
  fi

  if [ -d /usr/vue/dialogs -a -d /usr/vue/types -a -d /usr/vue/icons ]
  then
    rm -f /usr/vue/dialogs/700rx.ddf
    ln -s ${INSTALLDIR}/usr/vue/dialogs/700rx.ddf /usr/vue/dialogs/700rx.ddf

    if [ -d /usr/vue/types ]
    then
      rm -f /usr/vue/types/hpxt.vf
      ln -s ${INSTALLDIR}/usr/vue/types/hpxt.vf /usr/vue/types/hpxt.vf
    fi


    ICONDIR=/usr/vue/icons
 
    if [ -d $ICONDIR ]
    then
      ICONS="frontEnd.l.pm frontEnd.s.pm frontEnd.pm"
      for i in $ICONS
      do
        THISDIR=${ICONDIR}/Vuewm
        if [ -d $THISDIR ]
        then
          rm -f ${THISDIR}/$i
          ln -s ${INSTALLDIR}${THISDIR}/$i ${THISDIR}/$i
          THISDIR=${THISDIR}/Color
          if [ -d $THISDIR ]
          then
            rm -f ${THISDIR}/$i
            ln -s ${THISDIR}/$i ${THISDIR}/$i
          fi
        fi
      done

      ICONS="enware.l.bm enware.s.bm"
      for i in $ICONS
      do
        rm -f ${ICONDIR}/$i
        ln -s ${INSTALLDIR}${ICONDIR}/$i ${ICONDIR}/$i
      done

      ICONS="enware.l.pm enware.s.pm"
      for i in $ICONS
      do
        THISDIR=${ICONDIR}/Color
        if [ -d $THISDIR ]
        then
          rm -f ${THISDIR}/$i
          ln -s ${INSTALLDIR}${THISDIR}/$i ${THISDIR}/$i
        fi
      done

      ICONS="enware.i.bm"
      for i in $ICONS
      do
        THISDIR=${ICONDIR}/Vuewm
        if [ -d $THISDIR ]
        then
          rm -f ${THISDIR}/$i
          ln -s ${INSTALLDIR}${THISDIR}/$i ${THISDIR}/$i
        fi
      done
    fi
  fi


  XTADMLINK=${INSTALLDIR}/bin/xtadm
  rm -f ${XTADMLINK}
  rm -f ${INSTALLDIR}/bin/admin 
  ln -s ${INSTALLDIR}/lbin/xtadm  ${XTADMLINK}
  ln -s ${INSTALLDIR}/lbin ${INSTALLDIR}/bin/admin 
fi

#
#------------------------------------------------------------------------#
# remove old way of starting floppy capable X terminals
# as same functionality is now handled in (un)mounfloppy scripts
# - they just return without doing anything for non floppy capable terminals.
#------------------------------------------------------------------------#
#

#
# remove eventual lines from Xconfig
#
VUEPPID=""
XCONFIGS="/usr/vue/config/Xconfig /etc/vue/config/Xconfig"

for XCONFIG in $XCONFIGS
do
  if [ -f $XCONFIG ]
  then
    egrep '(Xstartup.hpxt)|(Xreset.hpxt)' $XCONFIG > /dev/null
    if [ $? -eq 0 ]
    then
      TMP=/tmp/tmp.$$
      sed -e '/Xstartup.hpxt/d' -e '/Xreset.hpxt/d' $XCONFIG > $TMP
      if [ -s $TMP ]
      then
        cp $TMP $XCONFIG
	if [ "$VUEPPID" = "" ]
	then
	  VUEPPID=`ps -ef | awk '$3 != "1"' | egrep '/vuelogin$' \
		| grep -v grep | awk '{print $3}' | head -1`
	fi
      fi
      rm -f $TMP
    fi
  fi
done

#
# remove (silly) condition from Xstartup/Xreset
#
if [ "$OS" = "hp10" ]
then
  VUENEWCONFIG=/usr/newconfig/etc/vue/config
  XDMSCRIPTS="/etc/vue/config/Xstartup /etc/vue/config/Xreset"
else
  VUENEWCONFIG=/usr/vue/newconfig
  XDMSCRIPTS="/usr/vue/config/Xstartup /usr/vue/config/Xreset"
fi

for XDMSCRIPT in $XDMSCRIPTS
do
  if [ ! -f "$XDMSCRIPT" ]
  then
    THISSCRIPT="`basename $XDMSCRIPT`"
    if [ -f "${VUENEWCONFIG}/${THISSCRIPT}" ]
    then
     cp "${VUENEWCONFIG}/${THISSCRIPT}"  "`dirname $XDMSCRIPT`"
    fi
  fi

  if [ -f "$XDMSCRIPT" ]
  then
    egrep '(Xstartup.hpxt)|(Xreset.hpxt)' $XDMSCRIPT > /dev/null
    if [ $? -eq 0 ]
    then
      TMP=/tmp/tmp.$$
      sed -e 's/-a "`basename $0`" = "Xstartup.hpxt" //' \
          -e 's/-a "`basename $0`" = "Xreset.hpxt" //' $XDMSCRIPT > $TMP
      cmp -s $TMP $XDMSCRIPT
      if [ $? -ne 0 ]
      then
        if [ -s $TMP ]
        then
          cp $TMP $XDMSCRIPT
        fi
      fi
      rm -f $TMP
    fi
  fi
done


#
# Add floppy lines to Xstartup and Xreset
#

VUEDIRS="/usr/lib/X11/vue/Vuelogin /usr/vue/config /etc/dt/config /usr/lib/X11/xdm"
DTUSRSTART=/usr/dt/config/Xstartup
DTUSRRESET=/usr/dt/config/Xreset
DTETCSTART=/etc/dt/config/Xstartup
DTETCRESET=/etc/dt/config/Xreset

if [ -f "$DTUSRSTART" ]
then
  if [ ! -f "$DTETCSTART" ]
  then
    cp "$DTUSRSTART" "$DTETCSTART"
  fi
  if [ ! -f "$DTETCRESET" ]
  then
    cp "$DTUSRRESET" "$DTETCRESET"
  fi
fi


for VUEDIR in $VUEDIRS
do
  XSTARTUP=$VUEDIR/Xstartup
  XRESET=$VUEDIR/Xreset

  if [ -f $XSTARTUP -a -f $XRESET ]
  then
	#
	# does Xstartup and Xreset seem clean?
	#
    cat $XSTARTUP $XRESET | egrep 'mountfloppy' > /dev/null
    if [ $? -ne 0 ]
    then
	#
	# yes, go ahead and append lines
	#
      cp -p $XSTARTUP $XSTARTUP.old.$$
      cat $XSTARTUP.old.$$  >  $XSTARTUP
      echo '' 				>> $XSTARTUP
      echo 'if [ "$USER" != "root" -a -x /usr/bin/X11/700X/admin/mountfloppy ]' 	>> $XSTARTUP
      echo 'then'				>> $XSTARTUP
      echo '  /usr/bin/X11/700X/admin/mountfloppy $DISPLAY $USER $HOME/floppy' \					>> $XSTARTUP
      echo '  :'				>> $XSTARTUP
      echo 'fi' 				>> $XSTARTUP

      cp -p $XRESET $XRESET.old.$$
      cat $XRESET.old.$$ >  $XRESET
      echo '' 							>> $XRESET
      echo 'if [ "$USER" != "root" -a -x /usr/bin/X11/700X/admin/unmountfloppy ]' 	>> $XRESET
      echo 'then'						>> $XRESET
      echo '  /usr/bin/X11/700X/admin/unmountfloppy $DISPLAY $USER $HOME/floppy' 	   	>> $XRESET
      echo '  :'						>> $XRESET
      echo 'fi' 						>> $XRESET

      echo
      echo "NOTE:"
      echo "   `basename $XSTARTUP` and `basename $XRESET` modified to include,"
      echo "   script code, that will mount/unmount an HP X Xstation floppy on directory"
      echo "   \$HOME/floppy, when logging in/out using vuelogin or dtlogin"
      echo "   Existing Xstartup and Xreset saved in `dirname $XSTARTUP`"
      echo ""
      echo "   For details refer to the"
      echo "   \"ENWARE X Station Software Technical Reference\" manual"
      echo ""
      echo ""	
    fi
  fi
done

if [ "$VUEPPID" != "" ]
then
  kill -1 $VUEPPID
  echo ""
  echo "NOTE:  signalled vuelogin to re-read $XCONFIG"
fi


#
#---------------------------------------------------------------------------#
# Tell inetd about the changes in /etc/inetd.conf
#---------------------------------------------------------------------------#
#  

if [ "$RESTARTINETD" = "true" ]
then
  if [ "$OS" = "hp10" ]
  then
    kill_named_procs bootpd
    kill_named_procs tftpd
    ps -e | awk '{print $NF}' | egrep inetd > /dev/null
    if [ $? -eq 0 ]
    then
      /usr/sbin/inetd -c
      if [ $? -ne 0 ]
      then
        echo "WARNING: \"$INETD -c\" returned an error." >&2
        echo "         Try to re-run the command manually." >&2
      fi
    else
      echo "INFO:      \"$INETD\" not running." >&2
    fi
  fi

  if [ "$OS" = "hp" ]
  then
    $INETD -k >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
      echo "WARNING: \"$INETD -k\" returned an error." >&2
      echo "         Try to re-run the command manually." >&2
    else
      sleep 2

	# 
	# if logging seems to be turned on then re-start it that way too 
	# 
      egrep "^[^#].*inetd[	 ].*-l" /etc/netlinkrc > /dev/null
      if [ $? -eq 0 ]
      then
        $INETD -l
      else
        $INETD
      fi
    fi
  fi
fi



#
# set up and start the x font server
#

#
# add dtfonts to the font server config file
#

CONFIG=/etc/X11/fs/config
XFONTS=/usr/dt/config/xfonts/C
if [ ! -d "$XFONTS" ]
then
  XFONTS=/etc/dt/config/xfonts/C
fi

if [ "$OS" = "hp10" -a -f "$CONFIG" -a -d "$XFONTS" ]
then
  RCCONFIGXFS=/etc/rc.config.d/xfs
  TMP=/tmp

  #
  # is $XFONTS already in the file?
  #

  egrep $XFONTS $CONFIG >/dev/null 2>&1


  if [ $? -ne 0 ]
  then
	#
	# no, so add it
	#

	#
	# make a backup copy of /etc/X11/fs/config before modifying it
	#

	cp $CONFIG /tmp/`basename $CONFIG`.$$
	echo "INFO:    a copy of $CONFIG file has been saved in " >&2
        echo "         /tmp/`basename $CONFIG`.$$" >&2          
	echo ""


	#
	# append /usr/dt/config/xfonts/C to the catalogue
	# line

	grep -v catalogue $CONFIG > $TMP/configminuscat.$$
	CATALOGUE=`egrep catalogue $CONFIG`,$XFONTS
	echo $CATALOGUE >> $TMP/configminuscat.$$
	cp /tmp/configminuscat.$$  $CONFIG
  fi	


  #
  # make sure /etc/rc.config.d/xfs has a 1 for
  # RUN_X_FONT_SERVER
  #
  if [ -f "$RCCONFIGXFS" ]
  then
	cp $RCCONFIGXFS /tmp/`basename $RCCONFIGXFS`.$$
	echo "INFO:    a copy of $RCCONFIGXFS file has been saved in " >&2
        echo "         /tmp/`basename $RCCONFIGXFS`.$$" >&2

	cat $RCCONFIGXFS  | sed 's/RUN_X_FONT_SERVER=0/RUN_X_FONT_SERVER=1/g' > $TMP/xfsnew.$$
	cp $TMP/xfsnew.$$ $RCCONFIGXFS
  fi


  #
  # start the font server, if it's not already running
  # else send it a SIGUSR1 signal
  #
  TMPPS=/tmp/ps.tmp.$$
  ps -ef | grep xfs | grep -v grep > $TMPPS
  if [ -s "$TMPPS" ]
  then
	XFSPID=`cat $TMPPS | awk '{print $2}' | head -1`
	kill -SIGUSR1 $XFSPID
  else
	/sbin/init.d/xfs start
  fi



  #
  # add the hpxt_addfs script to /etc/dt/config/Xsession.d
  # so that the font server is added to the terminal's path
  #

  #HPXTADDXFS=/etc/dt/config/Xsession.d/hpxt_addfs

  #
  # add the script to /etc/dt/config/Xsession.d directory
  #
  #if [ !  -f "$HPXTADDXFS" ]
  #then
  #	echo 'xset  -display $DISPLAY fp+ tcp/`hostname`:7000' >$HPXTADDXFS
  #	chmod 0755 $HPXTADDXFS
  #fi


  #
  # add the font server to the DISPLAY's font path in Xsetup
  #
  HOSTNAME=`hostname`
  ETCXSETUP=/etc/dt/config/Xsetup
  USRXSETUP=/usr/dt/config/Xsetup

  if [ ! -f "$ETCXSETUP" ]
  then
	cp $USRXSETUP $ETCXSETUP
  fi

  grep "$HOSTNAME:7000" $ETCXSETUP >/dev/null 2>&1

  if [ $? -ne 0 ]
  then
	echo "xset fp+ tcp/$HOSTNAME:7000" >>$ETCXSETUP
  fi

fi


#-------------------------------------------------------------------------#
#
# If ENWARE was installed previously 
# and into the old default location (/opt/enware) then 
# - create a link so that /opt/enware point to /opt/hpxt/enware.
# - also "update" bootptab with new path
#
#-------------------------------------------------------------------------#

if [ -d /opt/enware ]
then
  if [ "`ls -ld /opt/enware | awk '{print $NF}'`" != /opt/hpxt/enware ]
  then

    echo "INFO:    An older version of ENWARE seems to be installed in /opt/enware," >&2
    echo "INFO:    .. performing upgrade .." >&2
    # link
    #
    mv /opt/enware /opt/enware.old.$$
    ln -s /opt/hpxt/enware /opt/enware
    echo "INFO:    Created link from /opt/enware to /opt/hpxt/enware " >&2

    # bootptab 
    #
    egrep '/opt/enware' /etc/bootptab > /dev/null
    if [ $? -eq 0 ]
    then
      TMPBOOTPTAB=/tmp/bootptab.$$
      ETCBOOTPTAB=/etc/bootptab
      sed 's!/opt/enware!/opt/hpxt/enware!g' $ETCBOOTPTAB > $TMPBOOTPTAB
      if [ -s $TMPBOOTPTAB ]
      then
        cp ${ETCBOOTPTAB} ${ETCBOOTPTAB}.old.$$
        cp $TMPBOOTPTAB ${ETCBOOTPTAB}
        rm $TMPBOOTPTAB
        echo "INFO:    Modified $ETCBOOTPTAB to include path /opt/hpxt/enware" >&2
      fi
    fi
  fi
fi

exit 0


