#!/bin/sh
#@(#)HP Netstation Admin Scripts
#
# addxt		Do netstation addition 
#
set -a 

cat <<EOF

	_________________________________________________
		HP NETSTATION ADMINISTRATIVE TASKS
			ADD A NETSTATION

To add a netstation to this computer you will need to obtain the
following information from your local network administrator:

	* The name you want to call the netstation (network hostname).

	* The internet protocol (IP) address of the netstation.

	* The LAN hardware (link level) address of the netstation.
	  (From the boot screen or shipping carton of the netstation)

If the network you are adding the netstation to is connected to other
networks you will also need to know:

	* The subnet mask of the netstation.

	* The internet protocol (IP) address of the default gateway.



EOF

$ECHO "Continue adding a netstation [y|n] (y): $NL"
read SEL

case $SEL in
    y|Y|yes|YES|Yes|"")
	;;
    *)
	exit 100
	;;
esac

cat <<EOF


	_________________________________________________
		HP NETSTATION ADMINISTRATIVE TASKS
			ADD A NETSTATION
EOF

while [ 0 ]	# add a netstation loop
do
# Huge loop, I won't indent ... ends with "done netstation add loop"

case $? in
	1)
	exit 100
	;;
	99)
        exit 99		# fatal error
	;;
esac

while [ 0 ]	# network parameter loop
do
# Huge loop, I won't indent ... ends with "done network parameter loop"

# first check to see what type(s) of X terminal this could be.

if [ -f /usr/lib/X11/700X -o -d /usr/lib/X11/700X ]
then
  if [ -f /opt/hpxt/enware2 -o -d /opt/hpxt/enware2 ]
  then
    cat <<EOF

This system appears to have software installed for both ENVIZEX/ENTRIA
and ENVIZEX II/ENTRIA II netstations. To complete all of the steps in 
adding a netstation it is necessary to know what type it is. If you don't 
know what type of netstation you are adding then you can examine it. The 
name plate on the front of the netstation will normally tell you what type 
of terminal it is. If for some reason you cannot determine what type of 
netstation you are installing you can leave it "unknown". This will work in 
most circumstances.

	1) 700/RX, ENVIZEX, or ENTRIA

	2) ENVIZEX II or ENTRIA II

	u) Unknown

EOF
    $ECHO "Please enter a selection : (u) $NL"
    read SEL
    case $SEL in
      1)
	XTHOME=/usr/lib/X11/700X
	CONFIGDIR=$XTHOME/config
	NFSDIRS="/usr/lib/X11/700X/bin /usr/lib/X11/700X/fonts"
	;;
      2)
	XTHOME=/opt/hpxt/enware2/xthome
	CONFIGDIR=/opt/hpxt/enware2/config
	NFSDIRS="/opt/hpxt/enware2/xthome/bin /opt/hpxt/enware2/xthome/fonts /opt/hpxt/enware2/xthome/config"
	;;
      u|U|"")
	XTHOME=""
	CONFIGDIR=""
	NFSDIRS="/usr/lib/X11/700X/bin /usr/lib/X11/700X/fonts /opt/hpxt/enware2/xthome/bin /opt/hpxt/enware2/xthome/fonts /opt/hpxt/enware2/xthome/config"
	;;
      *)
	$ECHO "Invalid selection"
	continue
	;;
    esac
	
  else
    XTHOME=/usr/lib/X11/700X
    CONFIGDIR=$XTHOME/config
    NFSDIRS="/usr/lib/X11/700X/bin /usr/lib/X11/700X/fonts"
  fi
else
  if [ -f /opt/hpxt/enware2 -o -d /opt/hpxt/enware2 ]
  then
    XTHOME=/opt/hpxt/enware2/xthome
    CONFIGDIR=/opt/hpxt/enware2/config
    NFSDIRS="/opt/hpxt/enware2/xthome/bin /opt/hpxt/enware2/xthome/fonts /opt/hpxt/enware2/xthome/config"
  fi
fi

getxtname add

if [ $? -ne 0 ]
then
	exit 100
fi
echo ""

XTNAME=`cat $TMP/XTNAME`
NAME=`cat $TMP/NAME`
SHORTNAME=$NAME
export NAME
export SHORTNAME
export XTNAME
rm $TMP/XTNAME
rm $TMP/NAME


isxtnameinh $XTNAME
RETCODE=$?

IPADDR=`cat $TMP/IPADDR 2>/dev/null`

case $RETCODE in
    5) 
	echo "Got IP address of $IPADDR for $XTNAME from NIS."
	;;
    6) 
	echo "Got IP address of $IPADDR for $XTNAME from nameserver."
	;;
    7) 
	echo "Got IP address of $IPADDR for $XTNAME from NIS+."
	;;
    0)
	echo ""
	echo "WARNING: Name already in use in $HOSTTABLE (IP-address = $IPADDR)."
	$ECHO "Continue adding this netstation? [y|n] (y): $NL"
 	read SEL
	case $SEL in
	y|Y|YES|yes|"")
		;;
	*)
		exit 100
		;;
	esac
        ;;
  2|4|8)
	getuipaddr $XTNAME
	if [ $? -ne 0 ]
	then
		exit 100
  	else
		IPADDR=`cat $TMP/IPADDR`
		isipaddrinh $IPADDR
		RETCODE=$?
		if [ $RETCODE -eq 0 ] 
		then
			echo ""
			echo "Address $IPADDR is already in use in $HOSTTABLE."
			echo "To re-use it, please remove the existing entry. "
			echo ""
			exit 1
		fi

		if [ $NISPLUSH = "true" ]
		then 
                  echo ""
		  CMD="echo $IPADDR $XTNAME | /usr/lib/nis/nisaddent -a hosts"
		  echo "Execute the command \"$CMD\""
	          $ECHO "to update the NIS+ hosts database with the new IP address? [y|n] (y): $NL"
 	          read SEL
		  case $SEL in
			y|Y|YES|yes|"")
			  echo $CMD | $SHE
                          echo
			  ;;
			*)
			  ;;
		  esac
                else
		  putxtnameinh $IPADDR $XTNAME
		  if [ $? -ne 0 ]
		  then
			echo "ERROR: failed to add netstation to $HOSTTABLE."
			exit 1
		  fi
		  echo ""
		  echo "The following line was added to $HOSTTABLE:" 
		  cat $TMP/XTLINE
		  rm $TMP/XTLINE > /dev/null 2>&1

		  if [ $AMHMASTER = "true" ]
		  then 
		    echo "Execute the command \"$MAKEHCMD\" to update"
	            $ECHO "the NIS hosts database with the new IP address? [y|n] (y): $NL"
 	            read SEL
		    case $SEL in
			y|Y|YES|yes|"")
			  HERE=`pwd`
			  echo $MAKEHCMD | $SHE
			  cd $HERE
			  ;;
			*)
			  ;;
		    esac
		  elif [ "x$DOMN" != "x" ]
		  then
			cat <<EOF

 ****************************************************************
 * Please ask your site administrator to add the IP address of	*
 * this netstation to the Domain Name Server database.		*
 ****************************************************************

EOF
		  fi
                fi
        fi
	;;
 3|1)
	exit 1
	;;
esac

IPADDR=`cat $TMP/IPADDR`
rm $TMP/IPADDR
export IPADDR

dobootpstuff
RETCODE=$?

case $RETCODE in
    98)
	;;
97|99|100)
	exit $RETCODE
	;;
     *)
	break
	;;
esac

done	# network parameter loop

if [ $OS = "sco" ]
then
  $PSCMD | fgrep -v "grep" | grep scologin > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    echo
    echo "To start up scologin so that the netstation gets a login screen"
    echo "please select menu item 5 (scologin Administration) from main menu."
    echo
    exit 0
  fi
  DMRUNNING=true
  DM="scologin"
else

	#	Check if dtlogin  or vuelogin or xdm is running.
	#	If not offer user to set xdm up in $INITTAB 
	#	and start it now.
  DMRUNNING=false

  DMS="dtlogin vuelogin xdm"
  for DM in $DMS
  do
    if [ $OS = "sun4" ]
    then
      $PSCMD | awk '{print $NF}' | grep "^$DM\$"  > /dev/null 2>&1
    else
      ps -e | awk '{print $NF}' | grep "^$DM\$"  > /dev/null 2>&1
    fi

    if [ $? -eq 0 ]
    then
      DMRUNNING=true
      break
    fi
  done

  if [ "$DMRUNNING" = "false" ]
  then
    while [ 0 ]
    do
      echo ""
      $ECHO "xdm is not running. Do you want to start xdm? [y|n|?] (y) $NL"
      read SEL 
      echo ""
      case $SEL in
        y|Y|YES|yes|"")
	  startxdm
	  break
	  ;;
       \?)	 
	  cat <<EOF

XDM automatically gives the user a login prompt and
runs a script which starts X applications.

For more information, see the Technical Reference manual for HP Netstations.

EOF
		continue
		;;
	  *)	 
		echo "OK  .. no changes to xdm."
		break
		;;
	  esac
	done
  else
	DMRUNNING=true
  fi
fi


if [ "$DMRUNNING" = "true" ]
then
cat <<EOF

$DM is already running
This netstation will start using $DM after powerup if factory defaults
in the Startup config screen have not been changed.
EOF
  if [ $DM = "Vuelogin" ]
  then
    echo "See the \"Technical Reference Manual\" for HP Netstations for"
    echo "information on configuring Vuelogin."
  fi

  #
  # Engage in a dialog with installer in order to
  # put a .xsession file in user home directories if so desired
  # 

  makexsession
fi





echo ""
$ECHO "Add another netstation? [y|n] (y): $NL"
read SEL

case $SEL in
    y|Y|yes|YES|Yes|"")
	;;
    *)
	exit 100
	;;
esac

done	# add a netstation loop
