#!/usr/bin/env python
import sys
import os
import optparse
import mpslogic




def handle_options():
    usage   = """Usage: %prog FILE1 EXPORT_TYPE...|FILE2 [options]
    
FILE1                   Machine Protection Logic (MPL) file
FILE2                   MPL file to be compared

EXPORT_TYPE:
Specify one or more of the following export types

  -c, --ccode           MPS logic as C code
  -l, --latex           LaTeX documentation
  -d, --dump            Dump database to text file
"""
                
    version = '%prog 0.1'
    parser = optparse.OptionParser(usage=usage, version=version)

    parser.add_option('-v', '--verbose',           action='store_true',  dest='verbose',            help='print verbose output')

    parser.add_option('-c', '--ccode',   action='store_true',  dest='export_code',   help=optparse.SUPPRESS_HELP)
    parser.add_option('-l', '--latex',   action='store_true',  dest='export_latex',  help=optparse.SUPPRESS_HELP)
    parser.add_option('-d', '--dump',    action='store_true',  dest='export_dump',   help=optparse.SUPPRESS_HELP)
    
    group = optparse.OptionGroup(parser, 'File Export Paths', 'Use the following options to set destinations of exported files.')
    group.add_option('-C', '--code_path',   action='store',  type='string',  dest='code_path',   metavar='FILE',  help='Code file path')
    group.add_option('-L', '--latex_path',  action='store',  type='string',  dest='latex_path',  metavar='FILE',  help='LaTeX file path')
    group.add_option('-D', '--dump_path',   action='store',  type='string',  dest='dump_path',   metavar='FILE',  help='Dump file path')
    group.add_option('-F', '--diff_path',   action='store',  type='string',  dest='diff_path',   metavar='FILE',  help='Differences file path')
    parser.add_option_group(group)

    group = optparse.OptionGroup(parser, 'LaTeX Options', 'Use the following options to set LaTeX documentation properties')
    group.add_option('-r', '--latex_rev',   action='store',  type='string',  dest='latex_rev',   metavar='REV',   help='LaTeX document revision')
    parser.add_option_group(group)

    # Defaults
    parser.set_defaults(verbose      = False,
                        export_code  = False,
                        export_dump  = False,
                        export_latex = False,
                        export_diff  = False,
                        latex_rev    = '--')

    # Parse arguments
    (options, args) = parser.parse_args()
    
    if len(args) == 2:
        options.export_diff = True
    else:
        if len(args) != 1:
            parser.error('incorrect number of arguments')

    if not (options.export_code or options.export_dump or options.export_latex or options.export_diff):
        parser.error('must specify at least one of -c, -d, -l, or another file')

    # Paths
    options.dbfile = os.path.normcase(args[0])
    dbbase, dbext  = os.path.splitext(options.dbfile)

    if len(args) == 2:
        options.dbfile2 = os.path.normcase(args[1])
        dbbase2, dbext2 = os.path.splitext(options.dbfile2)

    # Check if the dbfile exists
    if os.path.isfile(options.dbfile) == False:
        sys.stderr.write('Error: database "%s" does not exist\n' % options.dbfile)
        return 1

    options.code_path   = os.path.normcase( options.code_path  or dbbase + '.c'          )
    options.latex_path  = os.path.normcase( options.latex_path or dbbase + '.tex'        )
    options.latex_path  = os.path.normcase( options.latex_path or '%s_vs_%s%s' % (dbbase, dbbase2, '.tex'   ))
    options.dump_path   = os.path.normcase( options.dump_path  or dbbase + '.dump.txt'   )

    return (options, args)






def main():
    (options, args) = handle_options()

    logic = mpslogic.MpsLogic()
    logic.filename = options.dbfile

    if (options.export_code or options.export_latex):
        logic.init_with_sqlite3_file(options.dbfile) 

    if options.export_code:   logic.export_c_code(options.code_path, options.verbose)
    if options.export_latex:  logic.export_latex(options.latex_path, options.latex_rev, options.verbose)
    if options.export_dump:   logic.export_dump(options.dump_path, options.verbose)
    
    

if __name__ == '__main__':
    main()

