#!/usr/bin/env python
#-----------------------------------------------------------------------------
#
# Startup script for the LSST CCD testbench at LPNHE
#
# L. Le Guillou
#-----------------------------------------------------------------------------

import sys
import os, os.path
import getopt
import subprocess
import time

#-----------------------------------------------------------------------------

import lsst.testbench.gui.wm as wm
from lsst.testbench.gui.clients import *

#-----------------------------------------------------------------------------

def usage():
      print >>sys.stderr, "usage: bench-start [elements [elements [...]]]"

def help():
      print >>sys.stderr, "usage: bench-start [elements [elements [...]]]"
      print ""
      print "Options:"
      print "     -h, --help             give this help message."
      print ""
      print "LSST LPNHE bench start script. This will start the various"
      print "sub-systems control clients requested."
      print ""

#-----------------------------------------------------------------------------

def launch(subs):
      for sub in subs:
            print "launching %s ..." % sub
            # Does this element exists?
            if not(clients.has_key(sub)):
                  print >>sys.stderr, (
                        "warning: client/subsystem %s is unknown. " +
                        "It will be ignored." ) % sub
                  continue
            
            client = clients[sub]

            # Is it a list of elements/subsystems?
            if isinstance(client, list):
                  # run all the sub elements
                  launch(client)

            # otherwise it is a dictionary
            if isinstance(client, dict):
                  # run the element
                  cmd    = client['commandline']
                  cmd_parts = cmd.split()
                  program = cmd_parts[0]
                  arguments = cmd_parts[1:]
                  screen = client.get('screen', None)
                  screen_id = None
                  if screen!=None: 
                        screen_id = screens[screen]['desktop']
                  position = client.get('position', None)

                  print cmd, screen_id, position

                  wm.launch(program,
                            arguments,
                            desktop = screen_id,
                            position = position)


#-----------------------------------------------------------------------------
# Parse command-line

try:
      opts, args = \
                   getopt.getopt(sys.argv[1:], "h", \
                                 ["help"])
except getopt.GetoptError:
      # print help information and exit
      usage()
      sys.exit(1)
        
# print opts
# print args

for option, arg in opts:
      if option in ("-h", "--help"):
            help()
            sys.exit(0)

if len(args) <= 0:
      usage()
      sys.exit(1)

subsystems = args[:]

#-----------------------------------------------------------------------------
# First Create the screens

n_screens = len(screens)
wm.setndesktop(n_screens)

scriptdir = os.path.dirname(os.path.realpath(__file__))

for s_name, s_properties in screens.iteritems():
      desktop = s_properties['desktop']
      if s_properties.has_key('background'):
            bgfile = os.path.join(scriptdir, 
                                  "..", "share", "images", 
                                  s_properties['background']) # path
            print "setting background of desktop %d with file %s" % (desktop, bgfile)
            wm.setbackground(desktop, bgfile)

#-----------------------------------------------------------------------------
# Then launch the requested clients (recursively)

launch(subsystems)

#-----------------------------------------------------------------------------
                  
