#!/bin/sh
# install xlogin
# Legacy install script.

# Check we are root, all bets off if not
if [ "$UID" -ne 0 ]; then
	echo "must install as root"
	exit 1
fi



# If the default configuration files dont exist then add them
if [ ! -d /etc/xlogin ]; then
	echo "Making /etc/xlogin directory"
	mkdir /etc/xlogin
	mkdir /etc/xlogin/desktops
	mkdir /etc/xlogin/backgrounds
	mkdir /etc/xlogin/identity

	cp ./desktops/* /etc/xlogin/desktops
	cp ./backgrounds/* /etc/xlogin/backgrounds
fi



# Install binary, rename xlogin if it exists - allows us to install even its running
if [ -f /usr/bin/xlogin ]; then
	mv /usr/bin/xlogin /usr/bin/xlogin.old
fi

if [ -f /usr/bin/xlogin-xdmcpserver ]; then
	mv /usr/bin/xlogin-xdmcpserver /usr/bin/xlogin-xdmcpserver.old
fi

cp ./xlogin /usr/bin
cp ./xlogin-makeidentity /usr/bin
cp ./xlogin-xdmcpserver /usr/bin
cp ./xlogin-rootjpeg /usr/bin
if [ -f ./xlogin-ldap ]; then
	cp ./xlogin-ldap /usr/bin
fi
rm -f /usr/bin/xlogin.old



# Install manual entries
MANEPATH="/usr/share/man/man8"
cp ./man/xlogin.8.gz $MANEPATH
cp ./man/xlogin-makeidentity.8.gz $MANEPATH
cp ./man/xlogin-rootjpeg.8.gz $MANEPATH
cp ./man/xlogin-xdmcpserver.8.gz $MANEPATH


INITHASXLOGIN=`cat /etc/inittab |grep 'xlogin'`
if [ -z "$INITHASXLOGIN" ]; then
	echo "/etc/inittab does not contain any reference to xlogin."
	echo "do you want to add the xlogin-xdmcpserver to it ? Answer Y or N"
	read ANSWER
	UPCASEANSWER=`echo $ANSWER |tr  [:lower:] [:upper:] |cut --bytes=1`
	if [ "$UPCASEANSWER" == "Y" ]; then
		echo "" >>/etc/inittab
		echo "xdm:345:respawn:/usr/bin/xlogin-xdmcpserver -ac -date -background tree -pos 10,10" >>/etc/inittab
		echo "Ok, added xdmcp server to inittab"
		kill -HUP 1
	fi

	ISRUNNING=`ps ax |grep 'xdmcp' |grep 'server'`
	if [ ! -z "$ISRUNNING" ]; then
		echo
		echo "xlogin-xdmcpserver is now running" 
		echo
	fi
fi

