#!/bin/ksh -p
#
# ident "@(#)xdm-reset-dpy.sh	1.2 04/01/14"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
#
# Script to reinitialize empty state for XDM
# Pass SRSS config directory as first arg (e.g. reset-dpy /tmp/SUNWut/config)
#

CONFIGDIR=$1
CONFIGXDIR=$CONFIGDIR/xconfig
DMCONFIGDIR=/etc/X11/xdm

cleanup() {
  /bin/mkdir -p $CONFIGXDIR

  for src 
  do
    tsrc=$CONFIGXDIR/$src

    #
    # Make the empty file
    #
    /bin/rm -f $tsrc
    /bin/touch $tsrc
  done

  return 0
}

#
# delblk <filename> <start block delimiter> <end block delimiter>
#
# Remove all text in <filename> between <start block delimiter> and
# <end block delimiter>, inclusive.
#
delblk() {
	awk -f - $1 <<-ENDAWKBLK
		BEGIN { DOPRINT = 1 }
		/$2/ { DOPRINT = 0 }
		{ if ( DOPRINT == 1 ) { print } }
		/$3/ { DOPRINT = 1 }
	ENDAWKBLK
}

cd $DMCONFIGDIR

# Strip old SUNWut block, if any, and add new one
delblk xdm-config \
	 "^! SUNWut start" \
	 "^! SUNWut end" > xdm-config.$$
cat >> xdm-config.$$ <<-ENDCONFIG
	! SUNWut start
	# WARNING: Do not edit anything in this block ending with "SUNWut end"
	DisplayManager.*.setup: $DMCONFIGDIR/Xsetup
	DisplayManager.*.startup: $DMCONFIGDIR/Xstartup
	DisplayManager.*.reset: $DMCONFIGDIR/Xreset
	DisplayManager.*.authorize:     true
	DisplayManager.servers:         $CONFIGXDIR/Xservers
	! SUNWut end
ENDCONFIG
rm -f xdm-config.bak
ln xdm-config xdm-config.bak
mv -f xdm-config.$$ xdm-config

cleanup Xconfig Xservers
cat > $CONFIGXDIR/Xconfig <<ENDXCONFIG
#
# Note that Xconfig is not really used by xdm, but is
# currently utilized by the SRSS xinitrc scripts to create an environment
# for the user session.  This implies that the environment is not created
# as early as is done for dtlogin.  If necessary this file could also be
# used in Xsetup to create the environment for the greeter context.

ENDXCONFIG

if [ -f Xservers ]; then
    /bin/cp Xservers $CONFIGXDIR/Xservers
fi

exit 0
