#!/sbin/sh
#@(#)HP X Station Admin Scripts
#
# startxdm	Insert xdm entry in /etc/inittab or /etc/rc
#

if [ $OS = "sco" ]
then
  # xdm admin not supported on SCO Unix
  exit 1
fi

if [ ! -x $XDMBIN ] 
then
	echo "No $XDMBIN executable. Cannot continue."
	exit 1
fi

if [ ! -w $INITTAB ] 
then
	echo "$INITTAB is not writeable. Cannot continue."
	exit 1
fi

if [ $OS = "sun5" ]
then
  ps -e | awk '{print $4 }' | egrep '(vuelogin)|(dtlogin)' > /dev/null 2>&1
else
  $PSCMD | fgrep -v "grep" | egrep '(vuelogin)|(dtlogin)' > /dev/null 2>&1
fi

if [ $? -eq 0 ]
then
	echo "vuelogin or dtlogin is already running."
	echo "xdm cannot run at the same time."
	echo ""
	exit 1
fi


cat <<EOF

This selection will :
	1) Check if xdm is being started up from $INITTAB.
	   If this is not the case, $INITTAB will be edited in order to
	   start up xdm automatically whenever your host computer boots.
	                 and
	2) After prompting you, xdm will be started up immediately.
 
EOF

if [ $OS = "sun5" ]
then
  $ECHO "Add xdm xdm.hp start-up script to directory $INITTAB ? [y|n] (y): $NL"
else
  cp $INITTAB $INITTAB.orig.$$
  echo copied existing $INITTAB to $INITTAB.orig.$$
  $ECHO "Add xdm start-up code to $INITTAB ? [y|n] (y): $NL"
fi

read SEL

echo ""

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

if [ $OS = "sun5" ]
then
  #
  # "INITTAB" is here directory /etc/init.d
  #
  if [ -f $INITTAB/xdm.hp ]
  then
    echo ""
    rm $INITTAB/xdm.hp
    echo "removed existing script $INITTAB/xdm.hp"
  fi
  cp $ADMINDIR/xdm.hp $INITTAB/xdm.hp
  chown root $INITTAB/xdm.hp
  chgrp root $INITTAB/xdm.hp
  chmod 644 $INITTAB/xdm.hp
  echo "script xdm.hp added to $INITTAB"
  ln -s $INITTAB/xdm.hp /etc/rc3.d/S172xdm.hp > /dev/null 2>&1
  ln -s $INITTAB/xdm.hp /etc/rc3.d/K172xdm.hp > /dev/null 2>&1
  echo "added links in /etc/rc3.d pointing to $INITTAB/xdm.hp"
fi
    

if [ $OS != "sun5" ]
then
  	#
	# Disregard lines with "#xtadm" in them 
	# They probably are sitting there from previous installtions
	#
  sed '/#xtadm/d' $INITTAB | egrep -v '^[ 	]*#' |  egrep '(xdm)|(dtlogin)' > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
	echo "Seems to be an entry for xdm or dtlogin in $INITTAB already."
	echo "No changes were made to $INITTAB."
	echo ""
  else
	echo ""

	case $OS in
	  hp|hp10)
  		echo $XDMLINE1 >> $INITTAB 
  		echo $XDMLINE1
		;;
	  *)
  		#
		# Disregard lines with "#xtadm" in them. 
		# They probably are sitting there from previous installations
		#
                sed '/#xtadm/d' $INITTAB > $TMP/inittab
		cp $TMP/inittab $INITTAB
		rm $TMP/inittab
		echo ""	>> $INITTAB	
  		echo $XDMLINE0 >> $INITTAB 
  		echo $XDMLINE1 >> $INITTAB 
  		echo $XDMLINE1
		echo $XDMLINE2 >> $INITTAB 
		echo $XDMLINE2
		echo $XDMLINE3 >> $INITTAB 
		echo $XDMLINE3
		;;
	esac

	echo ""
	echo "Above entry appended to $INITTAB."
	echo ""

  fi
fi

#------------------------------------------------------------------#
# Disable dtlogin 
#------------------------------------------------------------------#
#
if [ -x /usr/dt/bin/dtconfig ]
then
 /usr/dt/bin/dtconfig -d  > /dev/null 2>&1
fi

if [ $OS = "sun4" ]
then
  $PSCMD | awk '{print $NF}'  | egrep '^xdm$' > /dev/null 2>&1
else
  ps -e | awk '{print $NF}'  | egrep '^xdm$' > /dev/null 2>&1
fi

if [ $? -eq 0 ]
then
	echo "xdm is already running."
	echo ""
	exit 1
fi

$ECHO " "
$ECHO "Start xdm running now? [y|n|?] (y): $NL"
read SEL

echo ""

case $SEL in
    y|Y|yes|YES|Yes|"")
	;;
     \?)
	cat <<EOF

Answering yes will start running the xdm daemon right away.
Answering no will wait until the next time your computer is rebooted
to run xdm.

Your X stations should be able to start using xdm after powerup
if factory defaults in the Server config screen have not been changed.

If your X station has been re-configured differently from factory defaults:
	1. Press and hold F12 on the X station keyboard.
	2. Click on "Server".
	3. Make sure XDMCP is "Enabled".
	4. If blank, "Host" defaults to the file server.
	5. Click on "OK".
	6. Turn the X station off and then back on again.

EOF
	;;
    *)
	exit 100
	;;
esac

#------------------------------------------------------------------#
# kill dtlogin
#------------------------------------------------------------------#
#
if [ -x /usr/dt/bin/dtconfig ]
then
 /usr/dt/bin/dtconfig -kill  > /dev/null 2>&1
fi

rm $XDMDIR/xdm-pid > /dev/null 2>&1

case $OS in
  hp)
	telinit q
	;;
  hp10)
	/sbin/init q
	;;
      *)
	cd /
	sh -c "$XDMBIN -config $XDMCONFIG"
	;;
esac

sleep 3


if [ $OS = "sun4" ]
then
  $PSCMD | awk '{print $NF}'  | egrep '^xdm$' > /dev/null 2>&1
else
  ps -e | awk '{print $NF}'  | egrep '^xdm$' > /dev/null 2>&1
fi


if [ $? -eq 0 ]
then
	echo " "
	echo "xdm has been started."
else
	echo " "
	echo "ERROR: couldn't start xdm main process."
	exit 1
fi
