#!/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('-c', '--config', is_config_file=True, help='Config file path')
parser.add_argument('--username',default=None,
                    help='Username, if not set will pull from XNATCREDS env variable as USERNAME:PASSWORD')
parser.add_argument('--password', default=None,
                    help='Password, if not set will pull from XNATCREDS env variable as USERNAME:PASSWORD')
parser.add_argument('--logfile',
                    help='File to log upload events to, if not set use stdout',
                    default=None)
parser.add_argument('--tmpdir',
                    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('-d', '--datatype', default='dicom',
                    help='Data type to upload')
parser.add_argument('-f', '--folder', default=None,
                    help='Resource folder for non-dicom data')
parser.add_argument('--host', required=True,
                    help='URL of xnat host')
parser.add_argument('--project', required=True,
                    help='Project to upload to')
parser.add_argument('--subject', required=True,
                    help='Subject to upload to, can be string or in dicom tag format (0000,0000). '
                         'If tag in parenthesis is used, will be pulled from first dicom file found.')
parser.add_argument('--subjectheaders', default='{}',
                    help='Subject headers in json format')
parser.add_argument('--bsubjectheaders', default=None,
                    help='Subject headers in base64 json format')
parser.add_argument('--session', default=None,
                    help='Session name to use for upload, can be string or in dicom tag format (0000,0000). '
                         'If tag in parenthesis is used, will be pulled from first dicom file found.')
parser.add_argument('--sessionheaders', default='{}',
                    help='Session headers in json format')
parser.add_argument('--bsessionheaders', default=None,
                    help='Session headers in base64 json format')
parser.add_argument('--scan', default=None,
                    help='Scan to upload files to, can be string or in dicom tag format (0000,0000).')
parser.add_argument('--scanheaders', default='{}',
                    help='Scan headers in json format')
parser.add_argument('--bscanheaders', default=None,
                    help='Scan headers in base64 json format')
parser.add_argument('directory',
                    help='Directory to upload from')

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

mytool.start_upload()
