#!/bin/sh
#
# Copyright 1988-1997 Network Computing Devices, Inc.  All rights reserved.
# An unpublished work.
#
# $NCDId$
#
#
# Description:
#	This script is to be used when using ncddm in conjunction with
#	xdm.
#
#	The command "ncdloguser login" should be inserted in the xdm
#	Xstartup script.  This will create a log file that associates 
#	the display with the user who just logged in.
#
#	The command "ncdloguser logout" should be inserted in the xdm
#	Xreset script.  This will remove the file created by the
#	"ncdloguser login" command.
#
# Usage: ncdloguser  [ -logdir log_directory ]  login | logout
#
#

iam=$0
logdir=/usr/lib/X11/ncd/xdm

#
# parse arguments
#
while [ $# -gt 0 ]
do
    case "$1" in
	-logdir)
		logdir=$2
		shift 2
		;;

	login|logout)
		cmd=$1
		shift
		;;

	*)
		echo "usage: $iam  [ -logdir log_directory ]  login | logout" >&2
		exit 1
    esac
done

client=`echo $DISPLAY | sed 's/:.*//'`
file=$logdir/$client

case "$cmd" in 
    login)
	if [ ! -d $logdir ]
	then
	    mkdir -p $logdir
	    if [ $? != 0 ]
	    then
		echo $iam: unable to create directory $logdir >&2
		exit 1
	    fi
	    chmod 755 $logdir
	fi
	printenv >$file
	chmod 644 $file
	;;

    logout)
	if [ ! -d $logdir ]
	then
	    echo $iam: unable to find directory $logdir >&2
	    exit 1
	fi
	rm -f $file
	if [ $? != 0 ]
	then
	    echo $iam: unable to remove user log file $file >&2
	    exit 1
	fi
	;;

    *)
	echo "usage: $iam  [ -logdir log_directory ]  login | logout"
	exit 1
esac

exit 0
