#!/usr/bin/env python
'''
    Buildit - (C) 2011-2013, Mike Miller
    A simple build tool for small projects.  License: GPL3+

    %prog [options] [targets]
'''
import sys, os
from optparse import OptionParser
from buildit import __version__, main
from buildit.utils import bold

_def_filename       = 'buildit.ini'


def setup():
    'Parse the command line and check for build file.'
    parser = OptionParser(usage=__doc__.rstrip(), version=__version__,
                          add_help_option=False)
    parser.add_option('-c', '--continue-block', action='store_true',
        help='Continue through instruction block despite errors.')
    parser.add_option('-C', '--continue-targets', action='store_true',
        help='Continue to other targets despite errors.')
    parser.add_option('-f', '--filename', metavar='F', default=_def_filename,
        help='File containing build instructions, default: "%default"')
    parser.add_option('-h', '--help', action='store_true',
        help='show this help message and exit.')
    parser.add_option('-s', '--skip-validation', action='store_true',
      help='Skip up-front validation checks for improved startup performance.')
    parser.add_option('-v', '--verbose', action='store_true',
        help='Enable verbose output of the build process.')

    (opts, args) = parser.parse_args()

    if opts.help:
        parser.print_help()
        print

    if not os.access(opts.filename, os.R_OK):
        if not opts.help:
            parser.print_help(); print
        print '%s Cannot open "%s".\n' % (bold('Error:'), opts.filename)
        sys.exit(os.EX_NOINPUT)

    return args, opts


if __name__ == '__main__':
    sys.exit( main(*setup()) )
