#!/bin/sh


echo
#echo WARNING: remember to unset -u before release
#set -u

set -a

umask 022

UTILDIR1=/usr/bin/X11/700X
SERVERHOME1=/usr/lib/X11/700X


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

if [ -x /bin/sun ]
then
  if /bin/sun
  then
    OS=sun4
    if [ `uname -r | cut -c1` = "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
  sco)
    INITTAB="/etc/rc.d/7/udaemons"
    HOSTNAME=`uname -n`
    PSCMD="ps -e"
    PIDFIELD="\$2"
    PPIDFIELD="\$3"
    DFCMD="df -v"
    DIVISOR=2
    MOUNTTABLE=/etc/checklist
    TFTPD="/etc/tftpd"
    DEFDIR=/u
    if [ -d /opt ]
    then
      DEFDIR=/opt
    fi
    EXPORTS=/etc/exports
    EXPORTFS=exportfs
    NL1=""
    NL2='\c'
    NL3='\\\\c'
    INETDSTART="inetd"
    ;;
  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"
    DEFDIR=/opt
    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"
    DEFDIR=/usr
    EXPORTS=/etc/exports
    EXPORTFS=exportfs
    NL1="-n"
    NL2=""
    INETDSTART="inetd"
    ;;
  *) echo unknown OS=$OS
     exit 1
    ;;
esac

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



if [ $OS = "sun5" ]
then
  APTREX="HPaptrex"
  pkginfo $APTREX > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
     APTREX_INSTALLDIR="`pkgparam HPaptrex TOP`/Aptrex"
     echo
     echo "This host seems to have AptrexWare installed."
     echo
#    echo cannot co-exist with existing xdm
#    echo  please remove package $APTREX before installing
#    echo
#    exit 3
  fi
else
   #
   #--------------------------------------------------------------------#
   # First check if it's necessary to ask if Aptrex to ENWARE upgrade
   #--------------------------------------------------------------------#
   #
   APTREX_THERE="false"

   if [ -d /usr/Aptrex ]
   then
     APTREX_THERE="true"
   else
     FILES_TO_TEST="/etc/rc.local* /etc/inetd.conf /etc/bootparams"
     for FILE in $FILES_TO_TEST
     do
       if [ -f $FILE ]
       then
         grep -i aptrex $FILE > /dev/null
         if [ $? -eq 0 ]
         then
           APTREX_THERE="true"
          break
        fi
      fi
     done
   fi
   

   if [ $OS = "sun4" -a "$APTREX_THERE" = "true" ]
   then
      echo
      echo $NL1 "Is it software upgrade from Aptrex WARE to ENWARE?[y|n] (default=n) $NL2"
      REPLY=`line`
      case $REPLY in
        y|Y|yes|YES|y*)
           while true
           do
             echo
             echo "Enter path to  Aptrex base directory (default=/opt/Aptrex) $NL2"
             ANS=`line`
             if [ "$ANS" = "" ]
             then
                ANS=/opt/Aptrex
             fi

             if [ ! -d "$ANS" ]
             then 
                 echo "\"$ANS\" is not a valid. $NL2"
                 echo "press \"c\" to continue installation or any other key to try again $NL2"
                 read input 
                 if [ $input = "c" -o $input = "C" ]
                 then
                     break
                 else
                    continue
                 fi 
                 continue
             else 
                 APTREX_INSTALLDIR=$ANS
                 break
             fi
            done
             ;;
        *)   
             break
             ;;
       esac
  fi
fi


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


#################################################
#
# Check for an existing installation
#
#################################################
XDMDIR="/usr/lib/X11/700X/../xdm"
CFGDIR="/usr/lib/X11/700X/config"
BOOTPDIR="/usr/lib/X11/700X/../../../etc/700X"
 
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 is 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 [ $OS != "sco" ]
  then
    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 700X  | 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
  fi

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


  #################################################
  #
  # Check for a running xdm
  #
  #################################################

if [ $OS != "sco" ]
then

  if [ $OS = "sun5" ]
  then
    CMD="ps -ef"
  else
    CMD="ps -ax"
  fi
  
  $CMD | grep -v grep \
	| egrep '((enware)|(Aptrex)|(HPXT)).*/xdm[ 	]' > /dev/null
  if [ $? -eq 0 ]
  then
    sleep 2
    cat <<EOF


         X D M    I N F O R M A T I O N  
        
  You seem to be running an existing version of xdm.
  This SW installation contains an upgraded version of xdm.
  The existing version should be taken down now before you continue.

  (all users logged on through xdm should ***LOG OFF*** first!)

  The new version of xdm will be started up later if you:
    1)  configure terminals using the "xtadm" administration script
     or 
    2) if you select <XDM Admin> from the main menu of xtadm 
       followed by <Start XDM>.
EOF
 
    while true
    do
      echo
      echo $NL1 "Take down xdm now? [y|n] (default=y): $NL2"
      REPLY=`line`
      case $REPLY in
        y|Y|yes|YES|y*|"")
          if [ $OS = "sun5" ]
          then
            if [ -f /etc/init.d/xdm.hp ]
            then
              rm -f $INITTAB/xdm.hp
              rm -f /etc/rc3.d/S172xdm.hp
              rm -f /etc/rc3.d/K172xdm.hp
            fi
          fi
          if [ $OS = "sun4" ]
          then
            sed '/#xtadm/d' $INITTAB > $TMP/inittab
            cmp -s $INITTAB $TMP/inittab
            if [ $? -ne 0 ]
            then 
              if [ -s $TMP/inittab ]
              then 
                cp $INITTAB $BUDIR
                echo
                echo "removing xdm lines from $INITTAB"
                echo ".. existing $INITTAB saved in $BUDIR"
                cp $TMP/inittab $INITTAB
                rm $TMP/inittab
              fi 
            fi 
          fi
 
          echo "{print $PIDFIELD}" > $TMP/awk 
          if [ $OS = "sun5" ]
          then
            P=`ps -ef | egrep '[ 	/]xdm'| fgrep -v fgrep \
		| awk "$PPIDFIELD == 1" | awk -f $TMP/awk`
          else
            P=`$PSCMD | egrep '[ 	/]xdm'| fgrep -v fgrep \
		| awk "$PPIDFIELD == 1" | awk -f $TMP/awk`
          fi

          if [ "$P" != "" ]
          then
            kill $P
            echo "xdm process $P stopped."
          fi

          break
          ;;
        n*|N*|q*|Q*)
          cat <<EOF

       At the end of the installation you will be prompted to run the 
       administration script "xtadm". If you are using the HP version of xdm
       you MUST use this utility to stop and re-start xdm before you can 
       log in from X terminals using xdm. 
       To do this select <XDM Administration> from the main menu of "xtadm"
       and then <Stop XDM> followed by <Start XDM>.

EOF
          break
          ;;
        *)
          continue
          ;;
      esac
    done
    echo
    echo $NL1 "Enter <return> to continue: $NL2"
    read REPLY
    sleep 2
  fi
fi


########################################################
#
# Explain big picture as to how terminal 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 Software you now are installing 
  is to make it possible to boot and provide files for the HP X Station.

  The boot method of an HP X Station is usually one of:
  (i) host bootpd supplies boot parameters and terminal boots using NFS.
  (ii) host bootpd supplies boot parameters and terminal boots using tftp. 
  On a particular host the HP X Station 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=54

#################################################
#
# 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 X Station 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 
 terminal 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 
#
########################################################


ALTLINK="/enware"

if [ -h $ALTLINK ]
then
 rm $ALTLINK
fi

DEFDIR=$DEFDIR${ALTLINK}
cat <<EOF 


         INSTALLATION DIRECTORY & MAN PAGES

The software will be installed in one directory only, except 2 symbolic links: 
$SERVERHOME1 and $UTILDIR1 
The links point into the SW installation tree. 

EOF



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

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


while true
do
  echo $NL1 "OK to copy X Station 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 


         X STATION BOOT METHODS

Booting using NFS is faster and is the recommended boot method.
Your system seems to run SECURE tftp. If you want the X Station 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 terminal 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 X Station 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 "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=$ALTLINK
      ;;
    /*)
      # 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|/$||'`

      if [ "`basename $REPLY`" != "`basename ${ALTLINK}`" ]
      then
        TFTPDIR=${REPLY}${ALTLINK}
      else
        TFTPDIR=${REPLY}
      fi
      ;;
   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/usr/lib/X11/700X ]
then
  OLDSPACE=`du -s $TFTPDIR/usr/lib/X11/700X | 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 X Station 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


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


#
# Get rid of eventual old link(s) that supported versioning
# (these were there to support **later** install of individual 
# filesets/packages # that had not been installed along with HPXT-BASE
# during the initial installation
#
# The existence of the file /usr/lib/X11/700X/Multi_version or similar
# flags that installer wants versioning
# 

DIR700X=/usr/lib/X11/700X
if [ -d "$DIR700X" ]
then
  ODIR=`ls -la $DIR700X | awk '{print $NF}'`
else
  ODIR=none
fi

VERSIONING="no"
rm -f /usr/lib/X11/700X/bin.hpxt.*
rm -f $TFTPDIR/usr/lib/X11/700X/bin.hpxt.*

ls /usr/lib/X11/700X/* 2>&1 | egrep -i 'multi.*version' > /dev/null
if [ $? -ne 0 ]
then
  #----------------------------------------------------------------#
  # Versioning not required, just link the versioned parts of
  # the tape images to the non-versioned
  #----------------------------------------------------------------#
  #
  if [ ! -d $TFTPDIR/usr/lib/X11/700X ]
  then
    mkdir -p $TFTPDIR/usr/lib/X11/700X
  fi
  rm -rf $TFTPDIR/usr/lib/X11/700X.$HPXTVERSION
  ln -s $TFTPDIR/usr/lib/X11/700X $TFTPDIR/usr/lib/X11/700X.$HPXTVERSION
else
  if [ -d $TFTPDIR/usr/lib/X11/700X.$HPXTVERSION -o -h $TFTPDIR/usr/lib/X11/700X.$HPXTVERSION ]
  then
    echo
    echo "WARNING: $TFTPDIR/usr/lib/X11/700X.$HPXTVERSION already exists."
    echo "         Seems to be a reinstallation, versioning may not succeed."
    echo "         It is suggested that you remove this link or directory"
    echo "         and re-run the installation."
    echo
    echo $NL1 "please enter <Return> to continue q to quit: $NL2"
    REPLY=`line`
    case $REPLY in
      q|Q|quit|x|exit)
        exit 1
        ;;
      *) :
    esac
    rm -f $TFTPDIR/usr/lib/X11/700X.$HPXTVERSION > /dev/null 2>&1
  fi
  VERSIONING="yes"
  if [ ! -d $TFTPDIR/usr/lib/X11/700X/bin ]
  then
  #
  # Existence of old modules.ld is taken as proof that old install there
  #
  if [ -d "$ODIR" -a "$ODIR" != "$TFTPDIR/usr/lib/X11/700X" ]
  then
    if [ -f /usr/lib/X11/700X/bin/modules.ld ]
    then
      echo "Importing existing version of ENWARE for versioning support."
      #
      # Attempt to just move directory. If that fails cpio the stuff.
      #
      mkdir -p $TFTPDIR/usr/lib/X11/700X
      if [ $OS != "sco" ]
      then
        mv /usr/lib/X11/700X/* $TFTPDIR/usr/lib/X11/700X/ 2>&1 \
        | egrep -i 'cross' > /dev/null
      else
       :
      fi
      if [ $? -eq 0 ]
      then
        (
          cd /usr/lib/X11/700X/
          if [ ! -d $TFTPDIR/usr/lib/X11/700X ]
          then
            mkdir -p $TFTPDIR/usr/lib/X11/700X > /dev/null
          fi
          find . -depth -print | cpio -pdu $TFTPDIR/usr/lib/X11/700X
          if [ $? -ne 0 ]
          then
            echo "ERROR:  Import of existing version of ENWARE failed."
            echo "Probably out of disk space, - continuing without versioning."
            echo ""
            rm -rf $TFTPDIR/usr/lib/X11/700X/*
            VERSIONING="no"
          fi
        )
      fi
    fi
  fi
  fi
fi


echo 'set -a'			>  $1
echo WANTTFTP=$WANTTFTP		>> $1
echo TOPDIR=$TFTPDIR 		>> $1
echo TFTPDIR=$TFTPDIR 		>> $1
echo ALTLINK=$ALTLINK 		>> $1
echo OS=$OS                     >> $1
echo NL1="$NL1"                 >> $1
echo NL2="$NL3"                 >> $1
echo HOSTNAME=$HOSTNAME         >> $1
echo COPYMAN=$COPYMAN         	>> $1
echo HPXTVERSION=$HPXTVERSION   >> $1
echo VERSIONING=$VERSIONING    	>> $1

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

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
#

mkdir -p $TFTPDIR/usr/bin/X11/700X/admin > /dev/null 2>&1
cp $1 $TFTPDIR/usr/bin/X11/700X/admin/env.vars


exit 0
