#!/sbin/sh
#@(#)HP X Station Admin Scripts
#
# addxt		Do X station addition 
#

cat <<EOF

	_________________________________________________
		HP X STATION ADMINISTRATIVE TASKS
			ADD AN X STATION

To add an X station to this computer you will need to obtain the
following information from your local network administrator:

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

	* The internet protocol (IP) address of the X station.

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

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

	* The subnet mask of the X station.

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



EOF

$ECHO "Continue adding an X station [y|n] (y): $NL"
read SEL

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

cat <<EOF


	_________________________________________________
		HP X STATION ADMINISTRATIVE TASKS
			ADD AN X STATION
EOF

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

getxtmodel	# find out which series of X station he has

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

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

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

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 X station? [y|n] (y): $NL"
 	SEL=`line` 
	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"
 	          SEL=`line` 
		  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 X station 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"
 	            SEL=`line` 
		    case $SEL in
			y|Y|YES|yes|"")
			  HERE=`/bin/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 X station to the Domain Name Server database.		*
 ****************************************************************

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

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

if [ ! -f $BOOTPD ]
then
	echo "WARNING: $BOOTPD doesn't exist!"
	echo "Check the log file from the tape installation."
	echo ""
fi 

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 X station 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
          DM="xdm"
	  DMRUNNING=true
	  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 X stations.

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 X station 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 X stations 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
  # 

  if [ $DM = "xdm" ]
  then
    makexsession
  fi
fi





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

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

done	# add an X station loop
