#!/usr/bin/python -tt
# by skvidal
# gplv2+


import sys
import os
import func.overlord.client
from func.overlord.scripts import base_func_parser, handle_base_func_options, errorprint
from func.utils import is_error


def main(args):
    parser = base_func_parser(outputpath=False)
    parser.add_option('--returncodes', action='store_true', help="prefix each line with the commands returncode")
    parser.add_option('-1', '--oneline', action='store_true', help="output all things as one line - to make grepping easier, will not remove \\n's from output of commands, though")
    parser.add_option('-o', '--output-to-dir', dest='output_dest', default=None,
          help="output each hosts results to a file in a dir named for the host")    
    opts, args = parser.parse_args(args)
    opts = handle_base_func_options(parser, opts)

    if len(args) < 1:
        errorprint(parser.format_help())
        return 1

    mycmd = ' '.join(args)

    hosts ='*'
    if opts.host:
        hosts = ';'.join(opts.host)
    
    if opts.output_dest:
        if opts.output_dest[0] != '/':
            opts.output_dest = os.path.realpath(os.path.expanduser(opts.output_dest))
        if not os.path.exists(opts.output_dest):
            try:
                os.makedirs(opts.output_dest)
            except (IOError, OSError), e:
                print >> sys.stderr, "Could not make dir %s: %s" % (opts.output_dest, e)
                sys.exit(1)
        
        if not os.access(opts.output_dest, os.W_OK):
            print >> sys.stderr, "Cannot write to path %s" % opts.output_dest
            sys.exit(1)

    fc = func.overlord.client.Client(hosts, timeout=opts.timeout, nforks=opts.forks, delegate=opts.delegate)

    print mycmd
    results = fc.command.run(mycmd)
    for (hn, output) in results.items():
        if is_error(output) or output[0] == 127:
            msg = 'Error: %s: ' % hn
            for item in output[1:3]:
                if type(item) == type(''):
                    msg += ' %s' % item
            errorprint(msg)
            continue

        if opts.output_dest:
            fo = open(opts.output_dest + '/' + hn+'.output', 'w')
            fo.write(mycmd + '\n')
            if opts.returncodes:
                fo.write('%s:\nreturn code:%s\n%s\n' % (hn, output[0], ' '.join(output[1:])))
            else:
                fo.write('%s:\n%s\n' % (hn,' '.join(output[1:2])))
            fo.close()
            continue

        if opts.oneline:
            if opts.returncodes:
                print '%s:%s:%s' % (hn, output[0], ' '.join(output[1:]))
            else:
                print '%s:%s' % (hn, output[1:2])
        else:
            if opts.returncodes:
                print '%s:\nreturn code:%s\n%s' % (hn, output[0], ' '.join(output[1:]))
            else:
                print '%s:\n%s' % (hn, ' '.join(output[1:]))

    if opts.output_dest:
        print "output written to %s" % opts.output_dest

    
if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
