#!/bin/sh
#
# $Header: dmset,v 4.300 91/06/09 01:47:00 root Rel41 $
#
#	dmset    ---> use sxdm
#	dmset -x ---> use xdm
#	dmset -n ---> use tty
#
ttyset=/usr/sony/etc/ttyset
sxdm=/usr/bin/X11/sxdm
xdm=/usr/bin/X11/xdm
custom=/etc/rc.custom
corig=/etc/rc.custom.orig
ttys=/etc/ttys
torig=/etc/ttys.orig

case $# in
0)
	if [ ! -f $sxdm ] 
	then
		echo "$sxdm not exist."
		exit 1
	fi
	dm=sxdm
	/bin/cp -p $custom $corig
	if grep -s "^DM=" $custom
	then
		ed - $custom << EOF
g/^DM=/s/=.*/=sxdm/
w
q
EOF
	else
		echo "DM=sxdm" >> $custom
	fi
	/bin/mv -f $ttys $torig
	$ttyset -i $torig -o $ttys -dev console -off
	case $? in
	0)
		;; 
	*)	echo "$ttys not change"
		/bin/mv -f $torig $ttys
		/bin/mv -f $corig $custom
		exit 1
		;;
	esac
	;;
1)
	case $1 in
	-n)
		dm=no
		/bin/cp -p $custom $corig
		if grep -s "^DM=" $custom
		then
			ed - $custom << EOF
g/^DM=/s/=.*/=/
w
q
EOF
		else
			echo "DM=" >> $custom
		fi
		/bin/mv -f $ttys $torig
		$ttyset -i $torig -o $ttys -dev console -on
		case $? in
		0)
			;; 
		*)	echo "$ttys not change"
			/bin/mv -f $torig $ttys
			/bin/mv -f $corig $custom
			exit 1
			;;
		esac
		;;
	-x)
		if [ ! -f $xdm ]
		then
			echo "$xdm not exist."
			exit 1
		fi
		dm=xdm
		/bin/cp -p $custom $corig
		if grep -s "^DM=" $custom
		then
			ed - $custom << EOF
g/^DM=/s/=.*/=xdm/
w
q
EOF
		else
			echo "DM=xdm" >> $custom
		fi
		/bin/mv -f $ttys $torig
		$ttyset -i $torig -o $ttys -dev console -off
		case $? in
		0)
			;; 
		*)	echo "$ttys not change"
			/bin/mv -f $torig $ttys
			/bin/mv -f $corig $custom
			exit 1
			;;
		esac
		;;
	*)
		echo 'Usage: dmset [-n|-x]'
		exit 1
		;;
	esac
	;;
*)
	echo 'Usage: dmset [-n|-x]'
	exit 1
	;;
esac
chmod 444 $custom
chmod 444 $ttys

echo 'setup done'
while true
do
	echo -n 'reboot ok? (y/n) '
	read reb
	case $reb in
	y)
		/etc/fastboot
		break
		;;
	*)
		case $dm in
		sxdm|xdm)
			echo 'reboot the system manually to startup with X Window System'
			;;
		no)
			echo 'reboot the system manually to use console login'
			;;
		esac
		break
		;;
	esac
done

exit 0
