#!/usr/bin/env python3
"""hos-delta args module: Support for parsing cmdline arguments"""

from argparse import ArgumentParser

from hos_delta import meta

__SCRIPT_NAME = 'hos-delta'

def get_args():
    """Parse the provided arguments.

    Returns
    -------
    Namespace
        Returnes a populated argparse namespace.

    """
    parser = ArgumentParser(prog=__SCRIPT_NAME, add_help=False, description=meta.description)
    parser.add_argument('-h', '--help', action='help', help='Print this message and exit.')
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='Run the tool in verbose mode.')

    subparsers = parser.add_subparsers(title='Commands', dest='command',
                                       help='Subcommand to run.')

    # The delta generate subcommand
    generate = subparsers.add_parser('generate', help='Generate a delta root filesystem payload.')
    generate.set_defaults(func='generate')
    generate.add_argument('releases_path', help='Path to the directory providing all the'\
                          'released versions.')
    generate.add_argument('version', help='The version for which we generate the delta. '\
                          'Be aware that it must be included in releases-path.')
    generate.add_argument('image', help='The image name associated to the root filesystem '\
                          'which will be considered for delta generation.')
    generate.add_argument('machine', help='The machine name for which we generate the delta.')
    generate.add_argument('--novalidation', action='store_true',
                          help='By default, the generator validates all the releases for '\
                          'a valid update path to the target release. This flag speeds up '\
                          'the generation by avoiding carrying out these validations.')
    generate.add_argument('--delta-precentage-limit', type=float, default=50.0,
                          help='Deltas are designed to improve the network bandwidth when '\
                          'updating systems. This flag defines the precentage threshold above '\
                          'which the tool bails out in creating the delta. Generally, when this '\
                          'happens, a new baseline should be defined. Default value: 50.')

    # The manual generate subcommand
    mgenerate = subparsers.add_parser('manual-generate', help='Generate a delta root filesystem '\
                                      'payload, against one specific root filesystem.')
    mgenerate.set_defaults(func='mgenerate')
    mgenerate.add_argument('base_rootfs_path', help='The root filesystem path of the base.')
    mgenerate.add_argument('base_fingerprint_path', help='The fingerprint file path of the base.')
    mgenerate.add_argument('target_rootfs_path', help='The root filesystem path of the target.')
    mgenerate.add_argument('target_fingerprint_path', help='The fingerprint file path of the '\
                           'target.')
    mgenerate.add_argument('--novalidation', action='store_true',
                          help='By default, the generator validates all the releases for '\
                          'a valid update path to the target release. This flag speeds up '\
                          'the generation by avoiding carrying out these validations.')
    mgenerate.add_argument('--delta-precentage-limit', type=float, default=50.0,
                          help='Deltas are designed to improve the network bandwidth when '\
                          'updating systems. This flag defines the precentage threshold above '\
                          'which the tool bails out in creating the delta. Generally, when this '\
                          'happens, a new baseline should be defined. Default value: 50.')

    # The delta apply subcommand
    dapply = subparsers.add_parser('apply', help='Apply a delta to an inactive partition.')
    dapply.set_defaults(func='dapply')
    dapply.add_argument('delta_path',
                        help='Path to the location where the delta is unpacked on disk.')
    dapply.add_argument('active_path', help='Path to the active partition mount point.')
    dapply.add_argument('inactive_path',
                        help='Path to the inactive partition mount point. WARNING: This path '\
                        'will be cleaned up before applying the delta.')
    dapply.add_argument('--novalidation', action='store_true',
                        help='By default, after applying a delta, the destination mount point is '\
                        'validated based on its fingerprint. This flag speeds up applying a delta '\
                        'by avoiding carrying out these validations.')

    # The validate subcomand
    validate = subparsers.add_parser('validate',
                                     help='Validate a root filesystem based on its fingerprint '\
                                     'file.')
    validate.set_defaults(func='validate')
    validate.add_argument('rootfs_path', help='Path to the root filesystem to validate')

    return parser.parse_args()
