#!/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)
#

XDIR=/etc/dt/config
UDIR=/usr/dt/config
PKGID=SUNWut
TMPBASE=/tmp/$PKGID
TMPDIR=$TMPBASE/config
TMPXDIR=$TMPDIR/xconfig

LASTLANGFILE=/var/dt/sdtlogin/lastlang_*

clean_prototype() {
        /bin/nawk '
                BEGIN {
			remove_lines=0
		}
                /# BEGIN SUNRAY CONFIGURATION/ {
                        remove_lines=1
			print $0
                }
                /# END SUNRAY CONFIGURATION/ {
			remove_lines=0
		}
                remove_lines == 1 {
			next
		}
                {
			print $0
		}
        ' -

}


cleanup() {
  /bin/mkdir -p $XDIR
  /bin/mkdir -p $TMPXDIR

  for src 
  do
    tsrc=$TMPXDIR/$src
    sproto=$XDIR/$src.$PKGID.prototype

    #
    # If the $XDIR file exists and is not a link, back it up to use as
    # the prototype
    #
    if [ -f $XDIR/$src -a ! -h $XDIR/$src ]
    then
      if /bin/cmp -s $XDIR/$src $UDIR/$src
      then
	# No need to retain - just a copy of /usr/dt
        :
      else
	/bin/mv -f $XDIR/$src $sproto
      fi
    fi

    #
    # Make the link
    #
    /bin/rm -f $XDIR/$src
    /bin/ln -s $tsrc $XDIR/$src

    #
    # Use the prototype if it exists, or get a vanilla one from
    # /usr/dt/config
    #
    if [ -f $sproto ]
    then
      sf=$sproto
    else
      sf=$UDIR/$src
    fi

    if [ "$src" = "Xconfig" ]
    then
      #
      # Make sure we have the correct values for what we care about.
      # The class ID 'SunRay' must match the line that is dynamically
      # edited into the Xservers file to launch a Sun Ray X server.
      #
      sed								\
	-e '/^[	 ]*Dtlogin\.pidFile:/d'					\
	-e '/^[	 ]*Dtlogin\.servers:/d'					\
	-e '/^[	 ]*Dtlogin\.SunRay\.setup:/d'				\
	-e '/^[	 ]*Dtlogin\.SunRay\.startup:/d'				\
	-e '/^[	 ]*Dtlogin\.SunRay\.reset:/d'				\
	-e '/^[	 ]*Dtlogin\.SunRay\.startAttempts:/d'			\
	-e '/^[	 ]*Dtlogin\.SunRay\.serverAttempts:/d'			\
      	$sf | clean_prototype > $tsrc.new

      echo '#########################################################################' >> $tsrc.new
      echo '#' >> $tsrc.new
      echo "# The following entries are dynamically created by Sun Ray." >> $tsrc.new
      echo "# It is both dangerous and pointless to edit these lines." >> $tsrc.new
      echo "# Any changes made here will be discarded at the next reboot." >> $tsrc.new
      echo '#' >> $tsrc.new
      echo "# Sun Ray required parameters (DO NOT EDIT):" >> $tsrc.new
      echo '#' >> $tsrc.new
      echo 'Dtlogin.pidFile:	/var/dt/Xpid' >> $tsrc.new
      echo 'Dtlogin.servers:	Xservers' >> $tsrc.new
      echo 'Dtlogin.SunRay.startup:		Xstartup' >> $tsrc.new
      echo 'Dtlogin.SunRay.setup:		Xsetup' >> $tsrc.new
      echo 'Dtlogin.SunRay.reset:		Xreset' >> $tsrc.new
      echo 'Dtlogin.SunRay.startAttempts:	1000' >> $tsrc.new
      echo 'Dtlogin.SunRay.serverAttempts:	4' >> $tsrc.new
      echo '#' >> $tsrc.new
      echo "# Sun Ray dynamically created parameters (DO NOT EDIT):" >> $tsrc.new
      echo '#' >> $tsrc.new
    else
      /bin/cat $sf | clean_prototype > $tsrc.new
    fi
  done

  #
  # Create a copy and then move into place so there is never a broken
  # or partial version installed
  #
  # Here's where the files are replaced
  #
  for src 
  do
    tsrc=$TMPXDIR/$src
    /bin/mv -f $tsrc.new $tsrc
  done

  return 0
}

/bin/rm -f $LASTLANGFILE

#
# Reinitialize all dtlogin setup (create the new Xservers last)
#
cleanup Xconfig Xservers

exit 0
