[meta]
    title = "Capture baz prereqs and outputs after various state changes."

[cylc]
    [[events]]
        timeout = PT1M
        abort on timeout = True
    [[parameters]]
        m = succeeded, expired, failed, submit-failed, waiting
        x = 1..4
    [[parameter templates]]
        m = %(m)s

[scheduling]
    [[graph]]
        R1 = """
            FOO:succeed-any & BAR:finish-all => baz & baz2
            foo<x>:finish => foo<x+1>  # ensure foo<x=4> is the last to run
            bar<x>:finish => bar<x+1>  # ensure bar<x=4> is the last to run
            baz => <m=succeeded>
            <m-1> => <m>
            <m=waiting> => trigger
            trigger => !FOO & !BAR
        """

[runtime]
    [[FOO, BAR]]
    [[RESET]]
        pre-script = """
            if [[ "${CYLC_TASK_NAME}" == "waiting" ]]; then
                # Remove upstream outputs so it's clear that prereqs get unset.
                cylc remove ${CYLC_SUITE_NAME} FOO
                cylc remove ${CYLC_SUITE_NAME} BAR
                sleep 5
            fi

            NEW_STATE=${CYLC_TASK_NAME}
            CUR_STATE=$(cylc dump --tasks ${CYLC_SUITE_NAME} \
                | grep 'baz, 1' | awk '{print $3}' | sed -e 's/,//')
            if [[ "${NEW_STATE}" != "${CUR_STATE}" ]]; then
                cylc reset --state=${NEW_STATE} ${CYLC_SUITE_NAME} baz.1
            fi
            cylc suite-state "${CYLC_SUITE_NAME}" \
                -t baz -p 1 -S "${NEW_STATE}" --interval 1
        """
    [[SHOW]]
        post-script = """
            OUT=${CYLC_SUITE_SHARE_DIR}/${CYLC_TASK_NAME}.out
            cylc show ${CYLC_SUITE_NAME} baz.1 > ${OUT}
        """
    [[foo<x>]]
        inherit = FOO
        script = false
    [[foo<x=4>]]
        script = true
    [[bar<x>]]
        inherit = BAR
        script = false
    [[bar<x=4>]]
        inherit = BAR, SHOW
        script = true
    [[baz]]
        script = sleep 10
    [[baz2]]
        inherit = None, SHOW
        script = """
            cylc suite-state "${CYLC_SUITE_NAME}" \
                -t baz -p 1 -S start --interval=1
        """
    [[<m>]]
        inherit = None, RESET, SHOW
    [[trigger]]
        inherit = None, SHOW
        script = """
            cylc trigger ${CYLC_SUITE_NAME} baz.1
            cylc suite-state "${CYLC_SUITE_NAME}" \
                -t baz -p 1 -S start --interval=1
        """
