#!/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. '
                '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. '
                'If a single file is specifed rather than a directory, it is assumed to be non-dicom. This '
                'functionality requires the specification of other datatypes. This method is single threaded.'
)

parser.add_argument('-c', '--config', is_config_file=True, help='Config file path')
parser.add_argument('-i', '--ingestconfig', default=None,
                    help='Ingest filesystem using formatting definition in config file')
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=120)
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('-x', '--xmlnamespace', default='xnat',
                    help='XML namespace for datatype')
parser.add_argument('-d', '--datatype', default='dicom',
                    help='Data type to upload')
parser.add_argument('--resource', default=None,
                    help='Resource name for non-dicom files')
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', default=None,
                    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('--subjectfields', '--subjectheaders', default=None,
                    help='Subject fields in json format')
parser.add_argument('--bsubjectfields', '--bsubjectheaders', default=None,
                    help='Subject fields 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('--sessionfields', '--sessionheaders', default=None,
                    help='Session fields in json format')
parser.add_argument('--bsessionfields', '--bsessionheaders', default=None,
                    help='Session fields 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('--scanfields', '--scanheaders', default=None,
                    help='Scan fields in json format')
parser.add_argument('--bscanfields', '--bscanheaders', default=None,
                    help='Scan fields in base64 json format')
parser.add_argument('--newerthan', default=None,
                    help='Only upload files newer than x minutes (can be used in combination with olderthan')
parser.add_argument('--olderthan', default=None,
                    help='Only upload files older than x minutes (can be used in combination with newerthan)')

parser.add_argument('--deletesessions', action='store_true',
                    help='Delete existing sessions prior to upload of new data')
parser.add_argument('target',
                    help='Target upload. Can be a directory with subdirectories of dicom files, '
                         'or a single non-dicom file')

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

mytool.start_upload()
mytool.close_session()
