#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Executable to launch all jobs for a settings file describing which
project on XNAT and which pipelines to run on those projects.
"""

from __future__ import print_function

import dax
import sys
from dax import DAX_Settings

DAX_SETTINGS = DAX_Settings()
__description__ = "Launch all tasks that need to run (NEED_TO_RUN)."


def parse_args():
    """Method to parse arguments base on ArgumentParser.

    :return: parser object parsed
    """
    from argparse import ArgumentParser
    ap = ArgumentParser(prog='dax_launch', description=__description__)
    ap.add_argument(dest='settings_path', help='Settings Path')
    ap.add_argument('--logfile', dest='logfile',
                    help='Logs file path if needed.', default=None)
    _help = 'Project ID from XNAT to run dax_update on locally (only one \
project).'
    ap.add_argument('--project', dest='project', help=_help, default=None)
    _help = 'list of sessions label from XNAT to run dax_launch on locally.'
    ap.add_argument('--sessions', dest='sessions', help=_help, default=None)
    ap.add_argument('--writeonly', dest='writeonly', action='store_true',
                    help='Only write job files without launching them.')
    _help = 'Folder to store the PBS when using --writeonly. Default: \
RESULTS_DIR/TRASH.'
    ap.add_argument('--pbsfolder', dest='pbsfolder', help=_help, default=None)
    ap.add_argument('--nodebug', dest='debug', action='store_false',
                    help='Avoid printing DEBUG information.')
    ap.add_argument('--no_qsub', dest='no_qsub', action='store_true',
                    help='Run the jobs locally on your computer in serial.')
    return ap.parse_args()


if __name__ == '__main__':
    args = parse_args()

    print('Deprecated executable. Use dax launch instead.')
    if DAX_SETTINGS.is_cluster_valid():
        dax.bin.launch_jobs(args.settings_path, args.logfile, args.debug,
                            args.project, args.sessions, args.writeonly,
                            args.pbsfolder, args.no_qsub)
    else:
        sys.stdout.write('Please edit your settings via dax_setup for the \
cluster section\n.')
