#compdef proot-distro pd
#
# Zsh completion for proot-distro and pd
#
# Install:
#   cp _proot-distro /usr/share/zsh/site-functions/_proot-distro
# Or for the current user, add to a directory in $fpath:
#   mkdir -p ~/.local/share/zsh/site-functions
#   cp _proot-distro ~/.local/share/zsh/site-functions/_proot-distro
#   # Add to ~/.zshrc: fpath=(~/.local/share/zsh/site-functions $fpath)

# ---------------------------------------------------------------------------
# Helper: list installed containers
# ---------------------------------------------------------------------------
_proot_distro_containers() {
    local dir
    if [[ -n "${TERMUX_PREFIX}" ]]; then
        dir="${TERMUX_PREFIX}/var/lib/proot-distro/containers"
    elif [[ -n "${ANDROID_ROOT}" ]]; then
        dir="/data/data/com.termux/files/usr/var/lib/proot-distro/containers"
    else
        dir="${XDG_DATA_HOME:-${HOME}/.local/share}/proot-distro/containers"
    fi
    local -a containers
    if [[ -d "${dir}" ]]; then
        local d
        for d in "${dir}"/*/; do
            [[ -d "${d}rootfs" ]] && containers+=("${d:t}")
        done
    fi
    _describe 'container' containers
}

# ---------------------------------------------------------------------------
# Termux/Android detection — mirrors _detect_termux() in constants.py.
# Returns 0 (true) when at least two of three independent indicators match.
# ---------------------------------------------------------------------------
_proot_distro_is_termux() {
    local score=0
    [[ -f /system/build.prop || -d /data/app ]] && ((score++))
    [[ -n "${TERMUX_APP__APP_VERSION_NAME}" || -n "${TERMUX_VERSION}" ]] && ((score++))
    local prefix="${TERMUX__PREFIX:-/data/data/com.termux/files/usr}"
    [[ -r "${prefix}" && -x "${prefix}" ]] && ((score++))
    (( score >= 2 ))
}

# ---------------------------------------------------------------------------
# Main completion function
# ---------------------------------------------------------------------------
_proot_distro() {
    local curcontext="${curcontext}" state state_descr line
    typeset -A opt_args

    _arguments -C \
        '(-h --help)'{-h,--help}'[show help]' \
        '1:command:->command' \
        '*:: :->args'

    case "${state}" in
        command)
            local -a subcommands
            subcommands=(
                'install:install a container from a Docker image or local archive'
                'remove:remove an installed container'
                'rename:rename a container'
                'reset:reinstall a container from its original image'
                'login:open a shell inside a container'
                'list:list installed containers'
                'backup:backup a container to a tar archive'
                'restore:restore a container from a tar archive'
                'clear-cache:clear the download cache'
                'copy:copy files between host and container'
                'sync:synchronize files between host and container'
                'run:run the image entrypoint/cmd in a container'
                'help:show help'
            )
            _describe 'command' subcommands
            ;;

        args)
            local cmd="${words[1]}"
            curcontext="${curcontext%:*:*}:proot-distro-${cmd}:"

            case "${cmd}" in

                # -----------------------------------------------------------
                install)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]' \
                        '(--name --override-alias)--name[install under a custom container name]:alias' \
                        '(--name --override-alias)--override-alias[install under a custom container name]:alias' \
                        '--architecture[target CPU architecture]:arch:(aarch64 arm i686 riscv64 x86_64)' \
                        '1:docker image or local archive:_files'
                    ;;

                # -----------------------------------------------------------
                remove)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]' \
                        '(-v --verbose)'{-v,--verbose}'[print each removed file]' \
                        '1:container:_proot_distro_containers'
                    ;;

                # -----------------------------------------------------------
                rename)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]' \
                        '1:original container:_proot_distro_containers' \
                        '2:new container name'
                    ;;

                # -----------------------------------------------------------
                reset)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]' \
                        '1:container:_proot_distro_containers'
                    ;;

                # -----------------------------------------------------------
                login)
                    local -a _login_args
                    _login_args=(
                        '(-h --help)'{-h,--help}'[show help]'
                        '--user[run as this user (default\: root)]:user'
                        '--redirect-ports[redirect ports below 1024]'
                        '--shared-home[mount home inside container]'
                        '--shared-tmp[share /tmp with the host]'
                        '--shared-x11[share the X11 socket (/tmp/.X11-unix)]'
                        '*--bind[bind-mount PATH\[:DEST\] into the container]:path:_files'
                        '--emulator[path to QEMU user-mode emulator binary]:path:_files'
                        '--kernel[fake kernel release string]:string'
                        '--hostname[hostname visible inside the container]:string'
                        '--work-dir[initial working directory]:path:_directories'
                        '*--env[set environment variable]:VAR=VALUE'
                        '--get-proot-cmd[print the proot command line and exit]'
                        '1:container:_proot_distro_containers'
                        '*:inner command:_command_names -e'
                    )
                    _proot_distro_is_termux && _login_args+=(
                        '(--isolated --minimal)--isolated[isolated mode]'
                        '(--isolated --minimal)--minimal[minimal isolated mode]'
                        '--no-link2symlink[disable proot link2symlink extension]'
                        '--no-sysvipc[disable SysV IPC emulation]'
                        '--no-kill-on-exit[do not kill child processes when session ends]'
                    )
                    _arguments -s "${_login_args[@]}"
                    ;;

                # -----------------------------------------------------------
                list)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]'
                    ;;

                # -----------------------------------------------------------
                backup)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]' \
                        '(-v --verbose)'{-v,--verbose}'[print each archived file]' \
                        '--output[write archive to FILE instead of stdout]:file:_files' \
                        '--compress[compression algorithm]:type:(gzip bzip2 xz none)' \
                        '1:container:_proot_distro_containers'
                    ;;

                # -----------------------------------------------------------
                restore)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]' \
                        '(-v --verbose)'{-v,--verbose}'[print each extracted file]' \
                        '1:archive:_files -g "*.tar *.tar.gz *.tgz *.tar.bz2 *.tbz2 *.tar.xz *.txz"'
                    ;;

                # -----------------------------------------------------------
                clear-cache)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]' \
                        '(-v --verbose)'{-v,--verbose}'[list removed files]'
                    ;;

                # -----------------------------------------------------------
                copy)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]' \
                        '(-v --verbose)'{-v,--verbose}'[print each copied file]' \
                        '(-m --move)'{-m,--move}'[move instead of copy]' \
                        '(-r --recursive)'{-r,--recursive}'[copy directories recursively]' \
                        '1:source (host path or container\:path):_files' \
                        '2:destination (host path or container\:path):_files'
                    ;;

                # -----------------------------------------------------------
                sync)
                    _arguments \
                        '(-h --help)'{-h,--help}'[show help]' \
                        '(-v --verbose)'{-v,--verbose}'[print each synced file]' \
                        '--checksum[use CRC32 checksum instead of size+mtime]' \
                        '--delete[remove destination entries absent from source]' \
                        '1:source (host path or container\:path):_files' \
                        '2:destination (host path or container\:path):_files'
                    ;;

                # -----------------------------------------------------------
                run)
                    local -a _run_args
                    _run_args=(
                        '(-h --help)'{-h,--help}'[show help]'
                        '--user[run as this user (default\: root)]:user'
                        '--redirect-ports[redirect ports below 1024]'
                        '--shared-home[mount home inside container]'
                        '--shared-tmp[share /tmp with the host]'
                        '--shared-x11[share the X11 socket (/tmp/.X11-unix)]'
                        '*--bind[bind-mount PATH\[:DEST\] into the container]:path:_files'
                        '--emulator[path to QEMU user-mode emulator binary]:path:_files'
                        '--kernel[fake kernel release string]:string'
                        '--hostname[hostname visible inside the container]:string'
                        '--work-dir[initial working directory]:path:_directories'
                        '*--env[set environment variable]:VAR=VALUE'
                        '--get-proot-cmd[print the proot command line and exit]'
                        '1:container:_proot_distro_containers'
                        '*:arguments'
                    )
                    _proot_distro_is_termux && _run_args+=(
                        '(--isolated --minimal)--isolated[isolated mode]'
                        '(--isolated --minimal)--minimal[minimal isolated mode]'
                        '--no-link2symlink[disable proot link2symlink extension]'
                        '--no-sysvipc[disable SysV IPC emulation]'
                        '--no-kill-on-exit[do not kill child processes when session ends]'
                    )
                    _arguments -s "${_run_args[@]}"
                    ;;

                # -----------------------------------------------------------
                help)
                    local -a topics
                    topics=(
                        install remove rename reset login list
                        backup restore clear-cache copy sync run
                    )
                    _describe 'topic' topics
                    ;;

            esac
            ;;
    esac
}

_proot_distro "$@"
