#!python
# encoding: utf-8
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
from rapthor._version import __version__ as version
from rapthor import process, modifystate
from rapthor.lib.parset import parset_read
import logging
import optparse
import sys


if __name__ == '__main__':
    parser = optparse.OptionParser(usage='%prog <parset>',
            version='%%prog v%s' % (version))
    parser.add_option('-q', help='enable quiet mode', action='store_true', default=False)
    parser.add_option('-v', help='enable verbose mode', action='store_true', default=False)
    parser.add_option('-r', help='reset one or more operations', action='store_true', default=False)
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.print_help()
        sys.exit()

    parset_file = args[0]

    # Prepare logger
    if options.q:
        logging_level = 'warning'
    elif options.v:
        logging_level = 'debug'
    else:
        logging_level = 'info'

    if options.r:
        # Run state modification tool
        modifystate.run(parset_file)
    else:
        try:
            parset = parset_read(parset_file, use_log_file=False)
        except KeyError as e:
            log = logging.getLogger('rapthor:parset')
            log.critical('The parset is missing the required parameter %s', e)
            sys.exit(-1)
        except Exception as e:
            log = logging.getLogger('rapthor:parset')
            log.critical('Failed to parse parset:\n%s', e)
            sys.exit(-1)
        try:
            # Run the processing
            process.run(parset_file, logging_level=logging_level)
        except Exception as e:
            log = logging.getLogger('rapthor')
            if parset['cluster_specific']['debug_workflow']:
                log.exception(str(e))
            else:
                log.critical(str(e))
