#!/usr/bin/python3
from __future__ import print_function
import getopt
import os
import re
import sys
import tty, termios  ### for getch_unix()
import datetime      ### for timestamps
import time          ### for sleep
import stat          ### for os.chmod
from  UmenuConf import Conf
from  UmenuUtils import Utils
from  UmenuMenu  import Menu 
from  UmenuCommander  import Commander
from  UmenuMetadata  import Metadata




def main():
    cnf = Conf(sys.argv)
    cnf.showall()
    #sys.exit(1)
    os.chdir(cnf.cnf['prog'])
    metadata = Metadata(cnf.cnf['icons'])

    ltrstring = cnf.cnf['rootmnu']
    done = False
    while not done:
        Utils.pagefeed()
        mnu = Menu(cnf.cnf['menu'], ltrstring, metadata, cnf)
        mnu.render()
        ltr = mnu.get_user_input()
        mnu.handle_letter(ltr)
        nextype = mnu.next['type']
        if nextype == '=':
            ltrstring = cnf.cnf['rootmnu']
        if nextype == ';':                     ### redraw
            ltrstring = ltrstring
        elif nextype == 'm':
            ltrstring += ltr
        elif nextype == 'r':
            if len(ltrstring) > len(cnf.cnf['rootmnu']):
                ltrstring = ltrstring[:-1]
            else:
                done = True
        elif nextype == 'c':
            cmdr = Commander(mnu.next)    ### Prompted arg substitution happens in here.
            cmdr.import_conf_pieces(cnf)
            cmdr.run_script()
            if cnf.cnf['term']:
                time.sleep(1)
                done = True
    print('')

if __name__ == '__main__':
    main()

