#!/usr/bin/env python

"""
Generates the complete Camera stage DAG for one exposure
"""
import sys
from ipp.CamManager import CamManager as CamManager

#################################################################
if __name__ == '__main__':
    if len(sys.argv) != 4 and len(sys.argv) != 5:
        print 'generate_cam_jobs.py <stage> <label> <exposure_name> [<email recipient>]'
        print '  e.g.: generate_cam_jobs.py B010 MD08.jtrp o5806g0078o me@nospam.com'
        print '  Acceptable value for <stage>: DAG, B010, B020, B030, B040, B050'
        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'
    CAMQUERIER = CamManager(LABEL, EXPOSURE_NAME, recipient=RECIPIENT)
    if STAGE == 'B010':
        CAMQUERIER.build_new_camera_jobs()
    elif STAGE == 'B020' or STAGE == 'B030' or STAGE == 'B040':
        CAMQUERIER.build_camera_revert_jobs(STAGE)
    elif STAGE == 'B050':
        CAMQUERIER.advance_camera()
    elif STAGE == 'DAG':
        DAG_NAME = CAMQUERIER.build_camera_dag()
        CAMQUERIER.build_new_camera_jobs()
        print 'DAG file generated now run:'
        print 'condor_submit_dag %s' % DAG_NAME
    else:
        print 'Stage [%s] not supported' % STAGE
        sys.exit(2)
    sys.exit(0)
