#!/usr/bin/env python
#  This is an official linux-mandrake extension of the X windowing system for
#  the benefit of Declan Moriarty  Copyright 2001 by civileme@mandrakesoft all
#  rights reserved.  Use is hereby granted under the GNU General Publoic
#  License version 2 or any future version of said license at the user's option
#
#  No Warranty of course.
#  This program is designed for those who want to work in concole and   
#  occasionally boot into window managers or who want to avoid theme
#  persistence from one WM to another.
#
#
import sys
import os
import popen2
import string
def complain():
    print
    print 'What do you expect me to do with that?'
    print
    sys.exit(0)
    return
print
print '                  WELCOME to Sessions Selector'
print
fspc=os.path.expanduser('~')+'/'
io=popen2.Popen3('ls /etc/X11/wmsession.d',1)
L=io.fromchild.readlines()
Sessions={}
Sessionkeys=[]
for k in L:
    primekey=int(k[0:2])
    name=k[2:-1]
    j='cat /etc/X11/wmsession.d/'+k[:-1]
    declan=popen2.Popen3(j,1)
    M=declan.fromchild.readlines()
    for cc in M:
        if string.find(cc,'EXEC=') > -1:
            command=cc[5:-1]
            break
    Sessions[primekey]=(name, command)
Sessionkeys=Sessions.keys()
Sessionkeys.sort()
for j in Sessionkeys:
    if os.system('[ -x '+Sessions[j][1]+' ]') == 0:
        print j, Sessions[j][0]
    pass
print
sys.stdin.flush()
s=raw_input('Which do you choose? (number)')
if s == '0':
    print
    print 'You have selected X with no WM'
    print
    sys.stdin.flush()
    s=raw_input('Type another 0 to confirm')
    if s == '0':
        os.execvp('xinit',('xinit','/usr/bin/xvt'))
if len(s)==0:
    s='1'
    selct=1
if len(s)>2:
    print "Be Reasonable! That number is too big."
    complain()
if len(s)==2:
    if string.find(string.digits,s[1]) == -1:
        print s[1] + " isn't a dratteded digit."
        complain()
if string.find(string.digits,s[0]) == -1:
    print s[0] + " isn't a dratted digit."
    complain()
well=int(s)
wellformed=Sessions.has_key(well)
if wellformed:
    os.execvp('startx',Sessions[well])
else:
    print
    print 'All that work and you mistyped the number'
    print 'Hmph!'
    print
    complain()




        
    
