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

import os
import sys
import string

from tbroadcast import Scheduler

def usage():
    print 'Usage : > tbroadcast [global options] [<command>]'
    print '# command :'
    print '#   <command>: command to execute'
    print '# global options :'
    print '#   -f=<file>             : Input file'
    print '#   -help                 : Print help'
    print '#   -local                : Reach packages only within the current project'
    print '#   -global               : Reach packages in all CMTPATH/CMTPROJECTPATH items'    
    print '#   -ignore_cycles        : Suppress automatically the cycles'
    print '#   -make=<file>          : Generate a recursive Make, [see: http://www.tip.net.au/~millerp/rmch/recu-make-cons-harm.html]'
    print '#   -nb=<num_worker>      : Change the total number of threads[default is 20]'
    print '#   -no_keep_going        : Exit after the first exit code > 1 found and return it in the shell'
    print '#   -output=<location>    : Output directory to store output files with the form <package>_output.log'
    print '#   -error=<location>     : Output directory to store error output with the form <package>_error.log'
    print '#   -perf=<file>          : Store for each package the time for executing the command in the <file> file'
    print '#   -print                : Print dependencies for each package' 
    print '#   -version              : version of tbroadcast'        
    print '#   -silent               : Disable print'     
    print '#   -test                 : Simulate execution'

if __name__ == '__main__':
    # Default options
    num_worker    = 20
    command       = ''
    version       = 'v2.0.4'
    test          = False
    check         = False
    print_graph   = False
    local         = False
    ignore_cycles = False
    silent        = False
    perf          = False     
    output        = None
    error         = None
    file          = None
    make          = None
    makefile      = 'Makefile'
    keep_going    = True
    
    if len(sys.argv) == 1:
        test = True
    else:        
        for arg in sys.argv[1:len(sys.argv)]:            
             if arg[0]=='-':
                 option = string.split(arg,'=')[0]
                 if option == '-version':
                     print version
                     sys.exit(-1)
                 if option == '-nb':
                    num_worker = int (string.split(arg,'=')[1]) 
                 if option == '-f':
                    file = string.split(arg,'=')[1]
                 if option == '-perf':
                    perf = string.split(arg,'=')[1]
                 if option == '-output':
                    output = string.split(arg,'=')[1]
                 if option == '-error':
                    error = string.split(arg,'=')[1]
                 if option == '-local':
                     local= True
                 if option == '-ignore_cycles':             
                     ignore_cycles = True
                 if option == '-silent':             
                     silent = True        
                 if option == '-no_keep_going':             
                     keep_going = False
                              
                 if option == '-help':    
                      usage()
                      sys.exit(-1)      
                                  
                 if option == '-test':
                         test = True   
                 elif option == '-check':
                         check = True   
                 elif option == '-print':
                         print_graph = True   
                 elif option == '-make':        
                     make     = True
                     makefile = string.split(arg,'=')[1]
             else:
                  command = arg

    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)
    if test:
        master.simulate_execution()
    elif check:
        master.check_execution (package=master.get_current_package())    
    elif print_graph:
        master.print_dependencies ()
    elif make:
        master.generate_make (makefile, command)    
    else:
        master.execute_all (command)
    #sys.exit(-1);
#--------- EoF --------#
