#!/usr/bin/env python3
#                                                            _
# Pacs ToolKit Listen wrapper
#
# (c) 2016 Fetal-Neonatal Neuroimaging & Developmental Science Center
#                   Boston Children's Hospital
#
#              http://childrenshospital.org/FNNDSC/
#                        dev@babyMRI.org
#

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import  sys, os
from    argparse            import RawTextHelpFormatter
from    argparse            import ArgumentParser
from    terminaltables      import SingleTable
from    pfmisc._colors      import Colors
import  pypx

import  pudb
from    pudb.remote         import set_trace
import  rpudb
import  pfmisc

str_name    = "px-listen"
str_version = "1.2.0"
str_desc    = Colors.CYAN + """

             _ _     _
            | (_)   | |
 _ __ __  __| |_ ___| |_  ___ _ __
| '_ \\ \\/ / | | / __| __|/ _ \\ '_ \\
| |_) |>  < | | \\__ \\ |_|  __/ | | |
| .__//_/\\_\\| |_|___/\\__|\\___|_| |_|
| |
|_|


                        PACS ToolKit Wrapper
                             listener

                       -- version """ + \
             Colors.YELLOW + str_version + Colors.CYAN + """ --

    ``px-listen`` is a wrapper script about the DCMTK 'storescu' application
    that provides functionality for receiving PACS communictaions.

    ``px-listen`` is a core application that operates mostly as a daemon
    called by a management process (such as ``xinetd`` on Linux systems). It
    is not a user-facing application and not called by a user/client process.

""" + Colors.NO_COLOUR

def synopsis(ab_shortOnly = False):
    scriptName = os.path.basename(sys.argv[0])
    shortSynopsis =  Colors.YELLOW + '''
    NAME

	    %s

        - PACS listener / interpreter service

    SYNOPSIS

            px-listen                                               \\
                [--tmpdir|-t <tmpdir>]                              \\
                [--logdir|-l <logdir>]                              \\
                [--datadir|-d <datadir>]                            \\
                [--executable <executable>]                         \\
                [-x|--desc]                                         \\
                [-y|--synopsis]                                     \\
                [--version]                                         \\
                [--debug]                                           \\
                [--verbosity <level>]

    ''' % scriptName + Colors.NO_COLOUR

    description = Colors.LIGHT_GREEN + '''

        [--tmpdir|-t <tmpdir>]
        A tmp directory that will contain data as it is incoming from a
        remote PACS.

        [--logdir|-l <logdir>]
        The directory containing log files relevant to px-listen operation.

        [--datadir|-d <datadir>]
        The directory that will contain the root of the file tree of received
        image files.

        [--executable|-e <storescp>]
        The actual 'storescp' absolute location.

        [-x|--desc]
        Provide an overview help page.

        [-y|--synopsis]
        Provide a synopsis help summary.

        [--version]
        Print internal version number and exit.

        [--debug]
        If specified, then log any debugging noise also to the <logdir>.

        [-v|--verbosity <level>]
        Set the verbosity level. "0" typically means no/minimal output. Allows for
        more fine tuned output control as opposed to '--quiet' that effectively
        silences everything.

''' + Colors.PURPLE + '''

    EXAMPLES

        px-listen                                                   \\
                --tmpdir /dicom/tmp                                 \\
                --logdir /dicom/log                                 \\
                --datadir /dicom/data

''' + Colors.NO_COLOUR

    if ab_shortOnly:
        return shortSynopsis
    else:
        return shortSynopsis + description

parser = ArgumentParser(
            description         = str_desc,
            formatter_class     = RawTextHelpFormatter
        )

# Settings
parser.add_argument(
    '-t', '--tmpdir',
    action  = 'store',
    dest    = 'tmp_directory',
    type    = str,
    default = '/tmp',
    help    = 'Directory to store temporary files.'
    )
parser.add_argument(
    '-l', '--logdir',
    action  = 'store',
    dest    = 'log_directory',
    type    = str,
    default = '/tmp/log',
    help    = 'Directory to store log files.'
    )
parser.add_argument(
    '-d', '--datadir',
    action  = 'store',
    dest    = 'data_directory',
    type    = str,
    default = '/tmp/data',
    help    = 'Directory to store DICOM files.'
    )
parser.add_argument(
    '-e', '--executable',
    action  = 'store',
    dest    = 'executable',
    type    = str,
    default = '/usr/bin/storescp',
    help    = 'storescp executable absolute location'
    )
parser.add_argument(
    '--debug',
    action  = 'store_true',
    dest    = 'b_debug',
    default = False,
    help    = 'If specified, then also log debug info to <logdir>'
    )
parser.add_argument(
    "-v", "--verbosity",
    help    = "verbosity level for app",
    dest    = 'verbosity',
    type    = int,
    default = 1)
parser.add_argument(
    "-x", "--desc",
    help    = "long synopsis",
    dest    = 'desc',
    action  = 'store_true',
    default = False
)
parser.add_argument(
    "-y", "--synopsis",
    help    = "short synopsis",
    dest    = 'synopsis',
    action  = 'store_true',
    default = False
)
parser.add_argument(
    '--version',
    help    = 'if specified, print version number',
    dest    = 'b_version',
    action  = 'store_true',
    default = False
)

# set_trace(host = "0.0.0.0", port = 5555, term_size = (252, 63))

args        = parser.parse_args()

if args.b_debug:
    debug   = pfmisc.debug(
                verbosity   = 5,
                level       = 1,
                within      = 'px-listen::base module',
                debugToFile = True,
                debugFile   = '/tmp/startup.log'
    )
    debug.qprint("Starting...")

if args.desc or args.synopsis:
    print(str_desc)
    if args.desc:
        str_help     = synopsis(False)
    if args.synopsis:
        str_help     = synopsis(True)
    print(str_help)
    sys.exit(1)

if args.b_version:
    print("Version: %s" % str_version)
    sys.exit(1)

pypx.listen(vars(args))
