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

# Before all else do 8.0 xtadm iff it exists
if [ -x /opt/hpxt/enware2/lbin/xtadm ]
then
    . /opt/hpxt/enware2/lbin/xtadm
    exit
fi

set -a	
unset ENV

# default path
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/etc:/usr/etc:/usr/ucb:/usr/ccs:/usr/ccs/bin:/usr/local/bin"
# auto export all variables for simplicity

# These 2 variables are poked from the suninstall or scoinstall scripts

INSTALLDIR=unknown              # set to top of installation tree
FILEACCESS=tftp                 # set to "nfs" or "tftp"

# Variable below is poked from SUN installation scripts under rare (?) circum-
# stances when we are unable to create a symbolic link in /usr/lib/X11/700X
# If it is set the bf tag in the bootptab entries for terminals will be modified
# to the alternate boot path, typically /HPXT.B05/bin/

ALTLINK=none

THISPATH=`ls -la $0 | awk '{print $NF}'`
THISDIR=`dirname $THISPATH`

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

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, it cannot run on diskless client."
      echo
      exit 1
    fi
  fi
fi

if [ "$XTHOME" = "" ]
then 
  XTHOME=/usr/lib/X11/700X
fi

echo "Initializing..."

if [ "$OS" = "hp" ]
then
  XDMDIR=/usr/lib/X11/xdm
  XDMBIN="/usr/bin/X11/xdm"
  BOOTPTABLE="/etc/bootptab"
else
  if [ $INSTALLDIR = "unknown" ]
  then
    XDMDIR=/usr/lib/X11/xdm
    XDMBIN="/usr/bin/X11/xdm"
    BOOTPTABLE="/etc/bootptab"
  else
    XDMDIR=${INSTALLDIR}/usr/lib/X11/xdm
    XDMBIN=${INSTALLDIR}/usr/bin/X11/xdm
    BOOTPTABLE=${INSTALLDIR}/usr/etc/700X/bootptab
    if [ "$OS" = "hp10" ]
    then
      XDMDIR=${INSTALLDIR}/xdm
      XDMBIN=${INSTALLDIR}/xdm/xdm
      BOOTPTABLE=/etc/bootptab
    fi
  fi
fi

XDMCONFIG=${XDMDIR}/xdm-config


#  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`
    OSVER=`uname -r | cut -d"." -f 2`
    if [ "$OSVER" = "10" -o "$OSVER" = "11" ]
    then
      OS="hp10"
    fi
    if [ "$OSVER" = "09" -o "$OSVER" = "9" ]
    then
      OS="hp"
    fi
  fi
fi


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"


#
#		List the model numbers in each series
#		of X terminals that are currently supported.
#

MODELS_700X="C2300A C2300B" 
MODELS_700RX="C2700A C2703A C2707A C2708A C3225A"
MODELS_ENVIZEX="C2730A C2731A C3253A C2730ATR C2731ATR C3253ATR C3253A C2730AVG C2731AVG C3253AVG"
MODELS_ENTRIA="C3230A C3231A C3233A C3234A C3236A C3262A C3263A C3264A C3265A"


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 | fgrep 'ypwhich:' > /dev/null
  if [ $? -ne 0 ]
  then
    NISH=true
    if [ $NISHMASTER = $HOSTNAME ]
    then
      AMHMASTER=true
    fi
  fi

  NISSMASTER=`ypwhich -m services 2>&1`
  echo $NISSMASTER | fgrep 'ypwhich:' > /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"
else
  if [ -d /tmp ]
  then
    TMP="/tmp" ]
  else
    echo "No /tmp or /usr/tmp working directories, - cannot continue."
    exit 1
  fi
fi


if [ -x /sbin/sh ]
then
    SHE="/sbin/sh"
else
    SHE="/sbin/sh"
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


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
	  BOOTPQUERY="/usr/sbin/bootpquery" 
	  ADMINDIR=$INSTALLDIR/lbin
	  XTHOME=$INSTALLDIR/xthome
	  INETD=/usr/sbin/inetd
	  TFTPDIR=""
	  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
	  BOOTPQUERY="/etc/bootpquery" 
          BOOTPTABLE="/etc/bootptab"
	  ADMINDIR=/usr/bin/X11/700X/admin
	  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 -config $XDMCONFIG -nodaemon < /dev/null > /dev/null 2>&1"
	XDMLINE2=""
	XDMLINE3=""

	if [ "$OS" != "hp10" ]
	then
	  TFTPDIR=`grep "^tftp:" /etc/passwd | awk -F: ' {print $6}' | head -1`
	  if [ "$TFTPDIR" = "" ]
	  then
	    TFTPDIR=`ypcat passwd | grep "^tftp:" | awk -F: ' {print $6}' | head -1`
	    if [ "$TFTPDIR" = "" ]
	    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
	ADMINDIR=${INSTALLDIR}/usr/bin/X11/700X/admin
	TFTP=/usr/bin/tftp
	INETD=/etc/inetd
	NSLOOKUP=/usr/bin/nslookup
	ECHO="echo -n"
	NL=""
	TFTPDIR=$INSTALLDIR
	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 [ $INSTALLDIR = "unknown" ]
	then
	  ADMINDIR=/usr/bin/X11/700X/admin
	else
	  ADMINDIR=${INSTALLDIR}/usr/bin/X11/700X/admin
	fi

  	if [ "x$OPENWINHOME" = "x" ]
  	then
	  if [ -d /usr/openwin -o -h /usr/openwin ]
	  then
	    OPENWINHOME=/usr/openwin
	  else
            echo
            echo "ERROR: OPENWINHOME not set"
	  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

	if [ $INSTALLDIR = "unknown" ]
	then
	  BOOTPD=/usr/etc/700X/bootpd
	  BOOTPQUERY="/usr/etc/700X/bootpquery" 
	else
	  BOOTPD=${INSTALLDIR}/usr/etc/700X/bootpd
	  BOOTPPARAM=${INSTALLDIR}/usr/etc/700X/bootptab
	  BOOTPQUERY=${INSTALLDIR}/usr/etc/700X/bootpquery
	fi

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

	if [ $INSTALLDIR = "unknown" ]
	then
	  echo ""
	  echo "SYSTEM ERROR: Can't find installation directory!"
	  echo "It is supposed to be set up by the tape installation script."
	  echo ""
	  exit 1
	else
	  TFTPDIR=$INSTALLDIR
	fi

        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
		echo "WARNING: $OS is not supported, assuming BSD."
	fi

	if [ $INSTALLDIR = "unknown" ]
	then
	  ADMINDIR=/usr/bin/X11/700X/admin
	else
	  ADMINDIR=${INSTALLDIR}/usr/bin/X11/700X/admin
	fi

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

	if [ "x$LD_LIBRARY_PATH" = "x" ]
	then
	      LD_LIBRARY_PATH=$OPENWINHOME/lib
	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 [ $INSTALLDIR = "unknown" ]
	then
	  BOOTPD=/usr/etc/700X/bootpd
	  BOOTPQUERY="/usr/etc/700X/bootpquery" 
	else
	  BOOTPD=${INSTALLDIR}/usr/etc/700X/bootpd
	  BOOTPPARAM=${INSTALLDIR}/usr/etc/700X/bootptab
	  BOOTPQUERY=${INSTALLDIR}/usr/etc/700X/bootpquery
	fi

	TFTP=/usr/ucb/tftp
	INETD=/usr/etc/inetd
	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"

	if [ $INSTALLDIR = "unknown" ]
	then
	  echo ""
	  echo "SYSTEM ERROR: Can't find installation directory!"
	  echo "It is supposed to be set up by the tape installation script."
	  echo ""
	  exit 1
	else
	  TFTPDIR=$INSTALLDIR
	fi

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

#
#               For sun4  edit location of
#               binaries etc into printcap if and of filter scripts,
#
#
#               For HP-UX  do nothing as binaries and script
#               always go into /usr/lib/hpnp
#

if [ $OS = "sun4" ]
then
  HPNP=unknown                    # may be modified by install script
  if [ $HPNP = "unknown" ]                # to say /usr/lib/hpnp
  then
    HPNP=${INSTALLDIR}/usr/lib/hpnp  # hpnp, hpnptyd and filters went here
  fi                                      # by default
  for i in `ls $HPNP/*.sh*`
  do
    fgrep "hpnp" ${i} > /dev/null
    if [ $? -eq 0 ]
    then
      grep "$HPNP" ${i} > /dev/null
      if [ $? -ne 0 ]
      then
        cat ${i} | sed -e "s-/usr/lib/hpnp/cfg-${HPNP}-g" \
        | sed -e "s-/usr/lib/hpnp-${HPNP}-g" > ${i}.tmp.$$
        cp ${i}.tmp.$$ ${i}
        rm ${i}.tmp.$$
      fi
    fi
  done
else
  if [ $OS = "sco" -o $OS = "sun5" ]
  then
    HPNP=${INSTALLDIR}/usr/lib/hpnp
  else
    if [ $OS = "hp10" ]
    then
      HPNP=$INSTALLDIR/hpnp
    else
      HPNP=/usr/lib/hpnp
    fi
  fi
fi


BPINETDLINE="bootps       dgram  udp wait   root $BOOTPD  bootpd $BOOTPPARAM"
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


if [ $INSTALLDIR = "unknown" ]
then
  SWHOME=/usr/lib/X11/700X
else
  SWHOME=$INSTALLDIR/usr/lib/X11/700X
  if [ "$OS" = "hp10" ]
  then
    SWHOME=$XTHOME
  fi
fi

if [ "$TFTPDIR" = "/" ]
then 
  TFTPDIR=""
fi


BINDIR="${XTHOME}/bin"
RGBFILE="${XTHOME}/rgb.txt"
FONTDIR="${XTHOME}/fonts"
CONFIGDIR="${XTHOME}/config"
ETCDIR="${XTHOME}/etc"

NFSDIRS="$BINDIR $FONTDIR"


#
#
# 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 | 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 X STATION ADMINISTRATIVE TASKS
			MAIN MENU

	1) Add an X station

	2) Remove an X station
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 an X station
	Adds an X station to the hosts database and to /etc/bootptab.
2) Remove an X station
	Removes an X station from the hosts database and from /etc/bootptab.
3) Printers, plotters
	Allows you to add or remove an X station connected printer $PLOTTERMSG
	to this computer's print spooler.
4) Installation testing
	Tests the X station 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 of $XTHOME.
     The files/directories in this directory are the ones most likely
     to change between software releases. The directory holds the X station 
     boot images, fonts, local clients, and some configuration files.
     Keeping several versions of this directory around makes it possible
     for you to make the X stations 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 
(X station network configuration), file server (downloaded server image and 
fonts), and X session manager (VUE or xdm), for the X station(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

