import subprocess

# Plans are to flesh this out a bit more to make some smarter copying etc
from .errors import ValidationError, SubprocessError


def _args_check(args, function_name):
    if args is None:
        raise ValidationError(f"Error: no arguments provided in {function_name}")
    if type(args) != list or len(args) == 0:
        raise ValidationError(f"Error: no arguments provided in {function_name}")
    return 0


def cp_wrapper(args: [str]):
    if args is None:
        raise ValidationError(f"Error: no arguments provided to {cp_wrapper.__name__}")
    command_and_args = ["cp"] + args
    return simple_unix_wrapper(command_and_args)


def tar_wrapper(args: [str]):
    if args is None:
        raise ValidationError(f"Error: no arguments provided to {tar_wrapper.__name__}")
    command_and_args = ["tar"] + args
    return simple_unix_wrapper(command_and_args)


def chmod_wrapper(args: [str]):
    if args is None:
        raise ValidationError(f"Error: no arguments provided to {chmod_wrapper.__name__}")
    command_and_args = ["chmod"] + args
    return simple_unix_wrapper(command_and_args)


def local_rsync_wrapper(args: [str]):
    if args is None:
        raise ValidationError(f"Error:no arguments provided to {local_rsync_wrapper.__name__}")
    command_and_args = ["rsync"] + args
    return simple_unix_wrapper(command_and_args)


def simple_unix_wrapper(command_and_args: [str]):
    """ This should provide the program name and all arguments in a list.

Only a single command should be called.
    """
    _args_check(command_and_args, simple_unix_wrapper.__name__)

    try:
        subprocess_exitcode = subprocess.check_call(command_and_args)
    except (subprocess.CalledProcessError) as cpe:
        raise SubprocessError(f"{type(cpe)} in python simple_unix_wrapper") from cpe
    except (ValidationError) as ve:
        raise ValidationError(f"{type(ve)} in python simple_unix_wrapper") from ve
    return subprocess_exitcode
