#!/usr/bin/env python

# THIS FILE IS PART OF THE CYLC SUITE ENGINE.
# Copyright (C) 2008-2016 NIWA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""cylc [task] job-submit [--remote-mode] JOB-FILE-PATH

(This command is for internal use. Users should use "cylc submit".)
Submit a job file.

"""


import sys
from cylc.remote import remrun


def main():
    """CLI main."""
    parser = COP(
        __doc__,
        argdoc=[("JOB-FILE-PATH", "the path of the job file")])
    parser.add_option(
        "--remote-mode",
        help="Is this being run on a remote job host?",
        action="store_true", dest="remote_mode", default=False)
    opts, args = parser.parse_args()
    ret_code, out, err, job_id = BATCH_SYS_MANAGER.job_submit(
        args[0], opts.remote_mode)
    if err:
        sys.stderr.write(err)
    if out:
        sys.stdout.write(out)
    if job_id:
        sys.stdout.write(
            "%s=%s\n" % (BATCH_SYS_MANAGER.CYLC_BATCH_SYS_JOB_ID, job_id))
    sys.exit(ret_code)


if __name__ == "__main__" and not remrun().execute():
    from cylc.option_parsers import CylcOptionParser as COP
    from cylc.batch_sys_manager import BATCH_SYS_MANAGER
    main()
