#!/usr/bin/python

import ri_widget
import ri_tk
import ri_data

import sys
import getopt
from xml.dom import minidom
import os

def print_usages():
    print 'Usages: %s [-b|--begin step_name] [-l|--language language] [-t|--interface interface_xml_file] [-i|--install install_xml_file] [-c|--config config_xml_file]' %sys.argv[0]

try:
    opts, args = getopt.getopt(sys.argv[1:], "b:l:t:i:c:", ["begin=", "language=", "interface=", "install=", "config="])
except getopt.GetoptError:
    print_usages()
    sys.exit(1)

begin_step=None
itf_xml = "../xml/interface.xml"
for opt, arg in opts:
    if opt in ('-b', '--begin'):
        begin_step = arg
    elif opt in ('-l', '--language'):
        ri_tk.language = arg.lower()
    elif opt in ('-t', '--interface'):
        itf_xml = arg
    elif opt in ('-i', '--install'):
        ri_data.install_xml = arg
    elif opt in ('-c', '--config'):
        ri_data.config_xml = arg

if os.path.isfile(itf_xml):
    xmldoc = minidom.parse(itf_xml)
else:
    print 'error: interface xml(%s) does not exist' %itf_xml
    sys.exit(1)

if os.path.isfile(ri_data.install_xml):
    ri_data.init_from_xml_and_os()
else:
    if not os.path.isdir("/var/install"):
        os.mkdir("/var/install")
    ri_data.init()

ri_widget.construct(xmldoc.firstChild)

base_widget_name = xmldoc.firstChild.attributes["base_widget"].value
ri_widget.init_display(base_widget_name)

main_sequence_name = xmldoc.firstChild.attributes["sequence"].value
ri_widget.Sequence.set_current_sequence(main_sequence_name)
main_sequence = ri_widget.Sequence.dict[main_sequence_name]

if begin_step is None:
    begin_step = main_sequence.steps[0]
else:
    main_sequence.set_current_step(begin_step)
    
ri_widget.Widget.dict[begin_step].show()

ri_widget.mainloop()
