#!/bin/sh

set -a

umask 022

###################################################################
#
# Environment variable
# TOPDIR ALTLINK WANTTFTP HOSTNAME OS NL1 NL2 COPYMAN and more
# been set in request script for BASE package
# Or for sun4 and sco in installscript
###################################################################

###################################################
# pull in environment variables common to
# postinstall/postremove/request/and.. scripts
###################################################

ADMINDIR=$TOPDIR/usr/bin/X11/700X/admin
. $ADMINDIR/env.vars
. ${ADMINDIR}/HPXTVERSION
NEWDIR=${TOPDIR}/usr/lib/X11/700X.$HPXTVERSION
echo NEWDIR=$NEWDIR >> $ADMINDIR/env.vars


#################################################
#
# Support for NFS
# Put links in /usr/(lib)|(bin)/X11/700X pointing to
# $TOPDIR/usr/(lib)|(bin)/X11/700X
#
#################################################


#################################################
#
# Check if root privilege in $SYSTEMDIRS
#
#################################################

SYSTEMDIRS="/usr/lib/X11 /usr/bin/X11"

CANLINK="true"
for i in $SYSTEMDIRS
do
  if [ ! -d $i ]
  then
    mkdir $i
    if [ $? -ne 0 ]
    then
	 CANLINK="false"
    fi
  fi

  if [ -d $i ]
  then
    touch $i/tmp.$$  2>/dev/null
    if [ $? -ne 0 ]
    then
      CANLINK="false"
    else
      rm $i/tmp.$$
    fi
  fi
done


SERVERHOME1=/usr/lib/X11/700X
SERVERHOME2=${TOPDIR}$SERVERHOME1

UTILDIR1=/usr/bin/X11/700X
UTILDIR2=${TOPDIR}$UTILDIR1



DOALTLINK="false"

while [ $CANLINK = "false" ]
do
  echo "Can not create symbolic link in directory /usr/lib/X11" 
  echo "1) MANUAL PROCEDURE TO SOLVE THIS." 
  echo "   If you want X Station to boot/get fonts using its built-in default paths" 
  echo "   You need to create 2 symbolic links in /usr/(lib)|(bin)/X11" 
  echo "   The 2 links **you** must now create are:" 
  echo "   $SERVERHOME1 pointing to $SERVERHOME2" 
  echo "   and $UTILDIR1 pointing to $UTILDIR2." 
  echo "   For NFS mounted directories links may have to be created on the NFS server." 
  echo "2) AUTOMATIC PROCEDURE" 
  echo "   An alternative is to have this script modify the bootptab entries for the" 
  echo "   X Stations and boot through a symbolic link ($ALTLINK)." 
  echo "   The font paths in terminals remote configuration files will be modified " 
  echo "   to include this link as well." 
  echo "   If you prefer this alternative the X Station probably **has** to boot using NFS" 
  echo "   Details on NFS booting can be found in \"Enware Technical Reference Guide.\""
  echo "   Later, if the X Station automatic boot fails, please consult this manual." 
  echo $NL1 "enter 1 or 2 or q to quit : $NL2" 
  REPLY=`line`
  case $REPLY in
  1)
    if [ ! -d $SERVERHOME1 ]
    then
      echo "" 
      echo "WARNING: could not find link $SERVERHOME1" 
      echo "enter <return> to continue installation regardless." 
      REPLY=`line`
    fi

    break
    ;;
  2) 
    ln -s $SERVERHOME2 $ALTLINK
    if [ $? -ne 0 ]
    then
      echo "Link creation failed"
      exit 1
    else
      DOALTLINK="true"
      #
      # put ALTLINK in $TOPDIR also
      #
      if [ "$TOPDIR" != "/" -a -d "$TOPDIR" ]
      then
	ln -s $SERVERHOME2 ${TOPDIR}${ALTLINK}
      fi
    fi

    break
    ;;
  q)
    exit 1
    ;;
  *)
    continue
    ;;
  esac
done


#
#   V E R S I O N I N G
#
XTHOME=$TOPDIR/usr/lib/X11/700X

#
#--------------------------------------------------------------------------#
# support of "versioning" as a menu item in xtadm
#--------------------------------------------------------------------------#
#
#

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

if [ $VERSIONING = "no" ]
then
  if [ ! -h $NEWDIR ]
  then
    #
    #--------------------------------------------------# 
    # This code does not get executed anymore
    #--------------------------------------------------# 
    #
    for EXDIR in $EXCLUDED
    do
      rm -rf $NEWDIR/$EXDIR
      mv $XTHOME/$EXDIR $NEWDIR/$EXDIR
    done

    # remove everything in $XTHOME
    # (it's empty if this is not a reinstall)
    #
    rm -rf $XTHOME

    # 
    # make new stuff from tape the only version .. 
    #
    mv $NEWDIR $XTHOME
    ln -s $SERVERHOME1 $NEWDIR

    # remove old "NFS" links
    rm -f ${SERVERHOME1}.*
  else
    if [ $CANLINK = "true" ]
    then
      #
      #--------------------------------------------------# 
      # Only this code gets executed
      #--------------------------------------------------# 
      #

      # remove old "NFS" links
      rm -f ${SERVERHOME1}.*

      # Replace absoulute (NFS) link with link through /usr/lib/X11/700 
      rm $NEWDIR
      ln -s $SERVERHOME1 $NEWDIR
    fi
  fi
fi


if [ $VERSIONING = "yes" ]
then
  NVER=`$ADMINDIR/xtutil_GetVersion $NEWDIR/bin`
  OVER=`$ADMINDIR/xtutil_GetVersion $XTHOME/bin`
  
  if [ "$OVER" = "$NVER" ]
  then
    echo "INFO:    Seems to be a reinstallation of version $OVER of ENWARE"
    echo "         existing version of $XTHOME not saved"
  else
    rm -f $XTHOME.$OVER
    ln -s `basename $XTHOME` $XTHOME.$OVER

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

    for EXDIR in $EXCLUDED
    do
      rm -f $NEWDIR/$EXDIR
      ln -s $SERVERHOME1/$EXDIR $NEWDIR/$EXDIR
    done
  fi
fi

#
# Create link for NFS support
#

rm -f /usr/lib/X11/700X.$HPXTVERSION
ln -s $NEWDIR /usr/lib/X11/700X.$HPXTVERSION 


if [ $CANLINK = "true" ]
then
  rm ${SERVERHOME1} 2>/dev/null
  if [ -d ${SERVERHOME1} ]
  then
     mv ${SERVERHOME1} ${SERVERHOME1}.old.$$ 2>/dev/null
  fi
  ln -s ${SERVERHOME2} ${SERVERHOME1}
  if [ $? -ne 0 ]
  then
    echo "Creating link:" 
    echo "$SERVERHOME1 pointing to $SERVERHOME2" 
    echo "" 
    echo "link creation failed(9)" 
    echo "" 
    exit 1
  fi

  rm ${UTILDIR1} 2>/dev/null
  if [ -d ${UTILDIR1} ]
  then
     mv ${UTILDIR1} ${UTILDIR1}.old.$$ 2>/dev/null
  fi
  ln -s ${UTILDIR2} ${UTILDIR1}
  if [ $? -ne 0 ]
  then
    echo "" 
    echo "Creating link:" 
    echo "$UTILDIR1 pointing to $UTILDIR2." 
    echo 'link creation failed(9)' 
    echo "" 
    exit 1
  fi
fi

########################################################################
##
# In the normal case the link /tftpboot/tftpboot -> /tftpboot
# already exists. If it doesn't let's put it there so that
# chances of booting increases in cases where /usr/lib/X11/700X is absent
# Anyway it does not do any harm.
########################################################################
##

if [ "$TOPDIR" = "/tftpboot" ]
then
  if [ ! -h /tftpboot/tftpboot ]
  then
    ln -s /tftpboot /tftpboot/tftpboot
  fi
fi


#
#		Postprocess man pages
#		eg move them to system man page area
#		if permission granted


case $OS in
  sun*)
    SMANDIR=$TOPDIR/usr/share/man
    DMANDIR=/usr/share/man
    MANPAGES="man1/xtsnftosnf.1 man1/xtshowsnf.1 man1/xtbdftosnf.1 man1/xdm.1m \
	      man1/bootpd.1m man1/bootpquery.1m man1/sessreg.1m"
    ;;
  sco)
    SMANDIR=$TOPDIR/usr/share/man/man1
    DMANDIR=/usr/man/cat.LOCAL
    MANPAGES="xtsnftosnf.LOCAL xtshowsnf.LOCAL"
    ;;
esac


if [ "$COPYMAN" = "yes" ]
then
  for i in $MANPAGES
  do
    if [ ! -d $DMANDIR ]
    then
      mkdir -p $DMANDIR
    fi
    cp $SMANDIR/$i $DMANDIR/$i > /dev/null
    if [ $? -ne 0 ]
    then
      echo "" 
      echo "Copy of man page failed." 
      echo "$DMANDIR is probably nfs mounted or mounted read-only." 
      echo "Please install manually after installation" 
      echo "" 
      #
      # For other install scripts
      # record that we cannot copy into $DMANDIR 

      TMPENVVARS=/tmp/env.vars.$$
      sed "s!COPYMAN=yes!COPYMAN=no!g" $ADMINDIR/env.vars > $TMPENVVARS
      cp $TMPENVVARS $ADMINDIR/env.vars
      rm $TMPENVVARS

      break
    fi
    chmod 444  $DMANDIR/${i}
    chown root $DMANDIR/${i}
    chgrp bin  $DMANDIR/${i}
  done
fi


#################################################
# XDM files
#################################################

#################################################
# modify xdm-config depending on where xrdb lives
# Problem is: xrdb may/may not live in /usr/bin/X11
# an alternative location is $OPENWINHOME/bin 
#################################################

XDMDIR=/usr/lib/X11/xdm
if [ "$OPENWINHOME"x = "x" ]
then
  XRDBBIN=/usr/openwin/bin/xrdb
  if [ ! -x $XRDBBIN ]
  then
    if [ -x /usr/bin/X11/xrdb ]
    then
      XRDBBIN=/usr/bin/X11/xrdb
    fi
  fi
else
  XRDBBIN=$OPENWINHOME/bin/xrdb
fi


if [ -x $XRDBBIN ]
then
  echo "DisplayManager*xrdb: $XRDBBIN" >> ${TOPDIR}${XDMDIR}/xdm-config 
else
	FLAG=0;
	echo ''
	echo 'cannot find xrdb'
	echo $NL1 "Please enter full path name for xrdb. $NL2"
	read SEL
	while [ $FLAG -eq 0 ]
	do
	  if [ "$SEL"x != x ]
	  then
		echo "$SEL" | grep 'xrdb' >/dev/null
		if [ $? -a -f $SEL ]
		then
			FLAG=1
		elif [ -d $SEL ]
		then
			SEL=$SEL/xrdb
			SEL=`echo "$SEL" | sed "s!//!/!g"`
			if [ -f $SEL ]
			then
				FLAG=1
			fi
		fi
	fi
	if [ $FLAG -eq 0 ]
	then
		case $SEL in
			Q|q)
			echo ''
			echo 'A line similar to:'
			echo '  DisplayManager*xrdb: '
			echo 'will be added to your xdm-config file.  For xdm'
			echo 'to run properly you must enter the correct path'
			echo 'after the colon.'
			echo ''
			echo $NL1 "Hit return to continue.$NL2"
			read SEL
  			echo "DisplayManager*xrdb: " >> ${TOPDIR}${XDMDIR}/xdm-config 
			FLAG=1      #User requested skip of path entry.
			break
			;;
			*)
			echo 'There is no xrdb in this path.'
			echo $NL1 "Please reenter or press q for quit: $NL2"
			read SEL
			;;
		esac
	else
  		echo "DisplayManager*xrdb: $SEL" >> ${TOPDIR}${XDMDIR}/xdm-config 
		FLAG=1
	fi
done
fi



############################################################
# modify xdm-config depending on where xdm lives
# xdm assumes its config files are in $XDMDIR 
# config files in turn have hard coded paths also to $XDMDIR 
# These are here changed to ${TOPDIR}${XDMDIR} 
############################################################

NEWXDMLIB=${TOPDIR}${XDMDIR}
XDMFILES="xdm-config Xsession"

for i in $XDMFILES
do
 if [ -f $NEWXDMLIB/$i ]
 then
   egrep "$NEWXDMLIB" $NEWXDMLIB/$i > /dev/null
   if [ $? -ne 0 ]
   then
     sed "s!$XDMDIR!$NEWXDMLIB!g" $NEWXDMLIB/$i > /tmp/$i
     if [ -f $NEWXDMLIB/$i ]
     then
	mv $NEWXDMLIB/$i $NEWXDMLIB/${i}.old
     fi
     cp /tmp/$i $NEWXDMLIB/$i
     chmod 755 $NEWXDMLIB/$i
     rm /tmp/$i
   fi
 fi
done

################################################################
# do the same for xdm-config: /usr/bin/X11(sessreg)(chooser)
################################################################


egrep "$TOPDIR/usr/bin/X11" $NEWXDMLIB/xdm-config > /dev/null
if [ $? -ne 0 ]
then
  sed "s!/usr/bin/X11!$TOPDIR/usr/bin/X11!g" $NEWXDMLIB/xdm-config \
	> /tmp/xdm-config.$$
  if [ -f $NEWXDMLIB/xdm-config ]
  then
    mv $NEWXDMLIB/xdm-config $NEWXDMLIB/xdm-config.old
  fi
  cp /tmp/xdm-config.$$ $NEWXDMLIB/xdm-config
  rm /tmp/xdm-config.$$
fi


if [ $OS != "sco" ]
then
  ############################################################
  # modify xdm-config depending on where xdm lives
  # adding xdm's path to the system path.
  # This is done so xdm can find sessreg.
  ############################################################
  NEWXDMDIR=${TOPDIR}/usr/bin/X11

  grep systemPath $NEWXDMLIB/xdm-config > /dev/null 2>&1
  if [  $? -eq 0 ]
  then
	grep systemPath $NEWXDMLIB/xdm-config | grep $NEWXDMDIR > /dev/null
	if [ $? -ne 0 ]
	then
	  NEWXDMDIR=`echo $NEWXDMDIR | sed s!/!\\\\\\\\/!g`
	  sed "/systemPath/s/$/:/usr/openwin/bin:$NEWXDMDIR/" \
		$NEWXDMLIB/xdm-config > /tmp/xdm-config
	  cp /tmp/xdm-config $NEWXDMLIB/xdm-config
	  rm /tmp/xdm-config
	fi
  else
	echo "DisplayManager*systemPath:    /etc:/bin:/usr/bin:/usr/bin/X11:/usr/ucb:/usr/openwin/bin:$NEWXDMDIR" >> $NEWXDMLIB/xdm-config
  fi


  ############################################################
  # xdm gets rid of whole environment,
  # so modify xdm-config so that some environment variables 
  # (if set) get passed on to user xdm sessions
  ############################################################

  PASSON="OPENWINHOME LD_LIBRARY_PATH"
  echo "DisplayManager.exportList:  $PASSON" >> $NEWXDMLIB/xdm-config


  #######################################################
  # modify xdm Xresources file to include login host name
  # in xdm greet screen message
  #######################################################

  XRESOURCES="Xresources"

  sed "s!HP X-Station!HP X-Station.  Host: $HOSTNAME!g" \
	$NEWXDMLIB/$XRESOURCES > /tmp/$XRESOURCES
  cp /tmp/$XRESOURCES $NEWXDMLIB/$XRESOURCES
  rm /tmp/$XRESOURCES
fi



#################################################
#
# UPDATE THE FONTS.DIRs 
#
#################################################

FONTUTILDIR=${TOPDIR}/usr/bin/X11/700X
if [ ${OPENWINHOME}x != "x" ]
then
  MKFONTDIR=$OPENWINHOME/bin/mkfontdir
else
  if [ -f /usr/bin/X11/mkfontdir ]
  then
    MKFONTDIR=/usr/bin/X11/mkfontdir
  else
    if [ -f $FONTUTILDIR/xtmkfontdir ]
    then
      MKFONTDIR=$FONTUTILDIR/xtmkfontdir
    else
      MKFONTDIR=/usr/openwin/bin/mkfontdir
    fi
  fi
fi

if [ -f $MKFONTDIR ]
then
  DIRS="Xol iso_8859.1/75dpi iso_8859.1/100dpi hp_roman8/75dpi r2 misc"
  for DIR in $DIRS
  do
    if [ -d "$DIR" ]
    then
      echo making font directory for $DIR
      $MKFONTDIR $TOPDIR/usr/lib/X11/700X/fonts/$DIR
    fi
  done
else
  echo could not find mkfontdir. Please run mkfontdir manually on directories:
  echo $DIRS in $TOPDIR/usr/lib/X11/700X/font
fi



#################################################
#
# DO BOOTP 
#
#################################################

#
# Use eventual existing bootptab if there.
# If none present use the one from tape 
#
if [ "$OS" != "sco" ]
then
  if [ ! -f $TOPDIR/usr/etc/700X/bootptab ]
  then
    cp $TOPDIR/usr/etc/700X/bootptab.new $TOPDIR/usr/etc/700X/bootptab
  fi
fi



#################################################
# UPDATE /etc/services if necessary
# eg no bootp lines in it
#################################################

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


###############################################
#
# Are we running NIS+ for services
#
#################################################

NISPLUSS="false"

if [ -f /etc/nsswitch.conf ]
then
  awk '$1 == "services:" &&  $2 == "nisplus"' /etc/nsswitch.conf \
		| fgrep nisplus > /dev/null

  if [ $? -eq 0 ]
  then
    NISPLUSS="true"
  fi
fi

#
# Get YP master for services map
#
AMSMASTER=false
NISSMASTER=`ypwhich -m services 2>&1`
echo $NISSMASTER | egrep 'ypwhich:' > /dev/null

if [ $? -eq 0 ]
then
  NISS=false
else
  NISS=true
  if [ $NISSMASTER = $HOSTNAME ]
  then
    AMSMASTER=true
  fi
fi

NISSMAP="undefined"

if [ $NISS = "true" -a $NISPLUSS = "false" ]
then
  ypcat services | egrep '^bootps.*67/udp' > /dev/null
  if [ $? -eq 0 ]
  then
    NISSMAP="ok"
  fi
else
  cat /etc/services | egrep '^bootps.*67/udp' > /dev/null
  if [ $? -eq 0 ]
  then
    NISSMAP="ok"
  fi
fi
  
if [ $NISS = "true" -a $NISSMAP != "ok" -a $AMSMASTER = "false" -a $NISPLUSS = "false" ]
then
  echo "" 
  echo "You seem to be running the NIS services map from $NISSMASTER." 
  echo "In order for bootp to work and automatically boot terminals," 
  echo "please go to NIS master and append lines like the ones below" 
  echo "to services map (usually /etc/services). Then do a \"cd /var/yp; make services\""  
  echo $BOOTPS 
  echo $BOOTPC 
  case $OS in
    sun5)
      echo "" 
      ;;
    *)
      echo "When you are done please enter <return> to continue installation." 
      echo "" 
      REPLY=`line`
      ;;
  esac 
fi

DONISS="false"
if [ $NISSMAP != "ok" -a $AMSMASTER = "true" ]
then
  DONISS="true"
else
  if [ $NISSMAP != "ok" -a $NISS = "false" ]
  then
    DONISS="true"
  fi
fi
 
  
if [ $DONISS = "true" -a $NISPLUSS = "false" ]
then
  ###############################################
  #
  # Save existing versions of /etc/services
  #
  #################################################
  echo "" 
  FILES="services"
  for i in $FILES
  do
    cp /etc/${i} /tmp/${i}.$$
    echo Existing version of /etc/${i} copied to /tmp/${i}.$$  
  done


  echo "Appending bootps and bootpc lines to       /etc/services" 

  # BOOTPS part
  ###########################################
  grep '^bootps' /etc/services >/dev/null
  if [ $? != 0 ]
  then
    echo "# Following line added for HP X Station" >>/etc/services
    echo "$BOOTPS" >>/etc/services
    if [ $? -ne 0 ]
    then
      echo 'append failed(14)' 
      exit 1
    fi
  else
    /bin/sed -e 's/bootps/#bootps/g' /etc/services > /etc/services.tmp
    if /bin/cp /etc/services.tmp /etc/services && \
   	/bin/rm /etc/services.tmp 2>/dev/null
    then
      echo "# Following line added for HP X Station " >>/etc/services
      echo "$BOOTPS" >>/etc/services
      if [ $? -ne 0 ]
      then
        echo 'append failed' 
        exit 1
      fi
    else
      echo "FATAL ERROR:   Could not restore /etc/services file"  
      echo "         from /etc/services.tmp. "  
      exit 1
    fi
  fi
 
  # BOOTPC part
  ###########################################

  grep '^bootpc' /etc/services >/dev/null
  if [ $? != 0 ]
  then
    echo "# Following line added for HP X Station" >>/etc/services
    echo "$BOOTPC" >>/etc/services
    if [ $? -ne 0 ]
    then
       echo 'append failed' 
       exit 1
    fi
  else
    /bin/sed -e 's/bootpc/#bootpc/g' /etc/services > /etc/services.tmp
    if /bin/cp /etc/services.tmp /etc/services && \
   	  /bin/rm /etc/services.tmp 2>/dev/null
    then
      echo "# Following line added for HP X Station" >>/etc/services
      echo "$BOOTPC" >>/etc/services
      if [ $? -ne 0 ]
      then
        echo 'append failed' 
        exit 1
      fi
    else
      echo "ERROR:   Could not restore /etc/services file"  
      echo "         from /etc/services.tmp. " 
    fi
  fi 
fi


if [ $DONISS = "true" -a $NISS = "true" -a $NISPLUSS = "false" ]
then
  case $OS in
    sco)
      MAKESCMD="/usr/etc/yp/ypmake services"
      ;;
    sun*)
      MAKESCMD="cd /var/yp; make services"
      ;;
    *)
      echo do not know OS=$OS
      exit 1
      ;;
  esac

  echo "" 
  #echo $NL1 "Do a \"$MAKESCMD\" ? [y|n] (default=y): $NL2" 
  #REPLY=`line`
  REPLY="yes"
  case $REPLY in
	y*|Y*|"")
		echo $MAKESCMD | /bin/sh	;;
	n*|N*|*)
		: 				;;	
  esac
fi



###############################################
#
# Are we running NIS+ for services
#
#################################################

if [ $NISPLUSS = "true" ]
then
  #
  ##################################
  # yes, do we need to update maps?
  ##################################
  #
  nismatch name=bootps services.org_dir > /dev/null
  if [ $? -ne 0 ]
  then
    # 
    ################################################
    # we need to update maps, better ask permission
    ################################################
    # 
    while true 
    do
      echo ""
      echo ""
      echo "bootp service not in NIS+ database"
      echo ""
      CMD="echo bootps 67/udp | /usr/lib/nis/nisaddent -a services"
      #echo "Execute the command \"$CMD\""
      #echo $NL1 "to add bootp service to the NIS+ services map ? [y|n] (default=y): $NL2"
      #SEL=`line`
      SEL="yes"
      case $SEL in
        y*|Y*|"")

	  echo $CMD | /bin/sh

          nismatch name=bootpc services.org_dir > /dev/null
          if [ $? -ne 0 ]
          then
            CMD="echo bootpc 68/udp | /usr/lib/nis/nisaddent -a services"
	    echo $CMD | /bin/sh
          fi

	  break
          ;;
        n|N|NO|no)
	  break
          ;;
        *)
	  :
          ;;
      esac
    done
  else
      echo
      echo "bootp service already in NIS+ database"
  fi
fi



#################################################
# UPDATE /etc/inetd.conf
#################################################
 
 #
 # In order to better co-exist with other versions of bootp:
 # if /etc/bootpd seems to be there already use it
 # instead of version from tape
 # Also, if a bootptab seems to be in use use, use that instead
 #
if [ "$OS" = "sco" ]
then
  INETD="bootps dgram   udp     wait    root    /etc/bootpd     bootpd"
else
  INETD="bootps dgram udp wait root $TOPDIR/usr/etc/700X/bootpd bootpd $TOPDIR/usr/etc/700X/bootptab"
fi

BOOTPD=`grep '^bootps' /etc/inetd.conf   | awk '{print $6}' | grep -v 700X`
BOOTPTAB=`grep '^bootps' /etc/inetd.conf | awk '{print $NF}' \
	  | grep -v 700X | grep '/'`
		 
if [ $BOOTPD ]
then
  if [ ! -f $BOOTPD ]
  then
    BOOTPD=""
    BOOTPTAB=""
  else
    if [ $BOOTPTAB ]
    then
      if [ ! -f $BOOTPTAB ]
      then
        BOOTPTAB=""
        BOOTPD=""
      fi
    else
      BOOTPTAB=/etc/bootptab
    fi
  fi
else
  BOOTPTAB=""
fi


grep '^bootps' /etc/inetd.conf >/dev/null
if [ $? != 0 ]
then
  ###############################################
  #
  # Save existing versions of /etc/inetd.conf 
  #
  #################################################
  FILES="inetd.conf"
  for i in $FILES
  do
    cp /etc/${i} /tmp/${i}.$$
    echo Existing version of /etc/${i} copied to /tmp/${i}.$$  
  done

  echo "Appending bootps line to                /etc/inetd.conf" 
  echo "#" >>/etc/inetd.conf
  echo "# Following line added for HP X Station" >>/etc/inetd.conf
  echo "$INETD" >> /etc/inetd.conf
  echo "#" >>/etc/inetd.conf
  if [ $? -ne 0 ]
  then
    echo 'append failed' 
    exit 1
  fi
else
  if [ $BOOTPD ]
  then
    :
  else
    #
    # Import existing bootptab (probably from previous enware install)
    #
    if [ $OS != "sco" ]
    then
      OBPTAB=`egrep '^bootps' /etc/inetd.conf | awk '{print $NF}'`
      if [ -f "$OBPTAB" ]
      then
        if [ "$OBPTAB" != "$TOPDIR/usr/etc/700X/bootptab" ]
        then
          echo copying existing bootptab to $TOPDIR/usr/etc/700X/bootptab
          cp $OBPTAB $TOPDIR/usr/etc/700X/bootptab
        fi
      fi
    fi

    /bin/sed -e 's/^bootps/#bootps/' /etc/inetd.conf > /etc/inetd.tmp
    if /bin/cp /etc/inetd.tmp /etc/inetd.conf && \
	/bin/rm /etc/inetd.tmp 2>/dev/null
    then
      echo "# Following line added for HP X Station" >>/etc/inetd.conf
      echo "$INETD" >>/etc/inetd.conf
      if [ $? -ne 0 ]
      then
        echo 'append failed' 
        exit 1
      fi
    else
      echo "FATAL ERROR:   Could not restore /etc/inetd.conf file" 
      echo "         from /etc/inetd.tmp. " 
      exit 1
    fi
  fi
fi

if [ $BOOTPTAB ]
then
  echo xtadm terminal administration script will use existing $BOOTPTAB
  sed 's!BOOTPTABLE=${INSTALLDIR}/usr/etc/700X/bootptab!BOOTPTABLE=/etc/newbootptab!g' $ADMINDIR/xtadm  > /tmp/xtadm1.$$
  sed "s!BOOTPTABLE=/etc/newbootptab!BOOTPTABLE=$BOOTPTAB!g" \
	      /tmp/xtadm1.$$  > /tmp/xtadm2.$$
			 
  sed 's!BOOTPPARAM=${INSTALLDIR}/usr/etc/700X/bootptab!BOOTPPARAM=/etc/newbootptab!g'  /tmp/xtadm2.$$  > /tmp/xtadm3.$$
  sed "s!BOOTPPARAM=/etc/newbootptab!BOOTPPARAM=$BOOTPTAB!g" \
     /tmp/xtadm3.$$  > /tmp/xtadm4.$$
  if [ -s /tmp/xtadm4.$$ ]
  then
    cp /tmp/xtadm4.$$ $ADMINDIR/xtadm
  else
    echo editing of $ADMINDIR/xtadm for $BOOTPD and $BOOTPTAB failed
  fi
  rm /tmp/xtadm*.$$
fi
						  
######################################################
# stop running bootp procs
######################################################

case $OS in
  sun5|sco)
    PID=`ps -e | awk '$4 == "bootpd" {print $1 }' | grep .`
    ;;
  sun4)
    PID=`ps -axj | awk '$10 == "bootpd" {print $4 }' | grep .`
    ;;
esac

if [ $? -eq 0 ]
then
  if [ "$PID" != "" ]
  then
    echo $PID | egrep '[0-9]'  > /dev/null
    if [ $? -eq 0 ]
    then
      echo $PID | egrep '[^0-9]' > /dev/null
      if [ $? -ne 0 ]
      then
        echo killing running bootpd process
        kill $PID
      fi
    fi
  fi
fi


######################################################
# signal inetd that /etc/inetd.conf should be re-read.
######################################################

echo "" 

#
# Can't seem to make "ps -ef" work reliably on this OS
#
case $OS in
  sun5|sco)
    PID=`ps -e | awk '$4 == "inetd" {print $1 }' | grep .`
    ;;
  sun4)
    PID=`ps -axj | awk '$10 == "inetd" {print $4 }' | grep .`
    ;;
esac

if [ $? -ne 0 ]
then
         echo "WARNING: No inetd process running!!" 
else
  if [ "$PID" != "" ]
  then
    echo $PID | egrep '[0-9]'  > /dev/null
    if [ $? -eq 0 ]
    then
      echo $PID | egrep '[^0-9]' > /dev/null
      if [ $? -ne 0 ]
      then
        kill -1 $PID
        sleep 2
        echo "Signalled inetd to re-read /etc/inetd.conf" 
      else
        echo "Internal error 1: inetd pid=($PID)." 
      fi
    else
      echo "Internal error 2: inetd pid=($PID)." 
    fi
  else 
    echo "WARNING: Couldn't send SIGHUP to inetd." 
    echo "         inetd pid=($PID)." 
  fi
fi



#################################################
# Record for administration scripts where software was installed.
# Also whether transport nfs or tftp
##########################################################

sed "s!INSTALLDIR=unknown!INSTALLDIR=${TOPDIR}!g" $ADMINDIR/xtadm \
    > /tmp/xtadm
cp /tmp/xtadm $ADMINDIR/xtadm
rm /tmp/xtadm

if [ $WANTTFTP = "false" ]
then
  sed "s!FILEACCESS=tftp!FILEACCESS=nfs!" $ADMINDIR/xtadm \
    > /tmp/xtadm
  cp /tmp/xtadm $ADMINDIR/xtadm
  rm /tmp/xtadm
fi


if [ $OS != "sco" ]
then
  ###########################################################################
  # Add font directory /usr/lib/X11/700X/fonts/Xol
  # to remote config file. 
  ###########################################################################

  for CFGFILE in `ls $SERVERHOME2/config/*.cfg`
  do
	#
	# Already edited?
	#
    grep '/fonts/Xol' $CFGFILE > /dev/null
    if [ $? -ne 0 ]
    then
      TMPCFG=/tmp/$$.tmp1
	# no, do it
      sed -e '/Font Path.*=.*fonts.*default/s!# Font Path!Font Path!g' \
          -e '/Font Path/s!/rom/fonts/default/!/rom/fonts/default/,./fonts/Xol/!g' $CFGFILE > $TMPCFG

      if [ -s $TMPCFG ]
      then
        cp $TMPCFG $CFGFILE
      fi
      rm -f $TMPCFG
    fi
  done
fi

##############################################################
# Record for administration scripts where bootp/bootptab
# are in case we want to use some already existing versions
###############################################################
if [ $BOOTPD ]
then
  echo xtadm administration script will use existing $BOOTPD
  sed 's!BOOTPD=${INSTALLDIR}/usr/etc/700X/bootpd!BOOTPD=/etc/newbootpd!g' \
    $ADMINDIR/xtadm  > /tmp/xtadm
  sed "s!BOOTPD=/etc/newbootpd!BOOTPD=$BOOTPD!g"  /tmp/xtadm > $ADMINDIR/xtadm
  rm /tmp/xtadm
fi


##############################################################################
# if we have to boot through the alternate link in root file system
# then record this in remote configuration files, modules.ld and admin scripts
# In remote config files NFS becomes transport and fonts are loaded
# through the alternate link.
##############################################################################

if [ $DOALTLINK = "true" ]
then
  for CFGFILE in `ls $SERVERHOME2/config/*.cfg`
  do
    cp $CFGFILE $CFGFILE.orig
    sed "s!/usr/lib/X11/700X!$ALTLINK!g" $CFGFILE > $$.tmp3
    sed "s!# Access Mechanism!Access Mechanism = NFS    # !g" $$.tmp3 > $$.tmp4
    sed "s!# Remote Config!Remote Config = On    # !g" $$.tmp4 > $$.tmp5
    cp $$.tmp5 $CFGFILE
    rm $$.tmp3 $$.tmp4 $$.tmp5 
  done

  for CFGFILE in `ls $SERVERHOME2/bin/modules.ld`
  do
    cp $CFGFILE $CFGFILE.orig
    sed "s!/usr/lib/X11/700X!$ALTLINK!g" $CFGFILE > $$.tmp1
    cp $$.tmp1 $CFGFILE
    rm $$.tmp1
  done

  sed "s!ALTLINK=none!ALTLINK=$ALTLINK!" $ADMINDIR/xtadm \
	> /tmp/xtadm
  cp /tmp/xtadm $ADMINDIR/xtadm
  rm /tmp/xtadm
fi
							  

#---------------------------------------------------------------
#
# Update XKeysymDB with various HP proprietary keysyms
# (if necessary)
#---------------------------------------------------------------

KEYSYMDBS="/usr/lib/X11/XKeysymDB /usr/openwin/lib/XKeysymDB"

for KEYSYMDB in $KEYSYMDBS
do
  if [ -f $KEYSYMDB ]
  then
    egrep '^InsertLine' ${KEYSYMDB} >                         /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        echo "!HP specific keysym" >> $KEYSYMDB  >         /dev/null 2>&1
        echo "InsertLine        : 1000FF70" >> $KEYSYMDB > /dev/null 2>&1
    fi

    egrep '^DeleteLine' ${KEYSYMDB} > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        echo "!HP specific keysym" >> $KEYSYMDB >          /dev/null 2>&1
        echo "DeleteLine        : 1000FF71" >> $KEYSYMDB > /dev/null 2>&1
    fi

    egrep '^InsertChar' ${KEYSYMDB} > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        echo "!HP specific keysym" >> $KEYSYMDB >          /dev/null 2>&1
        echo "InsertChar        : 1000FF72" >> $KEYSYMDB > /dev/null 2>&1
    fi

    egrep '^DeleteChar' ${KEYSYMDB} > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        echo "!HP specific keysym" >> $KEYSYMDB >          /dev/null 2>&1
        echo "DeleteChar        : 1000FF73" >> $KEYSYMDB > /dev/null 2>&1
    fi
  fi
done


#
#----------------------------------------------------------------------
# update modules.ld with extensions from this fileset
# Curent directory is where tar took place
# so we can use relative path names
#----------------------------------------------------------------------

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

if [ -f $MODULES ]
then
  cp $MODULES $MODULES.old.$$
  echo "         existing modules.ld saved as modules.ld.old.$$" >&2
fi

mv $SMODULES $MODULES


#--------------------------------------------------------
# update modules.ld with info from filesets in this packages
#--------------------------------------------------------
#
PARTS="audio floppy nfs scanner"
for PART in $PARTS
do
  cat $NEWDIR/bin/modules.$PART >> $NEWDIR/bin/modules.ld
  rm -f $NEWDIR/bin/modules.$PART
done


if [ $OS = "sun5" ]
then

  ###########################################################################
  # Record in xdm startup/shutdown scripts where software was installed.
  ############################################################################

  TMPFILE=/tmp/xdm.hp
  sed "s!INSTALLDIR=""!INSTALLDIR=${TOPDIR}!g" $ADMINDIR/xdm.hp  > $TMPFILE 
  cp  $TMPFILE $ADMINDIR/xdm.hp
  rm  $TMPFILE 
fi

case $OS in
  sco)
    MENUMERGE=menumerge
    HW_MENU=/usr/lib/sysadm/.menu-hardware
    MENU_700RX=$ADMINDIR/sysadmmenu

    $MENUMERGE $HW_MENU $MENU_700RX
    if [ $? -ne 0 ]
    then
      echo "" 
      echo $MENUMERGE failed 
      else
      echo "" 
      echo "HP X Station administration script added to **sysadmsh** "
    fi
    ;;
esac


# 
# Aptrex migration. 
#
# Creates the following links 
#

if [ "x$APTREX_INSTALLDIR" != "x" ]
then
   if [ $CANLINK = "true" ]
   then
      echo 
      echo "Creating links from Aptrex directories to Enware directories"
      echo 

      if [  -d $APTREX_INSTALLDIR/xthome ]
      then 
         mv $APTREX_INSTALLDIR/xthome $APTREX_INSTALLDIR/xthome.aptrex 2>&1 > /dev/null
         if [ $? -eq 0 ]
         then
           echo  $APTREX_INSTALLDIR/xthome now is pointing to  $SERVERHOME1
           ln -s  $SERVERHOME1  $APTREX_INSTALLDIR/xthome
           ( 
             #-------------------------------------------------------#
             # Some Aptrex font paths were shorter, 
             # so make compatibility links
             #
             cd $SERVERHOME1/fonts 
             ln -s iso_8859.1/75dpi 75dpi
             ln -s iso_8859.1/100dpi 100dpi
           ) 
         fi     
      fi 

      if [  -d $APTREX_INSTALLDIR/bin ]
      then
         mv $APTREX_INSTALLDIR/bin $APTREX_INSTALLDIR/bin.aptrex 2>&1 > /dev/null
         if [ $? -eq 0 ]
         then
             echo  $APTREX_INSTALLDIR/bin is now pointing to  $UTILDIR1
             ln -s  $UTILDIR1 $APTREX_INSTALLDIR/bin     
         fi
      fi

      if [  -d $APTREX_INSTALLDIR/etc ]
      then
         # 
         # copy bootptab to new destination
         #
         
         if [ -f $APTREX_INSTALLDIR/etc/bootptab ]
         then
             if [ -f  $TOPDIR/usr/etc/700X/bootptab ] 
             then
                 mv $TOPDIR/usr/etc/700X/bootptab $TOPDIR/usr/etc/700X/bootptab.old
             fi
             cp $APTREX_INSTALLDIR/etc/bootptab $TOPDIR/usr/etc/700X > /dev/null 2>&1
         fi

         mv $APTREX_INSTALLDIR/etc $APTREX_INSTALLDIR/etc.aptrex 2>&1 > /dev/null
         if [ $? -eq 0 ]
         then
             echo $APTREX_INSTALLDIR/etc is now pointing to $TOPDIR/usr/etc/700X
             ln -s   $TOPDIR/usr/etc/700X  $APTREX_INSTALLDIR/etc     
         fi
      fi

      if [  -d $APTREX_INSTALLDIR/man  ]
      then 
         mv $APTREX_INSTALLDIR/man $APTREX_INSTALLDIR/man.aptrex 2>&1 > /dev/null
         if [ $? -eq 0 ]
         then
             echo $APTREX_INSTALLDIR/man is now pointing to  $TOPDIR/usr/share/man
             ln -s   $TOPDIR/usr/share/man   $APTREX_INSTALLDIR/man
         fi
      fi

      if [  -d $APTREX_INSTALLDIR/src ]
      then 
         mv $APTREX_INSTALLDIR/src $APTREX_INSTALLDIR/src.aptrex 2>&1 > /dev/null
         if [ $? -eq 0 ]
         then
             echo $APTREX_INSTALLDIR/src is now pointing to $SERVERHOME1/peer
             ln -s   $SERVERHOME1/peer   $APTREX_INSTALLDIR/src
         fi
      fi 

      if [  -d $APTREX_INSTALLDIR/doc ]
      then
          mv $APTREX_INSTALLDIR/doc $APTREX_INSTALLDIR/doc.aptrex 2>&1 > /dev/null
          if [ $? -eq 0 ]
          then
              echo $APTREX_INSTALLDIR/doc is now pointing to $SERVERHOME1/doc
              ln -s   $SERVERHOME1/doc   $APTREX_INSTALLDIR/doc
          fi
      fi

      if [  -d  $APTREX_INSTALLDIR/examples ]
      then 
          mv $APTREX_INSTALLDIR/examples $APTREX_INSTALLDIR/examples.aptrex 2>&1 > /dev/null
          if [ $? -eq 0 ]
          then
              echo $APTREX_INSTALLDIR/examples is now pointing to $SERVERHOME1/examples
              ln -s   $SERVERHOME1/examples   $APTREX_INSTALLDIR/examples
          fi
      fi

      if [ -d $APTREX_INSTALLDIR/xdm -a ! -d  $APTREX_INSTALLDIR/xdm.aptrex  ] 
      then 
          mv $APTREX_INSTALLDIR/xdm $APTREX_INSTALLDIR/xdm.aptrex 2>&1 > /dev/null
          if [ $? -eq 0 ]
          then
             mkdir $APTREX_INSTALLDIR/xdm 
             echo  $APTREX_INSTALLDIR/xdm/bin is now pointing to $TOPDIR/usr/bin/X11
             ln -s  $TOPDIR/usr/bin/X11  $APTREX_INSTALLDIR/xdm/bin 
             
             echo $APTREX_INSTALLDIR/xdm/lib is now pointing to $TOPDIR/usr/lib/X11/xdm
             ln -s  $TOPDIR/usr/lib/X11/xdm  $APTREX_INSTALLDIR/xdm/lib 
          fi
      fi

      if [ -d $APTREX_INSTALLDIR/printer -a ! -d $APTREX_INSTALLDIR/printer.aptrex ] 
      then 
          mv $APTREX_INSTALLDIR/printer $APTREX_INSTALLDIR/printer.aptrex 2>&1 > /dev/null
          if [ $? -eq 0 ]
          then
              
             echo $APTREX_INSTALLDIR/printer is now pointing to  $TOPDIR/usr/lib/hpnp
             ln -s  $TOPDIR/usr/lib/hpnp/  $APTREX_INSTALLDIR/printer 
                         
          fi

      fi

      if [ -d $APTREX_INSTALLDIR/config ]
      then 
          # Modify font path in existing configuration files
          
          for CFGFILE in `ls $APTREX_INSTALLDIR/config/*.cfg` 
          do 
             sed  -e '/Font[ ]*Path/s!/fonts/75dpi!/fonts/hp_roman8/75dpi/,./fonts/iso_8859.1/75dpi!g'  \
             -e '/Font[ ]*Path/s!/fonts/100dpi!/fonts/iso_8859.1/100dpi!g'  $CFGFILE  > $CFGFILE.$$ 
             cp $CFGFILE.$$ $SERVERHOME1/config/`basename $CFGFILE`
             rm $CFGFILE.$$
          done
          
           mv $APTREX_INSTALLDIR/config $APTREX_INSTALLDIR/config.aptrex
           ln -s $SERVERHOME1/config $APTREX_INSTALLDIR/config
        
        fi
   else
       echo "You must create symbolic links from Aptrex directories to Enware"
       echo "directories manually."
   fi
fi
  

# Make sure NFS knows about the installtion directory
#
exportfs -a > /dev/null 2>&1


exit 0
