#!/sbin/sh

#########################################################################
#
#   File:	 customize
#   Description: /etc/update customization script for HPXT2-ADMIN fileset
# 
#########################################################################
#
umask 022
set -a

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


RESTARTINETD="false"

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

DIR_ENWARE2=/opt/hpxt/enware2


OS="`uname -r | cut -d"." -f 2`"

if [ "$OS" = "10" -o "$OS" = "11" ]
then
  OS="hp10"
  INSTALLDIR="`echo ${SW_LOCATION} | sed 's!/$!!'`"/enware2
  XTHOME=${INSTALLDIR}/xthome
  BOOTPD=/usr/lbin/bootpd
  TFTPD=/usr/lbin/tftpd
  INETD=/usr/sbin/inetd
  ADMINDIR=${INSTALLDIR}/lbin
else
  OS="hp"
  INSTALLDIR=/usr/tftpdir${DIR_ENWARE2}
  XTHOME=${INSTALLDIR}/xthome
  BOOTPD=/etc/bootpd
  TFTPD=/etc/tftpd
  INETD=/etc/inetd
  ADMINDIR=${INSTALLDIR}/lbin
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 sym link /opt/hpxt/enware2 if installing into different directory
##---------------------------------------------------------------------#
##
#

if [ "$INSTALLDIR" != "$DIR_ENWARE2" ]
then

  if [ ! -d /opt/hpxt ]
  then
    mkdir -p /opt/hpxt
    chmod 775 /opt/hpxt
  fi

  if [ -h "$DIR_ENWARE2" ]
  then
    rm $DIR_ENWARE2
  fi

  if [ -d "$DIR_ENWARE2" ]
  then
    mv $DIR_ENWARE2 $DIR_ENWARE2.old.$$
  fi

  ln -s $INSTALLDIR $DIR_ENWARE2
  chmod 775 $DIR_ENWARE2
  echo "NOTE:    Created symlink from $DIR_ENWARE2 to $INSTALLDIR" 
fi


WSFONTDIR=/usr/lib/X11/fonts

if [ "$INSTALLDIR" = "$DIR_ENWARE2" ]
then
  PANACOMDIR=/opt/hpxt
else
  PANACOMDIR=$INSTALLDIR 
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}/HPXT2VERSION
NEWDIR=${XTHOME}.$HPXT2VERSION
echo "HPXT2VERSION=$HPXT2VERSION" >> /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




#
#------------------------------------------------------------------------#
# 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
    TFTPLINE="tftp dgram udp	wait	root	$TFTPD 	tftpd $PANACOMDIR"
    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
    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 "$DIR_ENWARE2" > /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 !" \
	  /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

#
# 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


  #--------------------------------------------------------------------#
  #  First, if we are running NIS then attempt to get tftp uid and gid from it
  #--------------------------------------------------------------------#
  #
  NIS="no"
  ps -e | grep ypbind > /dev/null
  if [ $? -eq 0 ]
  then
    egrep '^[+]:' /etc/passwd > /dev/null
    if [ $? -eq 0 ]
    then
    tftp_uid=`ypmatch tftp passwd 2> /dev/null | awk -F: '/^tftp:/{print  $3 }'`
    tftp_gid=`ypmatch tftp passwd 2> /dev/null | awk -F: '/^tftp:/{print  $4 }'`
    NIS="yes"
    fi
  fi

  #--------------------------------------------------------------------#
  # get UID for tftp from /etc/passwd  if NIS did no provide it
  # If that also fails use default values
  #--------------------------------------------------------------------#
  #
  if [ "$tftp_uid" = "" ]
  then
    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
  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
    
    if [ "$NIS" = "yes" ]
    then
    #
    # Insert the tftp entry before the "+:" line
    #
      LN=`sed -n '/^\+:/=' /etc/passwd`
      if [ "$LN" -ge 1 ]
      then
        TMPPASSWD=/tmp/passwd.$$
        head -`expr $LN - 1` /etc/passwd > $TMPPASSWD
        echo $PASSWD        >> $TMPPASSWD
        sed -n "$LN,\$p"      /etc/passwd >> $TMPPASSWD
        if [ -s $TMPPASSWD ]
        then
          cp /etc/passwd /etc/passwd.orig.$$
          cp $TMPPASSWD /etc/passwd
        else
          echo "$PASSWD" >>/etc/passwd
        fi
        rm -f $TMPPASSWD
      else
        echo "$PASSWD" >>/etc/passwd
      fi
    else
      echo "$PASSWD" >>/etc/passwd
    fi
    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
        if [ "$NIS" = "yes" ]
        then
        #
        # Insert the tftp entry before the "+:" line
        #
          LN=`sed -n '/^\+:/=' /etc/passwd`
          if [ "$LN" -ge 1 ]
          then
            TMPPASSWD=/tmp/passwd.$$
            head -`expr $LN - 1` /etc/passwd > $TMPPASSWD
            echo $PASSWD        >> $TMPPASSWD
            sed -n "$LN,\$p"      /etc/passwd >> $TMPPASSWD
            if [ -s $TMPPASSWD ]
            then
              cp /etc/passwd /etc/passwd.orig.$$
              cp $TMPPASSWD /etc/passwd
            else
              echo "$PASSWD" >>/etc/passwd
            fi
            rm -f $TMPPASSWD
          else
            echo "$PASSWD" >>/etc/passwd
          fi
        else
          echo "$PASSWD" >>/etc/passwd
        fi

        echo "WARNING: User 'tftp' entry in /etc/passwd was modified," 
        echo "         original /etc/passwd saved as /etc/passwd.old.$$"
      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

fi


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

# Merge "config" directory
if [ ! -d ${INSTALLDIR}/config ]
then
    mkdir -p ${INSTALLDIR}/config
    chmod 775 ${INSTALLDIR}/config
fi
if [ -d ${NEWDIR}/config ]
then
  if [ ! -h ${NEWDIR}/config ]
  then
    find ${NEWDIR}/config -type f \
	-exec cp -p {} ${INSTALLDIR}/config \;
    rm -rf ${NEWDIR}/config/*
    rmdir ${NEWDIR}/config 

    #----------------------------------------------#
    # Before making link ..
    # change umask to make swverify happy
    #
    umask 02
    ln -s ../config ${NEWDIR}/config
    umask 022
  fi
fi

#
# Merge "etc" directory
if [ ! -d ${INSTALLDIR}/etc ]
then
    mkdir -p ${INSTALLDIR}/etc
    chmod 775 ${INSTALLDIR}/etc
fi
if [ -d ${NEWDIR}/etc ]
then
  if [ ! -h ${NEWDIR}/etc ]
  then
    find ${NEWDIR}/etc -type f \
	-exec cp -p {} ${INSTALLDIR}/etc \;
    rm -rf ${NEWDIR}/etc/*
    rmdir ${NEWDIR}/etc 
    ln -s ../etc ${NEWDIR}/etc
  fi
fi

if [ ! -h ${NEWDIR}/etc ]
then
  if [ ! -d ${NEWDIR}/etc ]
  then
    umask 02
    ln -s ../etc ${NEWDIR}/etc
    umask 022
  fi
fi


#
# Set up "fonts" directory
if [ -h ${NEWDIR}/fonts ]
then
  rm ${NEWDIR}/fonts
fi
ln -s ../fonts ${NEWDIR}/fonts


#----------------------------------------------------------------------#
# If multiversioning not enabled, 
#
# 1) Create xthome link to new software
# 2) If there are previous version of Netstation SW installed
#    then merge in files from the old tree that are not in the new tree.
# 3) Finally remove the old tree.
#----------------------------------------------------------------------#
#
if [ "${VERSIONING}" != "yes" ]
then
  
  # Merge in 3270 stuff if there
  #
  if [ -d ${INSTALLDIR}/xthome/tn3270 ]
  then
    mv ${INSTALLDIR}/xthome/tn3270 ${INSTALLDIR}/xthome.${HPXT2VERSION}
  fi

  rm -f ${INSTALLDIR}/xthome
  ln -s xthome.${HPXT2VERSION} ${INSTALLDIR}/xthome
     
  (
  cd "$DIR_ENWARE2"
  NDIR=xthome.${HPXT2VERSION}

  ODIRS=`ls -d xthome.* | egrep -v $NDIR | sort -u -r`

  for ODIR in $ODIRS
  do
   NDLIST=/tmp/dlist.$NDIR
   (cd $NDIR; find . -type d -print | sort -u > $NDLIST)
   if [ -d $ODIR -a ! -h $ODIR ]
   then
     ODLIST=/tmp/dlist.$ODIR
     (cd $ODIR; find . -type d -print | sort -u > $ODLIST)
     comm -23 $ODLIST $NDLIST | sort -u | xargs -i mv $ODIR/{} $NDIR/{}
     NFLIST=/tmp/flist.$NDIR
     (cd $NDIR; find . \( -type f -o -type l \) -print | sort -u > $NFLIST)
     OFLIST=/tmp/flist.$ODIR
     (cd $ODIR; find . \( -type f -o -type l \) -print | sort -u > $OFLIST)
     comm -23 $OFLIST $NFLIST | sort -u | \
	xargs -i -t mv $ODIR/{} $NDIR/{} 2>&1 | grep mv > /dev/null 
     if [ $? -eq 0 ]
     then
        echo "INFO:    some files were merged into $NDIR from $ODIR"
     fi
   fi
   rm -rf $ODIR
   # rm -f $NDLIST $NFLIST $ODLIST $OFLIST
  done
  )
fi

#
#if [ -d "$DIR_ENWARE2" ]
#then
#  ODIR=`ls -la $DIR_ENWARE2 | 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 directives is taken as proof that old install there
##
#if [ ! -f $DIR_ENWARE2/bin/directives ]
#then
#  VERSIONING=no
#fi
#
##
## Existence of new directives is taken as proof that this is
## a reinstallation
##
#if [ -f $NEWDIR/bin/directives ]
#then
#  VERSIONING=ignore
#fi
#
## SW version sourced from $ADMINDIR/HPXT2VERSION 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/directives" ]
#    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) ln -s $XTHOME $NEWDIR ;;
#      *)    ln -s /opt/hpxt/enware2 $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 /opt/hpxt/enware2 $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 /opt/hpxt/enware2 $XTHOME.$OVER ;;
#    esac
#
#    #-----------------------------------------------
#    # Add NFS support for old version
#    #-----------------------------------------------
#    #
#    mv /opt/hpxt/enware2.$OVER /opt/hpxt/enware2.$OVER.$$ > /dev/null 2>&1
#    ln -s $XTHOME.$OVER /opt/hpxt/enware2.$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 /opt/hpxt/enware2/$EXDIR $NEWDIR/$EXDIR ;;
#      esac
#    done
#  fi
#fi
#
## 
## Create link for NFS support
## 
#rm -f ${DIR_ENWARE2}.${HPXT2VERSION}
#ln -s $NEWDIR ${DIR_ENWARE2}.${HPXT2VERSION}


#
#---------------------------------------------------------------------------#
# 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 /opt/hpxt/enware2/bin ]
  then
    if [ -d /opt/hpxt/enware2/lbin ]
    then
      (
      # Make sure we are not removing version  from this install
      #
      cd /opt/hpxt/enware2/lbin
      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=$DIR_ENWARE2
#
#  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=/opt/hpxt/enware2/bin
#
#  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 /opt/hpxt/enware2/*
# and replace it with a symbolic link to /usr/tftpdir/opt/hpxt/enware2
# (this is to clean-up old old stuff)
#----------------------------------------------------------------------#
#

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

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

  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 directives with extensions from this fileset
#----------------------------------------------------------------------#
#

MASTER_DIRECTIVES=${NEWDIR}/bin/directives.master
DIRECTIVES=${NEWDIR}/bin/directives

if [ -f $DIRECTIVES ]
then
  cp $DIRECTIVES        $DIRECTIVES.old.$$
  xtutil_add-file       $DIRECTIVES.old.$$
  mv $MASTER_DIRECTIVES $DIRECTIVES.new
  xtutil_add-file       $DIRECTIVES.new
  xtutil_rm-file        $MASTER_DIRECTIVES
  echo "" 
  echo "INFO:    existing directives file saved as directives.old.$$" 
  echo "INFO:    a new directives file is available in: "
  echo "INFO     $DIRECTIVES.new" 
  echo "" 
else
  mv $MASTER_DIRECTIVES $DIRECTIVES
  xtutil_add-file $DIRECTIVES
  xtutil_rm-file $MASTER_DIRECTIVES
fi


#---------------------------------------------------------------------#
# Add 8_2 information to cfg files
# 1) make BasePath a derivative of NsRoot
# 2) add new variables for LPR support
#---------------------------------------------------------------------#
#

CFGDIR=${NEWDIR}/config

for FILE in $CFGDIR/default.cfg $CFGDIR/sample.cfg
do
  TMP=/tmp/tmp.$$
  if [ -f $FILE ]
  then
    egrep -i 'NsRoot.*=' $FILE > /dev/null
    if [ $? -ne 0 ]
    then
      sed -e '/BasePath.*=/i\
#NsRoot                                   = /opt/hpxt/enware2
' -e '/BasePath.*=/s!/opt/hpxt/enware2/xthome!$(NsRoot)/xthome!' $FILE > $TMP
      cmp -s $TMP $FILE
      if [ $? -ne 0 ]
      then
        if [ -s $TMP ]
        then
          cp $FILE $FILE.orig.$$
          cp $TMP $FILE
        fi
      fi
    fi

    egrep -i "LPR Trailer" $FILE > /dev/null
    if [ $? -ne 0 ]
    then
      sed '/MicroTouch Screen.*Frequency/i\
#LPR Queue                           = laser:dave1x:HPLaserJ:"Daves desk"\
#LPR Leader                          = laser:"\\x1b(s20v(s1S"\
#LPR Trailer                         = laser:"\\x1b(s12v(s0S"\
#LPR Local Spool Threshold           = laser:0\
#LPR Spool Path                      = laser:/opt/hpxt/enware2/xthome/spool'         $FILE > $TMP

      cmp -s $TMP $FILE
      if [ $? -ne 0 ]
      then
        if [ -s $TMP ]
        then
          if [ ! -f $FILE.orig.$$ ]
          then
            cp $FILE $FILE.orig.$$
          fi
          cp $TMP $FILE
        fi
      fi
    fi


   if [ -d /opt/hpxt/enware2/Navio_3.0 ]
   then
     egrep -i navio.cfg $FILE > /dev/null
     if [ $? -ne 0 ]
     then
       echo "#"                                            >> $FILE
       echo "# BEGIN NAVIO config lines"                   >> $FILE
       echo 'include $(NsRoot)/Navio_3.0/config/navio.cfg' >> $FILE
       echo "# END NAVIO config lines"                     >> $FILE
       echo "#"                                            >> $FILE
     fi
   fi


  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 \"HP Netstation Administration\""           	>> $TMPSAM
  echo "icon_path \"/usr/vue/icons/Color/enware.m.pm\""      	>> $TMPSAM
  echo "command \"$ADMINDIR/xtadm.sam\""              		>> $TMPSAM
  chmod 644 $TMPSAM
  xtutil_add-file $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=""

  echo "label \"HP Netstation Administration\""                   >  $SAMMENU
  echo "command \"$ADMINDIR/xtadm\""            		>> $SAMMENU
  chmod 644 $SAMMENU
  echo "$SAMMENU" >> /etc/filesets/$FILESET 2>/dev/null
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 " \"HP Netstation Administration\" \"$ADMINDIR/xtadm\"" \
	>> $SAMMENU
  fi
fi

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

echo
echo "INFO:    For HP netstation 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}/share/man
fi


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

if [ "$OS" = "hp" ]
then
    DTOOLDIR=/usr/vue/config/types/tools
else
    DTOOLDIR=/etc/vue/config/types/tools
fi

if [ -d ${DTOOLDIR} ]
then
  rm -rf ${DTOOLDIR}/Netstations > /dev/null 2>&1
  ln -s ${DIR_ENWARE2}/usr/vue/config/types/tools/Netstations ${DTOOLDIR}/Netstations

  TYPESDIR=/usr/vue/config/types
  if [ ! -d "$TYPESDIR" ]
  then
    TYPESDIR=/usr/vue/types
  fi

  if [ -d "$TYPESDIR" ]
  then
    rm -f $TYPESDIR/hpns.vf
    ln -s ${DIR_ENWARE2}/usr/vue/types/hpns.vf $TYPESDIR/hpns.vf
  fi

  ICONDIR=/usr/vue/icons
 
  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 ${DIR_ENWARE2}${THISDIR}/$i ${THISDIR}/$i
    fi

    THISDIR=${ICONDIR}/Color
    if [ -d ${THISDIR} ]
    then
      rm -f ${THISDIR}/$i
      ln -s ${DIR_ENWARE2}${THISDIR}/$i ${THISDIR}/$i
    fi

    if [ -d ${ICONDIR}/Vuewm/Color ]
    then
      rm -f ${ICONDIR}/Vuewm/Color/$i
      ln -s ${DIR_ENWARE2}${THISDIR}/$i ${ICONDIR}/Vuewm/Color/$i
    fi
  done

  ICONS="enware.t.bm enware.s.bm enware.m.bm enware.l.bm"
  for i in $ICONS
  do
    if [ -d ${ICONDIR} ]
    then
      rm -f ${ICONDIR}/$i
      ln -s ${DIR_ENWARE2}${ICONDIR}/$i ${ICONDIR}/$i
    fi
  done

  ICONS="enware.t.pm enware.s.pm enware.m.pm enware.l.pm"
  for i in $ICONS
  do
    THISDIR=${ICONDIR}/Color
    if [ -d ${THISDIR} ]
    then
      rm -f ${THISDIR}/$i
      ln -s ${DIR_ENWARE2}${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 ${DIR_ENWARE2}${THISDIR}/$i ${THISDIR}/$i
    fi
  done
fi


XTADMLINK=${DIR_ENWARE2}/bin/xtadm
rm -f ${XTADMLINK}
ln -s ${DIR_ENWARE2}/lbin/xtadm  ${XTADMLINK}

##
##------------------------------------------------------------------------#
## 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 '# NFS mounting of HP Netstation floppies' >> $XSTARTUP
      echo '#'                                        >> $XSTARTUP
      echo 'if [ "$USER" != "root" -a -x /opt/hpxt/enware2/lbin/mountfloppy ]' 	>> $XSTARTUP
      echo 'then'				>> $XSTARTUP
      echo '  /opt/hpxt/enware2/lbin/mountfloppy $DISPLAY $USER $HOME/floppy' \					>> $XSTARTUP
      echo '  :'                        >> $XSTARTUP
      echo 'fi' 				>> $XSTARTUP
      echo '' 				>> $XSTARTUP

      cp -p $XRESET $XRESET.old.$$
      cat $XRESET.old.$$ >  $XRESET
      echo '' 							>> $XRESET
      echo 'if [ "$USER" != "root" -a -x /opt/hpxt/enware2/lbin/unmountfloppy ]' 	>> $XRESET
      echo 'then'						>> $XRESET
      echo '  /opt/hpxt/enware2/lbin/unmountfloppy $DISPLAY $USER $HOME/floppy' 	   	>> $XRESET
      echo '  :'                                                >> $XRESET
      echo 'fi' 						>> $XRESET
      echo '' 							>> $XRESET

      echo
      echo "NOTE:"
      echo "   `basename $XSTARTUP` and `basename $XRESET` modified to include,"
      echo "   script code, that will mount/unmount an HP netstation floppy on"
      echo "   \$HOME/floppy, when users log in/out using vuelogin or dtlogin"
      echo "   Existing Xstartup and Xreset saved in `dirname $XSTARTUP`"
      echo ""
      echo "   For details refer to the"
      echo "   \"HP Netstation Software Technical Reference\" manual"
      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 /usr/dt/config/xfonts/C 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.$$
	if [ -s $TMP/xfsnew.$$ ]
        then
	  cp $TMP/xfsnew.$$ $RCCONFIGXFS
        fi
  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 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 "         The HP netstations 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="$DIR_ENWARE2"
  else
    DIRSTOEXPORT="$DIR_ENWARE2 $WSFONTDIR"
  fi
  for DIRTOEXPORT in $DIRSTOEXPORT
  do
  
    exportfs -i "$DIRTOEXPORT" > /tmp/reply.$$ 2>&1
  
    egrep -i 'parent.*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
          $DIR_ENWARE2)
            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

#--------------------------------------------------
# If CDE is installed create CDE help links to
# the Netstation online help volume
#--------------------------------------------------
#
$ADMINDIR/xtutil_add_help_links


#-------------------------------------------------------
# If ENWARE is installed:
# Merge app-defaults files for ENWARE and Netstation SW
#--------------------------------------------------
#
$ADMINDIR/xtutil_merge_app-defaults create


#-------------------------------------------------------
# If ENWARE is installed:
# patch mountfloppy scripts to fix ACCESS control problem
#--------------------------------------------------
#
ENWADMINDIR=/usr/bin/X11/700X/admin
if [ -d "$ENWADMINDIR" ]
then
  echo "INFO:    existing ENWARE mountfloppy scripts updated" >&2
  cp $ADMINDIR/mountfloppy   "$ENWADMINDIR"
  cp $ADMINDIR/unmountfloppy "$ENWADMINDIR"
fi


exit 0
