#!/usr/bin/python
from XnatUploadTool import XnatUploadTool
import configargparse

parser = configargparse.ArgumentParser(
    default_config_files=['~/.xnatupload.cnf', './xnatupload.cnf', '/etc/xnatupload.cnf'],
    description='Xnat upload script, takes a single directory and uploads to site.\n\n'
                'Target directory is a session, with any number of scans within it. Directories within are treated '
                'as scans, populated with either many separate dicom files or a single compressed flat archive of '
                'dicom files.\n\n'
                'Zip files found in the top level are treated as a scan and are expected to have a compressed '
                'archive of dcm files. The session name is assumed as the same as the zip file name, without the '
                ' zip extension.')

parser.add_argument('--host', type=str,
                    help='URL of xnat host',
                    default='http://raddemo.radiologics.com')
parser.add_argument('--username', type=str,
                    help='Username, if not set will pull from XNATCREDS env variable as USERNAME:PASSWORD',
                    default=None)
parser.add_argument('--password', type=str,
                    help='Password, if not set will pull from XNATCREDS env variable as USERNAME:PASSWORD',
                    default=None)
parser.add_argument('--project', type=str,
                    help='Project to upload to',
                    default=None)
parser.add_argument('--subject', type=str,
                    help='Subject to upload to',
                    default=None)
parser.add_argument('--session', type=str,
                    help='Session name to use for upload')
parser.add_argument('--logfile', type=str,
                    help='File to log upload events to, if not set use stdout',
                    default=None)
parser.add_argument('--tmpdir', type=str,
                    help='Directory to untar/compress files to',
                    default='/tmp')
parser.add_argument('-V', '--validate', action='store_true',
                    help='Validate scan descriptions and filecounts after upload')
parser.add_argument('-r', '--raw', action='store_true',
                    help='Disable recompression as zip file uploading each scan file individually. Severely '
                         'impacts performance, but can solve problems with extremely large sessions')
parser.add_argument('-v', '--verbose', action='store_true',
                    help='Produce verbose logging')
parser.add_argument('-t', '--timeout', type=int,
                    help='Read timeout in seconds, set to higher values if uploads are failing due to timeout',
                    default=3600)
parser.add_argument('-s', '--sessiontimeout', type=int,
                    help='Session timeout for xnat site in minutes, to determine session refresh frequency',
                    default=15)
parser.add_argument('-j', '--jobs', type=int,
                    help='Run in X parallel processes to take advantage of multiple cores',
                    default=None)
parser.add_argument('directory', type=str,
                    help='Directory to upload from',
                    default=None)


mytool = XnatUploadTool(**vars(parser.parse_args()))
mytool.start_upload()
