#!/bin/ksh -p
#
# ident "@(#)gdm-reset-dpy.sh	1.9 08/06/20"
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms
#
# Script to reinitialize empty state for GDM
# Pass SRSS config directory as first arg (e.g. reset-dpy /tmp/SUNWut/config)
#
export PATH=/usr/bin:/usr/sbin:/opt/gnome/bin:/bin:/sbin

GDM_DIR="`/opt/SUNWut/lib/utgdmconfigpath`"

PRESESSION_DIR=${GDM_DIR}PreSession
POSTSESSION_DIR=${GDM_DIR}PostSession
POSTLOGIN_DIR=${GDM_DIR}PostLogin
INIT_DIR=${GDM_DIR}Init

CONFIGDIR=$1
CONFIGXDIR=$CONFIGDIR/xconfig
DMNAME=gdm-binary
which_gdm=$(whence gdm-binary)
if [ -z $which_gdm ]; then
        which_gdm=$(whence gdm)
        if [ -z $which_gdm ]; then
                print -u2 "gdm/gdm-binary not found"
                exit 1
        fi
	DMNAME=gdm
fi

# Note - we start MINDISP at 11 using gdm for linux. It gets around bug
# 5057552, which is actually caused by lax privileges defined by 
# /etc/security/console.perms
MINDISP=11              # minimum display number XXX
MAXDISP=1000            # maximum display number XXX -> must match OWconfig

function 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
}

cleanup Xconfig Xservers

#
# if gdm is running cleanup displays
#

pgrep -P 1 -u 0 $DMNAME >/dev/null
if [[ $? -eq 0 ]]
then
	dpys=`gdmdynamic -l | /bin/sed -e 's/:\([^,]*\),[^;]*/\1/g' -e 's/;/ /g' `
	for dpy in $dpys
	do
          if [[ $dpy -ge $MINDISP && $dpy -le $MAXDISP ]]; then
	    rm "$PRESESSION_DIR/:$dpy"
            rm "$POSTSESSION_DIR/:$dpy"
            rm "$POSTLOGIN_DIR/:$dpy"
            rm  "$INIT_DIR/:$dpy"
            gdmdynamic -d $dpy >/dev/null
          fi
	done
fi

exit 0
