#!/usr/bin/env python

"""
Generates the full chip DAG for one exposure
"""
import sys
from ipp.ChipManager import ChipManager as ChipManager
from ipp.exceptions.IppException import IppException

#################################################################
if __name__ == '__main__':
    if len(sys.argv) != 4 and len(sys.argv) != 5:
        print len(sys.argv)
        print 'generate_chip_jobs.py <stage> <label> <exposure_name> [<email recipient>]'
        print '  e.g.: generate_chip_jobs.py A010 MD08.jtrp o5806g0078o me@nospam.com'
        print '  Acceptable value for <stage>: DAG, A020, A030, A040, or A050'
        print '  Default for e-mail recipient is schastel@ifa.hawaii.edu'
        sys.exit(1)
    STAGE = sys.argv[1]
    LABEL = sys.argv[2]
    EXPOSURE_NAME = sys.argv[3]
    if len(sys.argv) == 5:
        RECIPIENT = sys.argv[4]
    else:
        RECIPIENT = 'schastel@ifa.hawaii.edu'
    CHIP_MANAGER = ChipManager(LABEL, EXPOSURE_NAME, 
                               RECIPIENT)
    try:
        if STAGE == 'A010':
            CHIP_MANAGER.build_new_chips_jobs()
        elif STAGE == 'A020' or STAGE == 'A030' or STAGE == 'A040':
            CHIP_MANAGER.build_chip_revert_jobs(STAGE)
        elif STAGE == 'A050':
            CHIP_MANAGER.advance_chips()
        elif STAGE == 'DAG':
            DAG_NAME = CHIP_MANAGER.build_chip_dag()
            CHIP_MANAGER.build_new_chips_jobs()
            print 
            print 'Now run: condor_submit_dag %s\n' % DAG_NAME
        else:
            raise IppException('Stage [%s] not supported' % STAGE)
    except IppException, e:
        print 'IppException caught: ', e
        print 'Aborting'
        sys.exit(1)
    sys.exit(0)
