#!/usr/bin/env python

import sys
import os.path

# Make sure that the skel modules are available...
# ...for the installed skel
sys.path.insert (0, os.path.dirname(sys.argv[0]) + '/../lib/python')

# ...for the skel in the utils dir of the adios source
sys.path.insert (0, os.path.dirname(sys.argv[0]) + '/../lib')

import argparse

import adios
import skelconf
import skel_settings
import skel_install
import skel_makefile
import skel_params
import skel_replay
import skel_source
import skel_submit
import skel_suite
import skel_template
import skel_xml

def get_parser():

    parser = argparse.ArgumentParser (
        formatter_class=argparse.RawDescriptionHelpFormatter,
        prog='skel',
        add_help=False,
        description='''\
Skel: Create, run and analyze skeletal I/O applications
-------------------------------------------------------
Available subcommands include:
    skel help
    skel makefile
    skel params
    skel replay
    skel source
    skel submit
    skel xml
        
    Use skel help <subcommand> to see detailed options for each subcommand.''')
    parser.add_argument ('subcommand', metavar='subcommand', help='Skel command to execute')
    #parser.add_argument ('project', metavar='project', help='Name of the skel project')
    #parser.add_argument ('-g', '--group', help='adios group (only valid for skel params)')

    return parser


def main(argv=None):

    skel_settings.create_settings_dir_if_needed()
    parser = get_parser()

    # Look for skel help (or no args), and print the list of subcommands...
    if (len(sys.argv) == 1) or (sys.argv[1] == 'help' and len(sys.argv) == 2):
        print parser.description 
        return 0

    # install does not require a project, so let's just check for it directly
    if sys.argv[1] == 'install':
        return 0

    if sys.argv[1] == 'help':
        if sys.argv[2] == 'makefile':
            sys.argv = ["skel", "makefile", "junk", "-h"]
            skel_makefile.generate_makefiles_with_args (parser)
        elif sys.argv[2] == 'params':
            sys.argv = ["skel", "params", "junk", "-h"]
            skel_params.generate_param_file_with_args (parser)
        elif sys.argv[2] == 'replay':
            sys.argv = ["skel", "replay", "junk", "-h"]
        elif sys.argv[2] == 'source':
            sys.argv = ["skel", "source", "junk", "-h"]
            skel_source.create_sources_with_args (parser)
        elif sys.argv[2] == 'submit':
            sys.argv = ["skel", "submit", "junk", "-h"]
            skel_submit.generate_submit_scripts_with_args (parser)
        elif sys.argv[2] == 'xml':
            sys.argv = ["skel", "xml", "junk", "-h"]
        else:
            print "Unknown help topic" 
            print parser.description 
            exit(1)

    #args = parse_command_line()
    args, unknown = parser.parse_known_args()

    # ignore .xml suffix if used (tab completion)
    #if args.project and args.project.endswith (".xml"):
    #    args.project = args.project[:-4]

    if args.subcommand == 'install':
        return 0

    if args.subcommand == 'template':
        skel_template.fill_template (parser)
        return 0

    if args.subcommand == 'xml':
        skel_xml.create_skel_xml (parser)
        return 0

    if args.subcommand == 'replay':
        skel_replay.do_replay_with_args (parser)
        return 0

    if args.subcommand == 'suite':
        skel_suite.gen_suite_with_args (parser)
        return 0


    # Decided to push this further down rather than deal with increasingly
    # complicated landscape of which subcommands do or don't require
    # a project or an xml file.
    #try:
    #    config = adios.adiosConfig (args.project + '_skel.xml')
    #except (IOError):
    #    print "XXError reading " + args.project + "_skel.xml. Try running skel xml " + args.project + " first."
    #    return 1

    if args.subcommand == 'params':
        #outfilename = args.project + '_params.xml'

        # Only proceed if outfilename does not already exist.
        #if os.path.exists (outfilename):
            #print "%s exists, aborting. Delete the file or use -f to overwrite." % outfilename
            #return 999

        #skel_params.generate_param_file (args.project, outfilename, config, args.group)
        skel_params.generate_param_file_with_args (parser)
        return 0
    
    
    if args.subcommand == 'source':
        #skel_source.create_sources (params, config, args.project)
        skel_source.create_sources_with_args (parser)
        return 0


    if args.subcommand == 'makefile':
        #skel_makefile.generate_makefiles (params, config)
        skel_makefile.generate_makefiles_with_args (parser)
        return 0


    if args.subcommand == 'submit':
        #skel_submit.generate_submit_scripts (params)
        skel_submit.generate_submit_scripts_with_args (parser)
        return 0

    # Unrecognized subcommand, print the help message
    print parser.description
    return 0


if __name__ == "__main__":
    main()



