#!/bin/sh

set -a

umask 022

###################################################################
#
# Environment variables
# TOPDIR WANTTFTP HOSTNAME OS NL1 NL2 COPYMAN PRODUCT 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
###################################################

ENVVARS=/usr/tmp/env.vars
. $ENVVARS

ADMINDIR=$TOPDIR/enware2/lbin
cp $ENVVARS $ADMINDIR/env.vars

. ${ADMINDIR}/HPXT2VERSION
NEWDIR=${TOPDIR}/$PRODUCT/xthome.$HPXT2VERSION
echo NEWDIR=$NEWDIR >> $ADMINDIR/env.vars


#################################################
#
# Check if we can create the symlink in $LINKDIR
#
#################################################

CANLINK="true"

LINKDIR=/opt/hpxt
SYMLINK=${LINKDIR}/${PRODUCT}
LINKDST=${TOPDIR}/$PRODUCT

if [ "$SYMLINK" != "$LINKDST" ]
then
      if [ ! -d $LINKDIR ]
      then
        mkdir -p $LINKDIR
        if [ $? -ne 0 ]
        then
	 CANLINK="false"
        fi
      fi

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




if [ "$CANLINK" = "false" ]
then
  echo "Cannot create symbolic link in directory $LINKDIR" 
  echo "   If you want Netstation to boot/get fonts using its built-in default paths" 
  echo "   You need to create a symbolic link in $LINKDIR" 
  echo "   The link you now **must** create before continuing is:" 
  echo "   $SYMLINK pointing to $LINKDST" 
  echo "   " 
  echo "   You can accomplish this by typing:" 
  echo "   \(ln -s $LINKDST $SYMLINK\)" 
  echo "   from the unix command line." 
  echo "   For NFS mounted directories links may have to be created on the NFS server." 
  echo "enter <return> to continue. $NL2" 
  REPLY=`line`

  if [ ! -d $SYMLINK ]
  then
    echo "" 
    echo "WARNING: could not find link $SYMLINK" 
    echo "enter <return> to continue installation regardless." 
    REPLY=`line`
  fi

  break
fi


if [ $CANLINK = "true" ]
then
  if [ "$SYMLINK" != "$LINKDST" ]
  then
    if [ -d ${SYMLINK} -o -f ${SYMLINK} ]
    then
       mv ${SYMLINK} ${SYMLINK}.old.$$ 2>/dev/null
    fi
    ln -s ${LINKDST} ${SYMLINK}
    if [ $? -eq 0 ]
    then
      echo "Created symlink $SYMLINK pointing to $LINKDST" 
      echo "" 
    else
      echo "link creation failed" 
      echo "" 
      exit 1
    fi

  fi
fi


#----------------------------------------------------------------------#
#
# to support the new versioning scheme create links:  
# XTHOME        -> NEWDIR
# NEWDIR/fonts  -> ../fonts
# NEWDIR/etc    -> ../etc
# NEWDIR/config -> ../config
# 
#----------------------------------------------------------------------#
# Merge "config" and "etc" directory
#
DIRS="config etc"
for DIR in $DIRS
do
  if [ ! -d ${TOPDIR}/${PRODUCT}/$DIR ]
  then
    mkdir -p ${TOPDIR}/${PRODUCT}/$DIR
  fi

  if [ -d ${NEWDIR}/$DIR ]
  then
    find ${NEWDIR}/$DIR -type f \
      -exec cp {} ${TOPDIR}/${PRODUCT}/$DIR \;
    mv ${NEWDIR}/$DIR ${NEWDIR}/$DIR.SAVE
  fi

  ln -s ../$DIR ${NEWDIR}/$DIR
done

# Just link "fonts"
#
rm -f $NEWDIR/fonts
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.
#----------------------------------------------------------------------#
#
ls $TOPDIR/$PRODUCT/* 2>&1 | egrep -i 'multi.*version' > /dev/null
if [ $? -ne 0 ]
then

  # Merge in 3270 stuff if there
  #
  if [ -d $TOPDIR/$PRODUCT/xthome/tn3270 ]
  then
    echo INFO:    moving tn3270 directory to $TOPDIR/$PRODUCT/xthome.${HPXT2VERSION}
    mv  $TOPDIR/$PRODUCT/xthome/tn3270 $TOPDIR/$PRODUCT/xthome.${HPXT2VERSION}
  fi


  rm -f $TOPDIR/$PRODUCT/xthome
  ln -s xthome.$HPXT2VERSION $TOPDIR/$PRODUCT/xthome

  (
  cd "$TOPDIR/$PRODUCT"
  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/{}
     comm -23 $ODLIST $NDLIST | sort -u | xargs -i mkdir -p $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


########################################################################
##
# 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/opt/hpxt" ]
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=$SYMLINK/share/man
    DMANDIR=/usr/share/man
    MANPAGES="man1/bootpd.1m man1/bootpquery.1m man1/dkmserver.1 man1/hpnpf.1 man1/hpnptyd.1"
    ;;
  sco)
    SMANDIR=$SYMLINK/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
      if [ -s $TMPENVVARS ]
      then
        cp $TMPENVVARS $ADMINDIR/env.vars
        rm $TMPENVVARS
      fi

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


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

FONTUTILDIR=${SYMLINK}/utils
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 $SYMLINK/fonts/$DIR ]
    then
      echo making font directory for $DIR
      $MKFONTDIR $SYMLINK/fonts/$DIR
    fi
  done
else
  echo could not find mkfontdir. Please run mkfontdir manually on directories:
  echo $DIRS in $SYMLINK/fonts
fi



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

#
# Use eventual existing bootptab if there.
# If none present use the one from tape 
#
if [ "$OS" != "sco" ]
then
  if [ ! -f $SYMLINK/bootp/bootptab ]
  then
    cp $SYMLINK/bootp/bootptab.new $SYMLINK/bootp/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
    ypcat services | egrep '^bootpc.*68/udp' > /dev/null
    if [ $? -eq 0 ]
    then
      NISSMAP="ok"
    fi
  fi
else
  cat /etc/services | egrep '^bootps.*67/udp' > /dev/null
  if [ $? -eq 0 ]
  then
    cat /etc/services | egrep '^bootpc.*68/udp' > /dev/null
    if [ $? -eq 0 ]
    then
      NISSMAP="ok"
    fi
  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 Netstation" >>/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 Netstation " >>/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 Netstation" >>/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 Netstation" >>/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 $SYMLINK/bootp/bootpd bootpd $SYMLINK/bootp/bootptab"
fi

BOOTPD=`grep '^bootps' /etc/inetd.conf   | awk '{print $6}' | egrep -v '(700X)|(enware2)'`
BOOTPTAB=`grep '^bootps' /etc/inetd.conf | awk '{print $NF}' \
	  | egrep -v '(700X)|(enware2)' | 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 Netstation" >>/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}' | egrep '(700X)|(enware2)'`
      if [ -f "$OBPTAB" ]
      then
        if [ "$OBPTAB" != "$SYMLINK/bootp/bootptab" ]
        then
          echo copying existing bootptab to $SYMLINK/bootp/bootptab
          cp $OBPTAB $SYMLINK/bootp/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 Netstation" >>/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

##############################################################
# Record for administration scripts where bootp/bootptab
# are in case we want to use some already existing versions
###############################################################
#
if [ $BOOTPTAB ]
then
  echo xtadm terminal administration script will use existing $BOOTPTAB
  echo "BOOTPTABLE=$BOOTPTAB" >> $ADMINDIR/env.vars
  echo "BOOTPPARAM=$BOOTPTAB" >> $ADMINDIR/env.vars
fi
						  
if [ $BOOTPD ]
then
  echo "BOOTPD=$BOOTPD" >> $ADMINDIR/env.vars
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.
######################################################

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

echo "INSTALLDIR=${TOPDIR}" >> $ADMINDIR/env.vars

if [ $WANTTFTP = "false" ]
then
  echo "FILEACCESS=nfs" >> $ADMINDIR/env.vars
else
  echo "FILEACCESS=tftp" >> $ADMINDIR/env.vars
fi



#---------------------------------------------------------------------#
# Add 8_2 information to sample.cfg
# 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 [ $OS != "sco" ]
then
  ###########################################################################
  # Add font directory /usr/lib/X11/700X/fonts/Xol
  # to remote config file. 
  ###########################################################################

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

      if [ -s $TMPCFG ]
      then
        cp $TMPCFG $CFGFILE
      fi
      rm -f $TMPCFG
    fi
  done
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 directives with extensions from this fileset
#----------------------------------------------------------------------#
#

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

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


#--------------------------------------------------------
# update directives with info from filesets in this packages
#--------------------------------------------------------
#
PARTS="drivers floppy nfs utils audio xtouch vidhw gated console lpd"
for PART in $PARTS
do
  THISDIRECTIVE=$NEWDIR/bin/directives.$PART
  cat $NEWDIR/bin/directives | egrep -i directive | egrep -i "$PART" > /dev/null
  if [ $? -ne 0 ]
  then
    if [ -f $THISDIRECTIVE ]
    then
      cat $THISDIRECTIVE >> $NEWDIR/bin/directives
    else
      echo "WARNING: could not find $THISDIRECTIVE"
    fi
  fi
  rm  -f $THISDIRECTIVE
done



#################################################
#
# Process Printer package
#
#################################################

HPNPDIR=${SYMLINK}/hpnp

if [ "$OS" = "sun5" ]
then
  MODELDIR=/usr/spool/lp/model

  NUMBER=`ls $HPNPDIR/sh | wc -w`

  while true 
  do
    echo ""
    #echo $NL1 "Copy model scripts into $MODELDIR? $NL2"
    #REPLY=`line`
    REPLY="yes"
    case $REPLY in
      y*|Y*) break ;;
    n*|N*)
      echo ""
      echo "Installation of this package cannot continue."
      echo ""
      exit 1
      ;;
     *)
       echo
       echo "please answer yes or no"
       continue
      ;;
    esac
  done


  ##########################################################
  # save eventual existing model script versions that differ
  # and then copy new models scripts into $MODELDIR
  ##########################################################

  echo

  HEADERDONE=no
  MODELS=`ls $HPNPDIR/sh`
  for MODEL in $MODELS
  do
    if [ -f $MODELDIR/$MODEL ]
    then
      cmp -s $HPNPDIR/sh/$MODEL $MODELDIR/$MODEL
      if [ $? -eq 1 ]
      then
        if [ "$HEADERDONE" = "no" ]
        then
          HEADERDONE=yes
          echo "Saving existing model file(s) in $MODELDIR"
        fi

        cp -p $MODELDIR/$MODEL $MODELDIR/$MODEL.orig.$$
        echo existing $MODEL saved as $MODEL.orig.$$
      fi
    fi
    cp $HPNPDIR/sh/$MODEL $MODELDIR/$MODEL
    chmod 555 $MODELDIR/$MODEL
    chgrp lp $MODELDIR/$MODEL
    chown lp $MODELDIR/$MODEL
  done
fi

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


#--------------------------------------------------
# If CDE is installed create CDE help links to
# the ENWARE online help volume
#--------------------------------------------------
#

if [ -d /usr/dt ]
then
##
## Add floppy lines to Xstartup and Xreset
##

DTDIRS="/etc/dt/config"
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 [ ! -d /etc/dt/config ]
  then
    mkdir -p /etc/dt/config
  fi
  if [ ! -f "$DTETCSTART" ]
  then
    cp "$DTUSRSTART" "$DTETCSTART"
  fi
  if [ ! -f "$DTETCRESET" ]
  then
    cp "$DTUSRRESET" "$DTETCRESET"
  fi
fi


for DTDIR in $DTDIRS
do
  XSTARTUP=$DTDIR/Xstartup
  XRESET=$DTDIR/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 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
fi


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