#!/usr/bin/env python

"""
Generates the complete warp stage DAG for one exposure
"""
import sys
from ipp.WarpManager import WarpManager as WarpManager

#################################################################
if __name__ == '__main__':
    if len(sys.argv) != 4 and len(sys.argv) != 5:
        print 'generate_warp_jobs.py <stage> <label> <exposure_name> [recipient]'
        print '  e.g.: generate_warp_jobs.py D010 MD08.jtrp o5806g0078o'
        print '  Acceptable value for <stage>: DAG, D010-D040, D110-D150, D210, D310'
        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'
    WARP_QUERIER = WarpManager(LABEL, EXPOSURE_NAME, RECIPIENT)
    if STAGE == 'D010':
        WARP_QUERIER.build_new_exp_warp_jobs()
    elif STAGE == 'D020' or STAGE == 'D030' or STAGE == 'D040':
        WARP_QUERIER.build_warp_exp_revert_jobs(STAGE)
    elif STAGE == 'D110':
        WARP_QUERIER.build_new_skycell_warp_jobs()
    elif STAGE == 'D120' or STAGE == 'D130' or STAGE == 'D140':
        WARP_QUERIER.build_warp_skycell_revert_jobs(STAGE)
    elif STAGE == 'D210':
        WARP_QUERIER.build_new_summary_warp_jobs()
    elif STAGE == 'D310':
        WARP_QUERIER.advance_warp()
    elif STAGE == 'DAG':
        DAG_NAME = WARP_QUERIER.build_warp_dag()
        WARP_QUERIER.build_new_exp_warp_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)
