#!/bin/sh
#
# ident "@(#)gdm-reset-dpy.sh	1.11	11/01/26 Oracle"
#
# Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
#

export PATH=/usr/bin:/usr/sbin:/opt/gnome/bin:/bin:/sbin

UTLIB=/opt/SUNWut/lib
UTWHICH=$UTLIB/utwhich
GDM_DIR="`$UTLIB/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=$($UTWHICH gdm-binary)
if [ -z $which_gdm ]; then
        which_gdm=$($UTWHICH gdm)
        if [ -z $which_gdm ]; then
                echo "gdm/gdm-binary not found" 1>&2
                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 {
  mkdir -p $CONFIGXDIR

  for src 
  do
    tsrc=$CONFIGXDIR/$src

    #
    # Make the empty file
    #
    rm -f $tsrc
    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 -f "$PRESESSION_DIR/:$dpy"
            rm -f "$POSTSESSION_DIR/:$dpy"
            rm -f "$POSTLOGIN_DIR/:$dpy"
            rm -f "$INIT_DIR/:$dpy"
            gdmdynamic -d $dpy >/dev/null
          fi
	done
fi

exit 0
