#!/bin/sh


echo

set -a

umask 022

PRODUCT="enware2"

###################################
#  What is the Architecture?
###################################

if [ -x /bin/sun ]
then
  if /bin/sun
  then
    OS=sun4
    if [ `uname -r | awk -F'.' '{print $1}'` = "5" ]
    then
      OS=sun5
    fi
  fi
else
  OS=`uname -m`
  if [ $OS = "i386" -o $OS = "i486" ]
  then
    OS=sco
  else
    OS=`uname`
  fi
fi


if [ -d /usr/tmp ]
then
  TMP="/usr/tmp"
elif
  [ -d /tmp ]
  then
    TMP="/tmp" ]
else
  echo "No /tmp or /usr/tmp working directories, - cannot continue."
  exit 1
fi

case $OS in
  sun5)
    INITTAB="/etc/init.d"
    HOSTNAME=`uname -n`
    PSCMD="ps -e"
    PIDFIELD="\$2"
    PPIDFIELD="\$3"
    DFCMD="df -k"
    DIVISOR=1
    MOUNTTABLE=/etc/vfstab
    TFTPD="/usr/sbin/in.tftpd"
    SYMLINK=/opt/hpxt/enware2
    DEFDIR=/opt/hpxt
    EXPORTS=/etc/dfs/dfstab
    EXPORTFS=share
    NL1=""
    NL2='\c'
    NL3='\\\\c'
    INETDSTART="inetd -s"
    ;;
  sun4)
    INITTAB="/etc/rc.local"
    HOSTNAME=`hostname`
    PSCMD="ps -axjc"
    PIDFIELD="\$2"
    PPIDFIELD="\$1"
    DFCMD="df"
    DIVISOR=1
    MOUNTTABLE=/etc/fstab
    TFTPD="/usr/etc/in.tftpd"
    SYMLINK=/opt/hpxt/enware2
    DEFDIR=/usr/hpxt
    EXPORTS=/etc/exports
    EXPORTFS=exportfs
    NL1="-n"
    NL2=""
    NL3=""
    INETDSTART="inetd"
    ;;
  *) echo unknown OS=$OS
     exit 1
    ;;
esac

BUDIR="$TMP/$PRODUCT.save"
mkdir $BUDIR > /dev/null 2>&1

    
BASEEXPORTS=`basename $EXPORTS`
PAR=`basename $TFTPD`


#################################################
#
# Check for an existing installation
#
#################################################
XDMDIR="$SYMLINK/xdm"
CFGDIR="$SYMLINK/config"
BOOTPDIR="$SYMLINK/bootp"
 
if [ -d $BOOTPDIR -o -d $CFGDIR -o -d $XDMDIR ]
then
  echo
  echo "         U P G R A D E    I N F O R M A T I O N  "
  echo
  echo "  This seems to be a SW upgrade installation."
  echo "  Files most likely to have been customized are being saved:"
  echo "  in directory $BUDIR:"
  if [ -d $CFGDIR ]
  then
    mkdir $BUDIR/config > /dev/null 2>&1
    cp $CFGDIR/*  $BUDIR/config
    echo $NL1 "       .. remote config files$NL2"
  fi

  if [ -d $XDMDIR ]
  then
    cp -r $XDMDIR $BUDIR
    echo $NL1 ".. xdm config files $NL2"
  fi
  BOOTPTAB=`grep '^bootps' /etc/inetd.conf | awk '{print $NF}' \
    | grep -v enware2  | grep '/'`
  if [ -f "$BOOTPTAB" ]
  then
   cp $BOOTPTAB $BUDIR/bootptab.from_inetd.conf
   echo $NL1 ".. $BOOTPTAB file $NL2"
  fi
  if [ -f $BOOTPDIR/bootptab ]
  then
    cp $BOOTPDIR/bootptab $BUDIR/bootptab.hpxt
    echo $NL1 ".. bootptab.hpxt file $NL2"
  fi

  echo
  echo $NL1 "Enter <return> to continue: $NL2"
  read REPLY
fi



########################################################
#
# Explain big picture as to how netstation may boot
#
########################################################

cat <<EOF

         G E N E R A L   I N F O R M A T I O N         

  The main functionality of the HP Netstation Software 
  is to make it possible to boot and provide files for the HP Netstation.

  The boot method of an HP Netstation is usually one of:
  (i) host bootpd supplies boot parameters and netstation boots using NFS.
  (ii) host bootpd supplies boot parameters and netstation boots using tftp. 
  On a particular host the HP Netstation may switch between (i) and (ii).
  Booting using NFS is the **recommended** boot method.

  .. examining your system, - will take a moment

EOF

#################################################
#
# Check if machine seems to be nfs capable 
#
#################################################

#
# nfsd running?
#
$PSCMD | awk '$NF == "nfsd" {print $NF}' | grep nfs > /dev/null 2>&1
if [ $? -ne 0 ]
then
  echo "-$HOSTNAME does not seem to be set up for NFS (no nfsd process running)" 
  DOESNFS="false" 
else
  echo "-$HOSTNAME seems to be set up for NFS (nfsd process running)" 
  DOESNFS="true" 
fi



TFTPSECURE=true
TFTPTHERE=true

#################################################
#
# Examine system
# GET tftp line in /etc/inetd.conf
#
#################################################

# See if tftp is mentioned at all.
#

egrep '^ *tftp' /etc/inetd.conf  > /dev/null 2>&1
if [ $? -ne 0 ]
then
  echo "-$HOSTNAME not set up for tftp (no active tftp line in /etc/inetd.conf)" 
  echo "" 
  TFTPTHERE=false
  if [ $DOESNFS = "false" ]
  then
    echo "-$HOSTNAME not set up for NFS (nfsd not runing or no local file systems), " 
  fi	
else
  egrep '^ *tftp' /etc/inetd.conf | awk '{print $(NF-1) $NF}' | egrep '\-[rs]'\
    			> /dev/null
  if [ $? -eq 0 ]
  then
    echo $NL1 "-tftp seems to be running SECURE, $NL2" 
  else
    echo $NL1 "-tftp seems to be running UNSECURE, $NL2" 
    TFTPTSECURE=false
  fi
fi

MBNEEDED=104

#################################################
#
# Examine system
# GET TFTPDIR ROOT
#
#################################################

# Discover if tftp is using a restricted path
# -- look in /etc/inetd.conf (last argument to tftpd)
# -- if the path is found then TFTPDIR = path
#

TFTPDIR="none"
	
if [ $TFTPTHERE = "true" ]
then
  TFTPDIR=`awk '$1 == "tftp" {print $NF}' < /etc/inetd.conf \
	| head -1 | sed -e 's/-[rs]//'`

  echo $TFTPDIR | grep "^/" > /dev/null
  if [ $? -ne 0 -o $TFTPDIR = "/" ]
  then
      echo " and has no \"home directory\"." 
      TFTPSECURE=false
  else 
    CHECKSPACE="true"
    if [ ! -d $TFTPDIR ]
    then
      echo ""
      echo "Directory $TFTPDIR from tftp line in /etc/inetd.conf does not exist"
      echo $NL1 "Do you want script to create it?  [y|n]  (default=y): $NL2"
      REPLY=`line`
      case $REPLY in
       y|Y|yes|YES|y*|"")
	 mkdir -p $TFTPDIR > /dev/null 2>&1
         if [ $? -ne 0 ]
         then
	   if [ -h $TFTPDIR ]
	   then
	     mkdir -p `ls -l $TFTPDIR | awk '{print $NF}'`
             if [ $? -ne 0 ]
             then
               echo "mkdir -p `ls -l $TFTPDIR | awk '{print $NF}'` failed"
               CHECKSPACE="false"
               TFTPTHERE=false
             else
               echo "mkdir -p `ls -l $TFTPDIR | awk '{print $NF}'` succeded"
             fi
           else
             echo "mkdir -p $TFTPDIR failed"
             CHECKSPACE="false"
             TFTPTHERE=false
           fi
         else
           echo "mkdir -p $TFTPDIR succeeded"
           echo "" 
	 fi
	 ;;
       *)
	 echo " .. ignoring INCONSISTENCY in /etc/inetd.conf.."
         CHECKSPACE="false"
         TFTPTHERE=false
	 ;;
      esac
    fi
    
      echo "tftp has $TFTPDIR as \"home directory\"." 
       
    if [ "$CHECKSPACE" = "true" ]
    then
       #
       # check if this host seems to have enough disk space for SW
       #

       if [ $OS = "sco" ]
       then
         KBTHERE=`$DFCMD $TFTPDIR | tail -1 | awk '{print $(NF-1)}'`
       else
         KBTHERE=`$DFCMD $TFTPDIR | tail -1 | awk '{print $4}'`
       fi

       KBTHERE=`expr $KBTHERE / $DIVISOR`
       SPACELEFT=`expr $KBTHERE / 1000 - $MBNEEDED`

       if [ $SPACELEFT -lt 0 ]
       then
         ENOUGH="false"
         echo "You seem to be short of disk space in $TFTPDIR for this SW"

         #
         # check if this host seems to be a boot server for diskless nodes
         # or X terminals, eg something in $TFTPDIR already?
         #
         ls $TFTPDIR | grep . > /dev/null
         if [ $? -eq 0 ]
         then
           BOOTHOST="true"
           echo "and diskless clients and/or X terminals seem to boot from $TFTPDIR."
           echo ""
         fi

         echo "If you choose to install SW for secure tftp this script will"
         echo "help you move $TFTPDIR and contents to a different file system."
         echo ""
       else
         echo "-You seem to have sufficient disk space in $TFTPDIR for this SW"
       fi
    fi
  fi
fi

echo

while true 
do
  echo $NL1 "display information on how to set-up NFS and/or tftp?  [y|n]  (n): $NL2"
  REPLY=`line`
  case $REPLY in
    y*|Y*)
cat <<EOF

 -o- Setting up **NFS**, if not already done, is usually a matter of
 editing the file $EXPORTS and re-booting the host.
 If you do not want to re-boot the command "$EXPORTFS" can be used.
 "man $BASEEXPORTS" and "man $EXPORTFS" will provide more detail.

 -o- Setting up **tftp**, if not already done, is usually a matter of
 entering a line similar to the one below into /etc/inetd.conf: 
tftp dgram udp wait root $TFTPD $PAR -s /tftpboot # SECURE tftp
 or a line similar to:
tftp dgram udp wait root $TFTPD $PAR             # UNSECURE tftp
 For changes to /etc/inetd.conf to take effect inetd must be restarted. 
 A "$PSCMD | fgrep inetd; kill pid_of_inetd; $INETDSTART"  may accomplish this.

 -o- note on secure tftp
 If you want the netstation to boot using secure tftp the whole tape tree 
 (approx. ${MBNEEDED}mb) must be installed relative to (below) the /tftpboot 
 directory.  This may be a problem if file system of /tftpboot does not have
 enough disk space, and in this case this script will help you move 
 /tftpboot to a file system with more space. An alternative is to have the 
 netstation boot using NFS, or quit now and change system to run UNSECURE tftp. 
 In both cases the tree may be installed "anywhere"

EOF
      echo $NL1 "please enter <Return> to continue q to quit: $NL2"
      REPLY=`line`
      case $REPLY in
	q|Q|quit|x|exit)
	  exit 1
	  ;;
      esac
      break
      ;;
    n*|N*|"")
      break
      ;;
    *)
      echo
      echo please answer yes or no 
      echo
      ;;
  esac
done


if [ $DOESNFS = "false" -o $TFTPTHERE = "false" -o $TFTPSECURE = "true" ]
then
 while true 
 do
  echo "" 
  if [ $DOESNFS = "false" -a $TFTPTHERE = "false" ]
  then
    echo
    echo "INFO: neither NFS nor tftp enabled on this host. This script"
    echo "      will enable NFS if you continue with current configuration."
  fi
  echo "If you want to change your system configuration, please do so now." 
  echo "Once you are done setting up NFS or tftp, this script should be"
  echo "re-run to pick up any changes."
  echo $NL1 "Enter q to quit, anything else to continue with current configuration: $NL2"
  REPLY=`line`

  case $REPLY in
    q*|Q*)
    exit 1
     ;;
    *)
      break
      ;;
  esac
 done
fi


sleep 1
echo
sleep 1


############2###########################################
#
# Explain big picture as to how things are installed 
#
########################################################

case $OS in
  sun*)
    DMANDIR=/usr/share/man
    ;;
  sco)
    DMANDIR=/usr/man/cat.LOCAL
    ;;
esac

cat <<EOF 


         INSTALLATION DIRECTORY & MAN PAGES

With your permission, some man pages will be installed into $DMANDIR.
The software itself will be installed into one user selectable directory, 
except for 1 symbolic link: $SYMLINK
This link points into the user selectable SW installation directory. 

EOF

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



while true
do
  echo $NL1 "OK to copy netstation man pages into $DMANDIR ? [y|n] (y): $NL2"
  REPLY=`line`
  case $REPLY in
    y*|Y*|"")
      COPYMAN="yes"
      break
      ;;
    n*|N*)
      COPYMAN="no"
      break
      ;;
  esac
done



#################################################
#
# If installation seems to be tftp secure 
# inform installer of the consequences.
# Ask installer if he wants to boot using NFS instead 
#
#################################################

WANTTFTP="false"

BOOTHOST="false"
ENOUGH="true"


if [ $TFTPTHERE = "true" -a $TFTPSECURE = "true" ]
then
cat <<EOF 


         NETSTATION BOOT METHODS

Booting using NFS is faster and is the recommended boot method.
Your system seems to run SECURE tftp. If you want the netstation to boot using 
secure tftp the whole tape tree (approx. ${MBNEEDED} MB) must be installed 
relative to (below) the $TFTPDIR directory. 
An alternative is to have the netstation boot using NFS, or quit now and change 
system to run UNSECURE tftp. In both cases the tree may be installed "anywhere"

EOF


  #
  # check if this host seems to have enough disk space for SW
  #

  if [ $OS = "sco" ]
  then
    KBTHERE=`$DFCMD $TFTPDIR | tail -1 | awk '{print $(NF-1)}'`
  else
    KBTHERE=`$DFCMD $TFTPDIR | tail -1 | awk '{print $(NF-2)}'`
  fi

  KBTHERE=`expr $KBTHERE / $DIVISOR`
  SPACELEFT=`expr $KBTHERE / 1000 - $MBNEEDED`

  if [ $SPACELEFT -lt 0 ]
  then
    echo "You seem to be short of disk space in $TFTPDIR for this SW"

    #
    # check if this host seems to be a boot server for diskless nodes
    # or X terminals, eg something in $TFTPDIR already?
    #
    ls $TFTPDIR | grep . > /dev/null
    if [ $? -eq 0 ]
    then
     BOOTHOST="true"
     echo "and diskless clients and/or X terminals seem to boot from $TFTPDIR."
     echo "If you choose to install SW for secure tftp this script will"
     echo "help you move $TFTPDIR and contents to a different file system."
     echo ""
    fi
  else
    echo "You seem to have sufficient disk space in $TFTPDIR for this SW"
  fi

  while true 
  do
    echo ""
    echo $NL1 "Please enter boot method [nfs|tftp|q]  (default=nfs): "$NL2 
   REPLY=`line`
   case $REPLY in
    tftp|TFTP)
      WANTTFTP="true"
      break
      ;;
    nfs|NFS|"")
      break
      ;;
    q|qq|qqq|Q|QQ|quit|QUIT)
      exit 1
      ;;
   esac
  done
fi


#################################################
#
# CHECK if tftpd present
#
#################################################

if [ $WANTTFTP = "true" ]
then
  if [ ! -f $TFTPD ]
  then
    echo
    echo "$i program not found." 
    echo "$i is necessary for the HP netstation to boot using tftp." 
    case $OS in
      sun5)
        echo "One reason could be that the Solaris \"Networking\" package" 
        echo "has not been installed on this system. If that is the case " 
        echo "the manual \"Solaris Installation Guide\" may be of help." 
	;;
      sco)
	echo "Perhaps the \"SCO TCP/IP\" package" 
	echo "has not been installed on this system."
	;;
      sun4)
        echo "One reason could be that the SunOS \"Networking\" option" 
        echo "has not been installed on this system. If that is the case " 
        echo "the manual \"Installing SunOS\" may be of help." 
	;;
    esac
    exit 1
  fi
fi



#################################################
#
# The lines below are executed when one or more 
# of following is true:
# 1) installation is tftp unsecure 
# 2) installation is tftp secure 
#    but installer chose to boot using NFS
# 3) installation doesn't have tftp 
#
#################################################


if [ $WANTTFTP = "false" ]
then
while true
do
   TFTPDIR=$DEFDIR
   echo
   echo

   if [ $OS = "sco" ]
   then
     df -v | head -15
   fi

   if [ $OS = "sun4" ]
   then
     df -t 4.2 | head -15
   fi

   if [ $OS = "sun5" ]
   then
     df -F ufs -k | head -15
   fi

   echo "" 
   echo "$MBNEEDED MB of disk space is needed to install the software." 
   echo "The default installation directory is $TFTPDIR." 
   echo $NL1 "Enter name of installation directory or <return> to use default: $NL2" 
   REPLY=`line`
   if [ $REPLY ]
   then
     echo $REPLY | egrep '^/' > /dev/null
     if [ $? -ne 0 ]
     then
       echo "" 
       echo "Please specify full path name of installation directory." 
       continue
     fi
   fi
   
   case $REPLY in
    /)
      TFTPDIR="/"
      ;;
    /*)
      # get rid of spaces/tabs/slash_at_end
      REPLY=`echo $REPLY | sed 's/ //g'`
      REPLY=`echo $REPLY | sed 's/       //g'`
      REPLY=`echo $REPLY | sed 's|/$||'`

      case $REPLY in
        /opt/hpxt/enware2)
          TFTPDIR=/opt/hpxt
          ;;
        /opt/hpxt/enware2/*)
          TFTPDIR=`echo ${REPLY} | sed 's/enware2/enware2.real/'`
          ;;
        *)
          TFTPDIR=${REPLY}
          ;;
      esac
   esac

   if [ ! -d $TFTPDIR ]
   then
     mkdir -p $TFTPDIR
   fi

   #
   # check if this host seems to have enough disk space for SW
   #
   if [ $OS = "sco" ]
   then
     KBTHERE=`$DFCMD $TFTPDIR | tail -1 | awk '{print $(NF-1)}'`
   else
     KBTHERE=`$DFCMD $TFTPDIR | tail -1 | awk '{print $4}'`
   fi

   KBTHERE=`expr $KBTHERE / $DIVISOR`
   SPACELEFT=`expr $KBTHERE / 1000 - $MBNEEDED`

   if [ $SPACELEFT -lt 0 ]
   then
     ENOUGH="false"
     echo ""
     echo "You seem to be short of disk space in $TFTPDIR for this SW"
     echo "cannot use that directory, need ${MBNEEDED}mb."
     echo ""
     continue
   else
    break
   fi
done
fi


if [ ! -d $TFTPDIR ]
then
  mkdir -p $TFTPDIR
  if [ $? -ne 0 ]
  then
    echo "" 
    echo "Couldn't create directory $TFTPDIR" 
    echo "Need to have root permissions on file system for duration" 
    echo "of this installation. If $TFTPDIR is NFS mounted "
    echo "you may need to update $EXPORTS on the NFS file server." 
    echo "Please change permissons and re-run." 
    echo "" 
    exit 1
  fi
fi


###############################################
# Check to see if file system with $TFTPDIR seems to have enough free 
# disk space
# If that's not the case EXIT with messages.
###############################################
        # get name of file system of $TFTPDIR
	#
case $OS in
  sco)
    FSDEV=`$DFCMD $TFTPDIR | tail -1 | awk '{print $2}`
    TFTPFS=`mount | grep $FSDEV | awk '{print $1}'`
  ;;
  sun*)
    TFTPFS=` $DFCMD $TFTPDIR | tail -1 | awk '{print $NF}'`
  ;;
esac

SHORT="false"
TOTALNEED=${MBNEEDED}000
if [ -x ${TFTPDIR}/${PRODUCT} ]
then
  OLDSPACE=`du -s ${TFTPDIR}/${PRODUCT} | awk '{print $1}'`
  OLDSPACE=`expr $OLDSPACE / $DIVISOR`
  NEED=`expr $TOTALNEED - $OLDSPACE`
else
  NEED=$TOTALNEED
fi

case $OS in
  sco)
    FREE=`$DFCMD $TFTPDIR | tail -1 | awk '{print $(NF-1)}'`
    ;;
  sun*)
    FREE=`$DFCMD $TFTPDIR | tail -1 | awk '{print $(NF-2)}'`
  ;;
esac

FREE=`expr $FREE / $DIVISOR`
if [ $FREE ]
then
 :
else
  echo "FATAL ERROR: Free space calculation failed for $TFTPDIR." 
  exit 10
fi


if test $FREE -lt $NEED
then
   SHORT="true"
   echo "" 
   echo "" 
   echo "----------NOT ENOUGH DISK SPACE IN $TFTPDIR ----------   " 	
   echo "******************************************************" 	
   case $OS in
     sco)
       FSTOTEST=`mount | awk '{print $1}'`
       ;;
     sun4)
       FSTOTEST=`cat $MOUNTTABLE | awk '{print $2}' | egrep '^/'`
       ;;
     sun5)
        FSTOTEST=`cat $MOUNTTABLE | egrep '^[^#]' | awk '$1 != "swap" {print $3}' | egrep '^/'`
       ;;
   esac

   echo "" 
   FOUNDONE="false"
   echo "Need $NEED kb of disk space and only $FREE kb free in $TFTPFS" 
   for i in $FSTOTEST
   do
     if [ -d "$i" ]
     then
       case $OS in
         sco)
           NEWFSFREE=`$DFCMD $i | tail -1 | awk '{print $(NF-1)}'`
           ;;
         sun*)
           NEWFSFREE=`$DFCMD $i | tail -1 | awk '{print $(NF-2)}'`
           ;;
       esac

       if [ "$NEWFSFREE" = "" ]
       then
	 continue
       fi

       echo $NEWFSFREE | egrep '[a-z][A-Z]' > /dev/null 2>&1
       if [ $? -eq 0 ]
       then
	 continue
       fi

       echo $NEWFSFREE | egrep -s '[0-9]'   > /dev/null 2>&1
       if [ $? -ne 0 ]
       then
	 continue
       fi

       NEWFSFREE=`expr $NEWFSFREE / $DIVISOR`
       if test $NEWFSFREE -gt $NEED
       then
 
         #
         #  Lets see if we have root priviledges on file system
         #  before we suggest it as an alternative
         #
         touch $i/tmp.$$ > /dev/null 2>&1
         if [ $? -ne 0 ]
         then
           continue
         fi

         chown root $i/tmp.$$ > /dev/null 2>&1
         if [ $? -ne 0 ]
         then
           rm $i/tmp.$$ > /dev/null 2>&1
	   continue
         fi

         rm $i/tmp.$$    > /dev/null 2>&1

         echo "File system **$i** has **enough** space ($NEWFSFREE kb free) " 
	 FOUNDONE="true"
       fi
     fi
   done
   if [ $FOUNDONE = "false" ]
   then
     echo "" 
     exit 1
   fi 
   if [ $WANTTFTP = "false" ]
   then
     echo "" 
     echo "Please re-run script with a different installation directory." 
     exit 1
   fi 
fi


if [ $WANTTFTP = "true" -a $SHORT = "true" ]
then
   NEWFS="/NEW_FS"
   echo "" 	
   echo "$TFTPDIR currently is in file system $TFTPFS" 	
   echo "(all HP netstation files will be placed under $TFTPDIR and must be" 	
   echo " in the SAME file system or tftp will not work correctly)" 	
   echo "" 	
   echo "  Following commands will move $TFTPDIR and contents - if any - "
   echo "  to another file system (NEW_FS):" 
   ls $TFTPDIR | grep . > /dev/null
   if [ $? -eq 0 ]
   then
     echo "  (existing tftp boot services will continue to work)" 
   fi
   echo "" 	
   echo "  % mkdir -p $NEWFS$TFTPDIR         	# make new $TFTPDIR" 
   echo "  % cd $TFTPDIR                     	# go to old $TFTPDIR"  
   echo "  % find . -depth -print | cpio -pduvm $NEWFS$TFTPDIR # copy old to new" 
   echo "  % mv $TFTPDIR $TFTPDIR.old.$$       	# rename old $TFTPDIR "  
   echo "  % ln -s $NEWFS$TFTPDIR $TFTPDIR   	# link $TFTPDIR to new home." 	
   echo"" 
   echo "If you want this script to execute above commands then " 
  while true
  do
   NEWFS="/NEW_FS"
   echo $NL1 "please enter the name of $NEWFS (q to quit): $NL2" 
   REPLY=`line`
   case $REPLY in
     /*)
       NEWFSFREE=`$DFCMD $REPLY | tail -1 | awk '{print $(NF-2)}'`
       if test $NEED -ge $NEWFSFREE
       then
         echo "Not enough space free in $REPLY ($NEWFSFREE kb free)" 
	 echo "Try another destination or quit and free some space." 
	 continue
       fi 

       touch $REPLY/tmp.$$ > /dev/null 2>&1
       if [ $? -ne 0 ]
       then
         echo "root on this machine doesn't seem to have write permission on $REPLY" 
	 echo "Please specify another file system" 
	 continue
       fi

       chown root $REPLY/tmp.$$ > /dev/null 2>&1
       if [ $? -ne 0 ]
       then
         echo "root on this machine doesn't seem to have root privileges on $REPLY" 
	 echo "Please specify another file system" 
	 rm $REPLY/tmp.$$ 
	 continue
      fi

      rm $REPLY/tmp.$$ 

      NEWFS=$REPLY
	   
      if [ -d $NEWFS$TFTPDIR ]
      then
        echo "$NEWFS$TFTPDIR already exists, please rename it" 
        echo "and rerun this script or try a different name. " 
        continue
      fi
	   
      mkdir -p $NEWFS$TFTPDIR       
      if [ $? -ne 0 ]
      then
        echo "above mkdir command failed, ... quitting" 
        exit 50
      else 
        echo \"mkdir -p $NEWFS$TFTPDIR\" succeeded       
      fi

      cd $TFTPDIR               

      echo "Doing the find | cpio  ... " 
      find . -depth -print | cpio -pduvm $NEWFS$TFTPDIR 
      if [ $? -ne 0 ]
      then
        echo "above find | cpio FAILED ... quitting" 
        exit 60
      else
        echo "\"find | cpio\"" succeeded 
      fi

      mv $TFTPDIR $TFTPDIR.old.$$ 
      if [ $? -ne 0 ]
      then
        echo "above mv command failed, ... quitting" 
        exit 70
      else 
        echo \"mv $TFTPDIR $TFTPDIR.old.$$\" succeeded 
      fi

      ln -s $NEWFS$TFTPDIR $TFTPDIR 
      if [ $? -ne 0 ]
      then
        echo "above ln command failed, ... quitting" 
        exit 70
      else 
        echo \"ln -s $NEWFS$TFTPDIR $TFTPDIR\" succeeded 
      fi

      echo "" 
      echo $NL1 "Move done ...  $NL2" 
      echo "$TFTPDIR.old.$$ may be removed at your discretion." 
      echo "" 
        echo $NL1 "enter <Return> to continue $NL2"
        REPLY=`line`
        break
        ;;
    q|quit|Q|QUIT)
      exit 1
      ;;	
    *)
      echo "Please specify a file system with a leading /" 
      continue
   ;;	
  esac
done
fi


if [ "$DOESNFS" = "false" -a "$WANTTFTP" = "false" ]
then
  echo "starting up NFS" 
  mkdir -p $TFTPDIR > /dev/null 2>&1
  case $OS in
    sun5) 
      cp /etc/dfs/dfstab /etc/dfs/dfstab.$$
      echo "share -F nfs $TFTPDIR" >> /etc/dfs/dfstab
      $PSCMD | fgrep nfsd > /dev/null 2>&1
      if [ $? -eq 1 ]
      then
        /etc/init.d/nfs.server start
      else
        unshareall
        shareall
      fi 
      ;;
    sun4|sco)  
      cp /etc/exports /etc/exports.$$ > /dev/null 2>&1
      echo "$TFTPDIR" >> /etc/exports
      PID=`$PSCMD | awk ' $NF == "nfsd" ' | grep .`
      if [ "$PID" != "" ]
      then
	exportfs -a
      else
        case $OS in
          sun4) 
            nfsd 8
            rpc.mountd
            exportfs -a
	    ;;
          sco) 
	    if [ -x /etc/nfs ]
	    then
	      echo "Starting NFS"
	      /etc/nfs start
	    else
	      echo
	      echo "Please enable this system as an NFS Server after "
	      echo "the installation. The \"sysadmsh\" utility may be used for this "
	      echo
	    fi
	    ;;
        esac
      fi 
      ;;
  esac
fi


set +u
if [ "$OPENWINHOME" = "" ]
then
  if [ -d /usr/openwin -o -h /usr/openwin ]
  then
    OPENWINHOME=/usr/openwin
  fi
fi
set -u


#
# Support for new ASX fonts on SunOS 4.1.x
# We test for presence of this font and if there
# it is taken as "proof" that this is a JLE environment
# (as recommended by Seiju) 
#

if [ $OS = "sun4" -o $OS = "sun5" ]
then
    if [ $OS = "sun4" ]
    then
      TESTFONT="$OPENWINHOME/lib/fonts/gotm14"
    else
      TESTFONT="$OPENWINHOME/lib/locale/ja/X11/fonts/75dpi/gotm14"
    fi
    ls ${TESTFONT}* 2> /dev/null | egrep 'gotm14' > /dev/null
    if [ $? -eq 0 ]
    then
      HPJAPANESE="yes"
    else
      HPJAPANESE="no"
    fi
fi

if [ -f /tmp/hpjapanese ]
then
  cat /tmp/hpjapanese | egrep -i '(no)|(false)' > /dev/null
  if [ $? -eq 0 ]
  then
    HPJAPANESE="no"
  else
    HPJAPANESE="yes"
  fi
fi


#

echo 'set -a'			>  $1
echo WANTTFTP=$WANTTFTP		>> $1

if [ $WANTTFTP = "true" -a $TFTPSECURE = "true" ]
then
  mkdir -p $TFTPDIR/opt/hpxt > /dev/null 2>&1
  echo TOPDIR=$TFTPDIR/opt/hpxt	>> $1
else
  echo TOPDIR=$TFTPDIR		>> $1
fi

echo TFTPDIR=$TFTPDIR		>> $1
echo OS=$OS                     >> $1
echo NL1="$NL1"                 >> $1
echo NL2="$NL3"                 >> $1
echo HOSTNAME=$HOSTNAME         >> $1
echo COPYMAN=$COPYMAN         	>> $1
echo PRODUCT=$PRODUCT    	>> $1
echo SYMLINK=$SYMLINK    	>> $1

if [ "$OPENWINHOME" != "" ]
then
  echo OPENWINHOME=$OPENWINHOME >> $1
fi

if [ $OS = "sun4" -o $OS = "sun5" ]
then
  echo HPJAPANESE=$HPJAPANESE  	>> $1
fi

#
#  FOR Solaris 2
# copy env vars to a place so we can  pull them in again 
# from other postinstall/request scripts
#

if [ $OS = "sun5" ]
then
  ENVVARS=/usr/tmp/env.vars
  rm -f $ENVVARS
  cp $1 $ENVVARS
  chmod 755 $TFTPDIR
  chgrp sys $TFTPDIR
  chown root $TFTPDIR
fi


exit 0
