#!/usr/bin/env python
#----------------------------------#
# -- Author: V. Garonne
# -- Mail: garonne@lal.in2p3.fr
# -- Date: 08/25/2006
# -- Name: tbroadcast
# -- Description: main program
#----------------------------------#

import sys
import time

def usage():
    print """
Usage: tbroadcast [global options] [<command>]
#
# <command> is executed in <package>/cmt
#
# global options :
#   -help                 : Print help
#   -local                : Reach packages only within the current project
#                         : if not specified reach packages in all CMTPATH/CMTPROJECTPATH items
#   -ignore[_cycles]      : Suppress automatically the cycles
#   -sort                 : Compile packages in order of use count, most significant first
#   -nb=<num_worker>      : Change the total number of threads[default is 20]
#   -output=<location>    : Output directory to store output files with the form <package>_output.log
#   -error=<location>     : Output directory to store error output with the form <package>_error.log
#   -perf=<file>          : Store for each package the time for executing the command in the <file> file
#   -make=<file>          : Generate a recursive Make, [see: http://www.tip.net.au/~millerp/rmch/recu-make-cons-harm.html]
#   -print                : Print dependencies for each package and exit
#   -version              : Print version of tbroadcast and exit
#   -test                 : Simulate execution and exit
#   -                     : is accepted and does nothing
#
# Example:
#   tbroadcast -local -ignore -nb=4 'make -j6'
"""

# Unused options
#   -f=<file>             : Input file (option for debug only)
#   -no_keep_going        : Exit after the first exit code > 1 found and return it in the shell
#   -silent               : Disable print

if __name__ == '__main__':
    # check python version
    req_version = (2,5)
    cur_version = sys.version_info

    if not (cur_version[0] > req_version[0] or (cur_version[0] == req_version[0] and cur_version[1] >= req_version[1])):
        print "tbroadcast: must use python 2.5 or greater"
        sys.exit(-1)

    from tbroadcast import Scheduler

    # Default options
    num_worker    = 20
    command       = ''
    version       = 'v2.0.9'
    test          = False
    print_graph   = False
    local         = False
    ignore_cycles = False
    silent        = False
    perf          = False
    sort          = False
    output        = None
    error         = None
    file          = None
    make          = None
    makefile      = 'Makefile'
    keep_going    = True

    if len(sys.argv) == 1:
        usage()
        sys.exit(-1)
    else:
        for arg in sys.argv[1:len(sys.argv)]:
#            print "Argument is",arg
             if arg[0]=='-':
                 option = arg.split('=')[0]
                 if option == '-version':
                     print version
                     sys.exit(-1)
                 elif option == '-nb':
                     num_worker = int (arg.split('=')[1])
                 elif option == '-f':
                     file = arg.split('=')[1]
                 elif option == '-perf':
                     perf = arg.split('=')[1]
                 elif option == '-output':
                     output = arg.split('=')[1]
                 elif option == '-error':
                     error = arg.split('=')[1]
                 elif option == '-local':
                     local = True
                 elif option == '-sort':
                     sort = True
                 elif option[:7] == '-ignore':
                     ignore_cycles = True
                 elif option == '-silent':
                     silent = True
                 elif option == '-no_keep_going':
                     keep_going = False
                 elif option == '-help':
                     usage()
                     sys.exit(-1)
                 elif option == '-test':
                     test = True
                 elif option == '-print':
                     print_graph = True
                 elif option == '-make':
                     make     = True
                     makefile = arg.split('=')[1]
                 elif option == '-':
                     pass
                 else:
                     print 'tbroadcast: bad option "%s", use -help for help' % option
                     sys.exit(-1)
             else:
                 command = arg

#   print "End of arguments. Command to execute", command

    if not (command or test or print_graph):
        print 'tbroadcast: no command specified'
        sys.exit(-1)

    master = Scheduler (num_workers=num_worker, file=file, ignore_cycles=ignore_cycles,
                        local=local, output=output, error=error, silent=silent, perf=perf,
                        keep_going=keep_going, sort=sort)
    if test:
        master.simulate_execution()
    elif print_graph:
        master.print_dependencies()
    elif make:
        master.generate_make (makefile, command)
    else:
        print 'tbroadcast: start of job at', time.strftime('%d-%b-%Y %T')
        master.execute_all (command)
        print 'tbroadcast: end of job at', time.strftime('%d-%b-%Y %T')
    #sys.exit(-1)
#--------- EoF --------#
