#!/bin/sh

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

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

ADMINDIR=$TOPDIR/usr/bin/X11/700X/admin
. $ADMINDIR/env.vars


HPNPDIR=${TOPDIR}/usr/lib/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



	#
	#
	# Find files with hardcoded path in them 
	# eg /usr/lib/hpnp and replace it with
	# $HPNPDIR/usr/lib/hpnp
	#

  HPNPFILES=`find $HPNPDIR -exec grep -l '=/usr/lib/hpnp' {} \;`

  for i in $HPNPFILES
  do
   file $i | grep 'executable SPARC' > /dev/null
   if [ $? -ne 0 ]
   then
     sed "s!=/usr/lib/hpnp!=$HPNPDIR!g" $i > /tmp/tmp.$$
     cp /tmp/tmp.$$ $i
     rm /tmp/tmp.$$
   fi
  done


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

  echo

  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 ]
        then
         :
        else
          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 necessary then
# warn about possible collision with "Network Peripheral SW"
##########################################################

STDHPNPDIR=/usr/lib/hpnp
if [ -d $STDHPNPDIR ]
then
  echo "" 
  echo "The HP NPI network spooler software seems be installed already in" 
  echo "$STDHPNPDIR. "
  echo "This package includes a sub-set of this software in "
  echo "$TOPDIR/$STDHPNPDIR"
  echo "The two packages should be able to co-exist without problems."  
  echo "" 
  echo "enter <Return> to continue"
  read REPLY
fi


if [ "$OS" = "sco" ]
then
  sed -e "s-/usr/lib/hpnp-${HPNPDIR}-g" ${HPNPDIR}/hpnp.model > /tmp/tmp.$$
  cp /tmp/tmp.$$                        ${HPNPDIR}/hpnp.model
  rm /tmp/tmp.$$ 
fi


if [ "$OS" = "sun4" ]
then
  ## this is currently done in /usr/bin/X11/700X/admin/xtadm
  ## that stuff will get moved here when time permits
  :
fi

##########################################################
# Install hpnpf/hpnptyd man pages
##########################################################


if [ "$OS" = "sco" ]
then
  SMANDIR=$TOPDIR/usr/share/man/man1
  DMANDIR=/usr/man/cat.LOCAL
  MANPAGES="hpnpf.LOCAL hpnptyd.LOCAL" 
else
  SMANDIR=$TOPDIR/usr/share/man
  DMANDIR=/usr/share/man
  MANPAGES="man1/hpnpf.1 man1/hpnptyd.1" 
fi

if [ "$COPYMAN" = "yes" ]
then
    for i in $MANPAGES
    do
      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 "you can do this copy manually after installation" 
	echo ".. continuing"
        echo ""
        break
      fi
      chmod 444  $DMANDIR/${i}
      chown root $DMANDIR/${i}
      chgrp bin  $DMANDIR/${i}
    done
fi


exit 0
