#!/usr/bin/python
"""
    The main entry point to the Report library.
    Copyright (C) 2009 Red Hat, Inc

    Author(s): Gavin Romig-Koch <gavin@redhat.com>
               Adam Stokes <ajs@redhat.com>

    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.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

import sys
import os
from subprocess import Popen, PIPE
import report
import report.io.TextIO
import report.accountmanager
from optparse import OptionParser

def parse_options(options):
    parser = OptionParser(usage="report [opts] args")
    parser.add_option('--plugin', dest='plugin',
                      help='Select template', default=None)
    parser.add_option('--ticket', dest='ticket',
                      help='Ticket to associate FILE with', default=None)
    parser.add_option('--username', dest='username',
                      help='File to attach to ticket', default=None)
    parser.add_option('--host', dest='host',
                      help='Define a host for plugin', default=None)
    parser.add_option('--path', dest='path',
                      help='Define path for plugin', default=None)
    parser.add_option('-v','--verbose', dest='verbose',
                      help='Increase verbosity', action='count')

    cmdopts, cmdargs = parser.parse_args(options)
    if len(cmdargs) < 1:
        raise SystemExit('Needs a filename.')
    elif len(cmdargs) > 1:
        raise SystemExit('Please specify only 1 filename.')
    else:
        cmdopts.filename = os.path.abspath(cmdargs[0])

    return (cmdopts, cmdargs)

if __name__=="__main__":
    # parse cmdline options
    opts, args = parse_options(sys.argv[1:])

    io = report.io.TextIO.TextIO()

    if report.isSignatureFile(opts.filename):
        signature = report.createSignatureFromFile(opts.filename, io)

    else: 
        p = Popen(["file","-L","-b", opts.filename], stdout=PIPE,stderr=PIPE)
        out, err = p.communicate()
        isBinary = True
        if 'text' in out:
            isBinary = False

        signature = report.createSimpleFileSignature(opts.filename, isBinary)

    if not signature:
        raise SystemExit("Could not create signature")

    # convert config object into dict
    optsDict = {}
    for k,v in vars(opts).iteritems():
        if v and k in ('plugin','ticket','host','path','username'):
            optsDict[k] = v
        
    app = report.report(signature, io, **optsDict)
    raise SystemExit("Complete %s" % (app,))

# vim:ts=4 sw=4 et
