#!/sbin/sh
#@(#)HP Netstation Admin Scripts
#
# xtadm		Perform various HP Netstation related administrative tasks 
#


# auto export all variables for simplicity
set -a	

# make sure our path does not get overwritten
unset ENV

# default path
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/etc:/usr/etc:/usr/ucb:/usr/ccs:/usr/ccs/bin:/usr/local/bin"

FILEACCESS=tftp                 # set to "nfs" or "tftp"

# The following lines were removed because they don't work in some
# cases with the B.07 integration. Instead we hardcode the path.
#THISPATH=`ls -la $0 | awk '{print $NF}'`
#THISDIR=`dirname $THISPATH`
THISDIR=/opt/hpxt/enware2/lbin

#
# Sun, and HP-UX 10.0 installs deliver some environment variables 
# in this file
#
if [ -f "${THISDIR}/env.vars" ]
then
  . ${THISDIR}/env.vars
  if [ "$WANTTFTP" = "false" ]
  then
    FILEACCESS=nfs
  fi
fi

ADMINDIR=${ADMINDIR:-$THISDIR}
if [ "$ADMINDIR" = "." ]
then
# This would be bad security.
  ADMINDIR=/opt/hpxt/enware2/lbin
fi

#
# Only "root" can run 
# Check if root is executing this script. 
# On SCO this is complicated by the fact 
# that if a "su" has been done we cannot easily tell
# if user has root priviledges. (LUID is constant for a session)
#
if [ "$OS" = "sco" ]
then
  touch /etc/$$ > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    USER="notroot"
  else
    USER="root"
    rm /etc/$$
  fi
else
  if [ "$OS" = "sun5" ]
  then
    USER=`id | awk -F"(" '{print $2}' | awk -F")" '{print $1}'`
  else
    USER=`whoami`
  fi
fi
if [ "$USER" != "root" ]
then
  echo "You must be super-user (root) to execute this script."
  exit 1
fi


#-----------------------------------------------------------------------#
# Don't run on diskless clients
#-----------------------------------------------------------------------#
#
if [ "$OS" = "hp10" ]
then

  if [ -f /usr/bin/dcnodes -o -f /usr/sbin/dcnodes ]
  then
    dcnodes -q
    if [ $? -eq 2 ]
    then
      echo
      echo "ERROR: script should run on server, cannot run on diskless client."
      echo
     exit 1
    fi
  fi

fi


echo "Initializing..."
if [ -x /usr/bin/X11/xdm ]
then
  XDMBIN=${XDMBIN:-/usr/bin/X11/xdm}
  XDMDIR=${XDMDIR:-/usr/lib/X11/xdm}
fi


XDMDIR=${XDMDIR:-/opt/hpxt/enware2/xdm}
egrep XDMDIR ${THISDIR}/env.vars > /dev/null
if [ $? -ne 0 ]
then
  echo "XDMDIR=${XDMDIR}" >> ${THISDIR}/env.vars
  if [ "$OS" = "hp" -o "$OS" = "hp10" ]
  then
     FILESET=HPXT2-ADMIN
    /opt/hpxt/enware2/lbin/xtutil_add-file ${THISDIR}/env.vars
  fi
fi

XDMBIN=${XDMBIN:-/opt/hpxt/enware2/xdm/xdm}
egrep XDMBIN ${THISDIR}/env.vars > /dev/null
if [ $? -ne 0 ]
then
  echo "XDMBIN=${XDMBIN}" >> ${THISDIR}/env.vars
  if [ "$OS" = "hp" -o "$OS" = "hp10" ]
  then
     FILESET=HPXT2-ADMIN
    /opt/hpxt/enware2/lbin/xtutil_add-file ${THISDIR}/env.vars
  fi
fi

XDMCONFIG=${XDMDIR}/xdm-config

if [ "$OS" = "sun5" ]
then
  HOSTNAME=`uname -n`
else
  HOSTNAME=`hostname`
fi


EXPORTS=/etc/exports
HOSTTABLE="/etc/hosts"
YPWHICH=/usr/bin/ypwhich
NISPLUS="none"

XTNAME=""
NAME=""
PLOTTERMSG="or plotter"

RELEASE=0

#
#		Assume we are not NIS master server
#		for service or hosts map
#

AMSMASTER=false
AMHMASTER=false
NISS=false
NISH=false

if [ -f $YPWHICH ]
then
  NISHMASTER=`ypwhich -m hosts 2>&1`
  echo $NISHMASTER | egrep "(ypwhich:)|(Can\'t find.*master)" > /dev/null
  if [ $? -ne 0 ]
  then
    NISH=true
    if [ "$NISHMASTER" = "$HOSTNAME" ]
    then
      AMHMASTER=true
    fi
  fi

  NISSMASTER=`ypwhich -m services 2>&1`
  echo $NISSMASTER | egrep "(ypwhich:)|(Can\'t find.*master)" > /dev/null
  if [ $? -ne 0 ]
  then
    NISS=true
    if [ "$NISSMASTER" = "$HOSTNAME" ]
    then
      AMSMASTER=true
    fi
  fi
fi

#
#		Assume we do not run NIS+ 
#		for service or hosts map
#

NISPLUSH="false"
NISPLUSS="false"


XSTARTUP=`egrep '^[^!].+startup' $XDMDIR/xdm-config 2> /dev/null | head -1 | awk '{print $2}'`
if [ -f "$XSTARTUP" ]
then
  :
else
  XSTARTUP=$XDMDIR/Xstartup
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

if [ -x /sbin/sh ]
then
    SHE="/sbin/sh"
else
    SHE="/sbin/sh"
fi



case $OS in
    hp|hp10)
	PSCMD="ps -ef"
	PIDFIELD="\$2"
	PPIDFIELD="\$3"
	DFCMD="bdf"
	MAKEHCMD="/usr/etc/yp/ypmake hosts"
	MAKESCMD="/usr/etc/yp/ypmake services"
	PINGSTART="ping"
	PINGEND="100 -n 2"
	INITTAB="/etc/inittab"
	MOUNTTABLE=/etc/checklist
	TFTP=/usr/bin/tftp
	if [ "$OS" = "hp10" ]
	then
	  EXPORTFS=/usr/sbin/exportfs
	  MOUNTTABLE=/etc/fstab
	  BOOTPD=/usr/lbin/bootpd
          BOOTPTABLE=/etc/bootptab
	  BOOTPQUERY="/usr/sbin/bootpquery" 
	  INETD=/usr/sbin/inetd
	  LPSHUT=/usr/sbin/lpshut
	  CANCEL=/usr/bin/cancel
	  LPADMIN=/usr/sbin/lpadmin
	  LPSCHED=/usr/sbin/lpsched
	  ACCEPT=/usr/sbin/accept
	  ENABLE=/usr/bin/enable
	  INTFDIR=/etc/lp/interface
	  MODELDIR=/usr/lib/lp/model
	else
	  EXPORTFS=/usr/etc/exportfs
	  BOOTPD=/etc/bootpd
          BOOTPTABLE=/etc/bootptab
	  BOOTPQUERY="/etc/bootpquery" 
	  INETD=/etc/inetd
	  LPSHUT=/usr/lib/lpshut
	  CANCEL=/usr/bin/cancel
	  LPADMIN=/usr/lib/lpadmin
	  LPSCHED=/usr/lib/lpsched
	  ACCEPT=/usr/lib/accept
	  ENABLE=/usr/bin/enable
	  INTFDIR=/usr/spool/lp/interface
	  MODELDIR=/usr/spool/lp/model
	fi
	NSLOOKUP=/usr/bin/nslookup
	ECHO="echo"
	NL="\c"
	XDMLINE1="xd:234:respawn:$XDMBIN -nodaemon -config $XDMCONFIG < /dev/null > /dev/null 2>&1"
	XDMLINE2=""
	XDMLINE3=""

	if [ "$OS" != "hp10" ]
	then
	  grep "^tftp:" /etc/passwd > /dev/null 2>&1
	  if [ $? -ne 0 ]
	  then
	    ypcat passwd | grep "^tftp:" > /dev/null 2>&1
	    if [ $? -ne 0 ]
	    then
	      echo "ERROR: No user tftp in /etc/passwd, - please add account!"
	      exit 1
	    fi
	  fi
	fi

	RELEASE=`uname -r | sed -e 's/[^0-9.]//g' -e 's/^\.//' | cut -d. -f1`
	REMSH=remsh
	;;

    sco)
	PSCMD="ps -ef"
	PIDFIELD="\$2"
	PPIDFIELD="\$3"
	DFCMD="df"
	MAKEHCMD="/usr/etc/yp/ypmake hosts"
	MAKESCMD="/usr/etc/yp/ypmake services"
	INITTAB="/etc/rc.d/7/udaemons"
	PINGSTART="ping -c3"
	PINGEND=""
	EXPORTFS=/etc/exportfs
	MOUNTTABLE=/etc/checklist
	BOOTPD=/etc/bootpd
	BOOTPTABLE=/etc/bootptab
	TFTP=/usr/bin/tftp
	INETD=/etc/inetd
	NSLOOKUP=/usr/bin/nslookup
	ECHO="echo -n"
	NL=""
	SCOLOGINENABLE="/etc/scologin enable"
	SCOLOGINDISABLE="/etc/scologin disable"
	SCOLOGINQUERY="/etc/scologin query"
	SCOLOGINSTART="/etc/scologin start"
	SCOLOGINSTOP="/etc/scologin stop"
        PLOTTERMSG=""
	REMSH=rcmd
	;;

    sun5)           # Solaris 2

  	if [ "x$OPENWINHOME" = "x" ]
  	then
	  if [ -d /usr/openwin -o -h /usr/openwin ]
	  then
	    OPENWINHOME=/usr/openwin
	  else
            if [ "$OS" != "" ]
	    then
		echo
		echo "ERROR: OPENWINHOME not set"
	    fi
	  fi
	fi

	if [ "x$LD_LIBRARY_PATH" = "x" ]
	then
	      LD_LIBRARY_PATH=$OPENWINHOME/lib
	fi

	PSCMD="ps -ef"
	PIDFIELD="\$2"
	PPIDFIELD="\$3"
	DFCMD="df -k"
	MAKEHCMD="cd /var/yp; make hosts"
	MAKESCMD="cd /var/yp; make services"
	PINGSTART="ping"
	PINGEND="100 2"
	EXPORTFS=/usr/sbin/share
	EXPORTS=/etc/dfs/dfstab
	INITTAB="/etc/init.d"
	MOUNTTABLE=/etc/vfstab

	TFTP=/usr/bin/tftp
	INETD=/usr/sbin/inetd
	NSLOOKUP=/usr/sbin/nslookup
	ECHO="echo"
	NL="\c"

        awk '$1 == "hosts:" &&  $2 == "nisplus"' /etc/nsswitch.conf \
		| fgrep nisplus > /dev/null 
        if [ $? -eq 0 ]
	then
	  NISPLUSH="true"
	  AMHMASTER=false
	  NISH=false
        fi

        awk '$1 == "services:" &&  $2 == "nisplus"' /etc/nsswitch.conf \
		| fgrep nisplus > /dev/null 
        if [ $? -eq 0 ]
	then
	  NISPLUSS="true"
	  AMSMASTER=false
	  NISS=false
	fi
        
	PRINTCAP=/etc/printcap
	REMSH=rsh
	;;
    *)           # sun4 or unknown OS
	if [ "$OS" != "sun4" ]
	then
		OS=`uname`
		echo "WARNING: OS is not supported, assuming BSD."
	fi

  	if [ "x$OPENWINHOME" = "x" ]
  	then
	  if [ -d /usr/openwin -o -h /usr/openwin ]
	  then
	    OPENWINHOME=/usr/openwin
	  else
	   if [ "$OS" = "sun4" ]
	   then
            echo
            echo "ERROR: OPENWINHOME not set"
	   fi
	  fi
	fi

	if [ "x$LD_LIBRARY_PATH" = "x" ]
	then
	      LD_LIBRARY_PATH=$OPENWINHOME/lib
	fi
		
	if [ -x /etc/bootpd ]
	then
	  BOOTPD=/etc/bootpd
	fi
	if [ -f /etc/bootptab ]
	then
	  BOOTPTABLE=/etc/bootptab
	fi
	PSCMD="ps -axjc"
	PIDFIELD="\$2"
	PPIDFIELD="\$1"
	DFCMD="df"
	MAKEHCMD="cd /var/yp; make hosts"
	MAKESCMD="cd /var/yp; make services"
	PINGSTART="ping"
	PINGEND="100 2"
	EXPORTFS=/usr/etc/exportfs
	INITTAB="/etc/rc.local"
	MOUNTTABLE=/etc/fstab

	if [ -x /usr/bin/tftp ]
	then
	  TFTP=/usr/bin/tftp
	else
	  TFTP=/usr/ucb/tftp
	fi
	if [ -x /usr/sbin/inted ]
	then
	  INETD=/usr/sbin/inetd
	else
	  INETD=/usr/etc/inetd
	fi
	NSLOOKUP=/usr/etc/nslookup
	ECHO="echo -n"
	NL=""

	XDMLINE0="# ## DON'T EDIT these 4 lines (xtadm manipulates) ########xtadm"
	XDMLINE1="if [ -f $XDMBIN ]; then #########################xtadm"
	XDMLINE2="$XDMBIN -config $XDMCONFIG < /dev/null > /dev/null 2>&1 ; echo xdm #xtadm"
	XDMLINE3="fi #######################################################xtadm"

	PRINTCAP=/etc/printcap
	REMSH=rsh
	;;
esac

if [ -f /opt/hpxt/enware2/hpnp -o -d /opt/hpxt/enware2/hpnp ]
then
  HPNP=/opt/hpxt/enware2/hpnp
else
  if [ -d /usr/lib/hpnp ]
  then
    HPNP=/usr/lib/hpnp
  else
    if [ -d /opt/enware/usr/lib/hpnp ]
    then
      HPNP=/opt/enware/usr/lib/hpnp
    fi
  fi
fi

BOOTPD=${BOOTPD:-/opt/hpxt/enware2/bootp/bootpd}
BOOTPTABLE=${BOOTPTABLE:-/opt/hpxt/enware2/bootp/bootptab}
BOOTPQUERY=${BOOTPQUERY:-/opt/hpxt/enware2/bootp/bootpquery}
BPINETDLINE="bootps       dgram  udp wait   root $BOOTPD  bootpd $BOOTPTABLE"
BOOTPCLINE="bootpc        68/udp                 # Bootstrap Protocol Client"
BOOTPSLINE="bootps        67/udp                 # Bootstrap Protocol Server"

PATH=$ADMINDIR:$PATH

#
# get domain name and nameserver IP address
#
if [ -f /etc/resolv.conf -a -f $NSLOOKUP ]
then
  DOMN=`egrep '^domain[ 	]' /etc/resolv.conf | head -1 | awk '{print $2}'`
  DOMA=`egrep '^nameserver[ 	]' /etc/resolv.conf | head -1 | awk '{print $2}'`
fi

if [ "$DOMA" != "" ]
then
  echo $DOMA | egrep '[0-9]' > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    DOMA="NONE"
    DOMN=""
  fi
else
  DOMA="NONE"
  DOMN=""
fi

DOMN=`echo $DOMN | sed -e 's/[.]*$//'`

if [ "$DOMN" != "" ]
then
  echo $DOMN | egrep '[a-z|A-Z]' > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    DOMN=""
    DOMA="NONE"
  fi

  DOMN=".$DOMN"
else
  DOMN=""
  DOMA="NONE"
fi


#
#
# Does bootp daemon seem to understand the ba tag
# 1) so we may broadcast queries using bootpquery 
# 2) and put ba tags in bootptab
#
#
strings $BOOTPD 2>/dev/null | egrep 'ba -- broadcast address' > /dev/null
if [ $? -eq 0 ]
then
  #
  # yes, it seem to know about it, make a note for later
  #
  UNDERSTANDSBA="true"
else
  UNDERSTANDSBA="false"
fi

while [ 0 ] 
do
	cat <<EOF

	_________________________________________________
		HP NETSTATION ADMINISTRATIVE TASKS
			MAIN MENU

	1) Add a netstation

	2) Remove a netstation
EOF
	if [ "$OS" != "sco" ]
	then
	cat <<EOF

	3) Printers, plotters

	4) Installation testing 

	5) XDM Administration
EOF
	else
	cat <<EOF

	3) Printers

	4) Installation testing

	5) scologin Administration
EOF
	fi
        
	cat <<EOF

        6) Manage software versions

		?) Help		x) exit

EOF
  	$ECHO "Please enter selection (default=1): $NL"
	read SEL 
	case $SEL in
	   1|"")
		addxt
		;;
	      2)
		rmxt
		;;
	      3)
		printermenu
		;;
	      4)
		utils
		;;
	      5)
		xdmmenu
		;;
	      6)
		versions
		;;
	    x|X)
		break
		;;
	     \?)
		cat <<EOF | more

Selecting:
1) Add a netstation
	Adds a netstation to the hosts database and to /etc/bootptab.
2) Remove a netstation
	Removes a netstation from the hosts database and from /etc/bootptab.
3) Printers, plotters
	Allows you to add or remove a netstation connected printer $PLOTTERMSG
	to this computer's print spooler.
4) Installation testing
	Tests the netstation software installation, bootp, NFS, and tftp.
5) XDM/scologin Administration
	Allows you to start/stop and configure xdm/scologin.
6) Manage software versions
     The SW installation will detect already installed versions of ENWARE and
     try to save existing versions.
     The files/directories in this directory are the ones most likely
     to change between software releases. The directory holds the netstation 
     boot images, fonts, local clients, and some configuration files.
     Keeping several versions of this directory around makes it possible
     for you to make the netstations use the version of your choice.
     This selection will:
      -display installed software versions.
      -display the version currently being used (the active version).
      -allow you to change the active version or remove a version.
x) exit
	Exits from xtadm.

For help at most prompts enter a ?

These administration menus assume that this host computer is the BOOTP server 
(netstation network configuration), file server (downloaded server image and 
fonts), and X session manager (VUE or xdm), for the netstation(s).

EOF
		;;
	      *)
		echo ""
		echo "Invalid selection"
		continue
		;;
	esac

	RETCODE=$?
	if [ $RETCODE -eq 20 ]
	then
	  break
	fi

	if [ $RETCODE -eq 99 ]
	then
	  exit $RETCODE
	fi

	if [ $RETCODE -ne 100 ]
	then
	  $ECHO "Please press <Enter> to continue: $NL"
	  read SEL
	fi
done

exit 0
