#!/bin/bash


# kdmflexiserver -- an alternative for gdmflexiserver for KDM
# KDE, when started from KDM, allows users to start new KDM sessions or
# swith to other sessions using the "Switch User" menu item in the K menu.
#
# However, users of desktop environments other than KDE do not have an easy
# access to this KDM functionality. This script presents the user with a 
# list of runing sessions and an option to start a new session. kdmctl
# is then called to switch to the selected session.
#
# (C) 2008 Jiri Bohac <jbohac@jikos.cz>
# Licensed under the GPL, version 2 or later


SESSIONS=`kdmctl list alllocal`|| {
	echo "cannot get the session list"
	exit 1
}
SESSIONS=`echo $SESSIONS|sed "s/^ok/,New-Session,/"`

for i in $SESSIONS; do
	NAME=`echo $i|cut -f3 -d,`
	VT=`echo $i|cut -f2 -d,`
	CMD="$CMD \"$VT\" \"$NAME\""
done

ITEM=`eval Xdialog --stdout --menu \"Switch to a running session or start a new one\" 0 0 10 $CMD` || {
	echo "Cancelled"
	exit 1
}

if [[ $ITEM == New-Session ]]; then
	kdmctl reserve
else
	echo Switching to: $ITEM

	kdmctl activate $ITEM
fi

exit
