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

parser = configargparse.ArgumentParser(
    default_config_files=['~/.xnatupload.cnf', './xnatupload.cnf', '/etc/xnatupload.cnf'],
    description='Xnat dcim upload script, takes a single directory of dicm files, recurses the structure, analyzes'
                'dcim headres and uploads appropriately to xnat site.\n\n'
                '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('--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('-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('--host', required=True,
                    help='URL of xnat host')
parser.add_argument('--project', required=True,
                    help='Project to upload to')
parser.add_argument('--projectlabel', default=None,
                    help='Project to upload to, if no static project is declared. Can be string or in dicom tag format '
                         '(0000,0000)')
parser.add_argument('--subjectlabel', default='(0010,0020)',
                    help='Subject to upload to, can be string or in dicom tag format (0010,0020)')
parser.add_argument('--sessionlabel', default='(0010,0010)',
                    help='Session name to use for upload, can be string or in dicom tag format (0010,0010)')
parser.add_argument('--scanlabel', default='(0020,0011)',
                    help='Session name to use for upload, can be string or in dicom tag format (0020,0011)')

parser.add_argument('--sessiondate', default='(0008,0020)',
                    help='Session date tag in format  (0008,0020)')
parser.add_argument('--scandate', default='(0008,0021)',
                    help='Session date tag in format  (0008,0021)')

parser.add_argument('--deletesessions', action='store_true',
                    help='Delete existing sessions prior to upload of new data')
parser.add_argument('target',
                    help='Target upload directory')
mytool = XnatUploadToolDicom(**vars(parser.parse_args()))
mytool.start_upload()
