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




def handle_options():

    usage   = """Usage: %prog DATABASE EXPORT_TYPE... [options]
    
DATABASE                SQLite3 file containing MPS logic data

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")
    parser.add_option_group(group)

    # Defaults
    parser.set_defaults(verbose      = False,
                        export_code  = False,
                        export_dump  = False,
                        export_latex = False)

    # Parse arguments
    (options, args) = parser.parse_args()

    if (len(args) != 1):
        parser.error("incorrect number of arguments")

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

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

    # 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.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.verbose)
    if options.export_dump:   logic.export_dump(options.dump_path, options.verbose)
    
    

if __name__ == "__main__":
    main()

