#!/bin/sh
#
# Xstartup
#
# This program is run as root after the user is verified
#

xset fp+ /p/lib/X11/fonts	# Local fonts
xrdb -merge /p/etc/xdm/xlogin-resources

shell=`/bin/awk -F: '
	BEGIN { shell="NOSHELL_FOOBAR" }
	$1 == "'"$USER"'" { shell=$7 }
	END { print shell}' </etc/passwd`
#echo $shell
/bin/grep -s '^'"$shell"'$' /etc/shells
case $? in
	0) ;;		# Ok, shell is in /etc/shells
	1) if [ -f $shell ] # Argh, shell not in there
	   then
		rm -f /tmp/xmotd$$
		su nobody -c $shell </dev/null >/tmp/xmotd$$ 2>&1

		# If the output from the command is empty, generate a
		# stupid message. Perhaps we should bypass xlogin in this
		# case?

                if [ ! -s /tmp/xmotd$$ ]
		then
		  echo "(No output from $shell)" >/tmp/xmotd$$
		fi

		# Display the command output to the user.

		XNOLOGINFILE=/tmp/xmotd$$ /p/etc/xdm/xlogin -showMOTD Always
		/p/etc/xdm/xlogin -logout   # remove user from utmp
		rm -f /tmp/xmotd$$
	   fi
 	   exit 1;	# return 1 here -- xdm does not continue
	   ;;
	2) exit 1;;	# Some other error
	*) exit 3;;	# Fatal
esac

# OK fall through

# Xlogins resources are in Xresources
if [ -f /etc/nologin ]; then
	/p/etc/xdm/xlogin # no -logout needed, xlogin knows about /etc/nologin
	exit 1
else
	if [ "X$USER" != "X" ]; then
		chown $USER /dev/console
		chown $USER /tmp/.X11-unix /tmp/.X11-unix/*
		chmod 700 /tmp/.X11-unix /tmp/.X11-unix/*
	fi
	/p/etc/xdm/xlogin </dev/null >/dev/null 2>&1 & # Background
fi
				# Put user in /etc/utmp (see Xreset)
#/usr/sony/lib/setutmp console "$USER" 'Niksula U405[AB]'
#sessreg -a -l $DISPLAY -h "Niksula U405[AB]" -x /p/etc/xdm/Xservers $USER
#sessreg -a -l null -h "Niksula local" -x /p/etc/xdm/Xservers $USER
#cat </dev/null			# Update time on /dev/null 

exit 0		# Fatal, oops, normal
