#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK

# Copyright 2012-2023, Andrey Kislyuk and argcomplete contributors.
# Licensed under the Apache License. See https://github.com/kislyuk/argcomplete for more info.

"""
Activate the generic bash-completion script or zsh completion autoload function for the argcomplete module.
"""

import argparse
import os
import shutil
import sys

import argcomplete


def get_zsh_system_dir():
    return "/usr/local/share/zsh/site-functions"


def get_bash_system_dir():
    if "BASH_COMPLETION_COMPAT_DIR" in os.environ:
        return os.environ["BASH_COMPLETION_COMPAT_DIR"]
    elif os.path.exists("/etc/bash_completion.d"):
        return "/etc/bash_completion.d"
    else:
        return "/usr/local/etc/bash_completion.d"


def install_to_destination(dest):
    activator = os.path.join(os.path.dirname(argcomplete.__file__), "bash_completion.d", "python-argcomplete")
    if dest == "-":
        with open(activator) as fh:
            sys.stdout.write(fh.read())
        return
    destdir = os.path.dirname(dest)
    if not os.path.exists(destdir):
        try:
            os.makedirs(destdir, exist_ok=True)
        except Exception as e:
            parser.error(f"path {destdir} does not exist and could not be created: {e}")
    try:
        print(f"Installing {activator} to {dest}...", file=sys.stderr)
        shutil.copy(activator, dest)
        print("Installed.", file=sys.stderr)
    except Exception as e:
        parser.error(
            f"while installing to {dest}: {e}. Please run this command using sudo, or see --help for more options."
        )


parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--dest", help='Specify the shell completion modules directory to install into, or "-" for stdout')
parser.add_argument("--user", help="Install into user directory", action="store_true")
argcomplete.autocomplete(parser)
args = parser.parse_args()
destinations = []

if args.dest:
    if args.dest != "-" and not os.path.exists(args.dest):
        parser.error(f"directory {args.dest} was specified via --dest, but it does not exist")
    destinations.append(args.dest)
elif args.user:
    print(
        'WARNING: zsh has no standard user completions directory. To use global completion with zsh, please run this '
        'command using sudo, or run "echo $fpath" and install the completion module into one of the listed directories'
        ' using "activate-global-python-argcomplete --dest=- > completions-dir/_python-argcomplete".',
        file=sys.stderr,
    )
    user_destination = os.path.expanduser("~/.bash_completion")
    if os.path.exists(user_destination):
        parser.error(
            f'Bash user completion configuration file {user_destination} already exists. Please remove it and try '
            f'again, or use "activate-global-python-argcomplete --dest=- >> {user_destination}".'
        )
    destinations.append(os.path.expanduser("~/.bash_completion"))
else:
    destinations.append(f"{get_zsh_system_dir()}/_python-argcomplete")
    destinations.append(f"{get_bash_system_dir()}/python-argcomplete")

for destination in destinations:
    install_to_destination(destination)

if args.dest is None:
    print("Please restart your shell or source the installed file to activate it.", file=sys.stderr)
