# SPDX-License-Identifier: LGPL-2.1-or-later
# shellcheck shell=bash

_mkosi_compgen_files() {
    compgen -f -- "$1"
}

_mkosi_compgen_dirs() {
    compgen -d -- "$1"
}

_mkosi_completion() {
    local -a _mkosi_options _mkosi_verbs
    local -A _mkosi_nargs _mkosi_choices _mkosi_compgen

    _mkosi_options=(-f -C -B -I -d -r -m -t -o -O -p -E -T -a -i --force --directory --debug --debug-shell --debug-workspace --no-pager --genkey-valid-days --genkey-common-name --auto-bump --doc-format --json --include --initrd-include --profile --dependency --minimum-version --configure-script --pass-environment --distribution --release --architecture --mirror --local-mirror --repository-key-check --repositories --cache-only --package-manager-tree --format --manifest-format --output --compress-output --compress-level --output-dir --workspace-dir --cache-dir --package-cache-dir --build-dir --image-version --image-id --split-artifacts --repart-dir --sector-size --repart-offline --overlay --use-subvolumes --seed --clean-script --package --build-package --volatile-package --package-directory --volatile-package-directory --with-recommends --with-docs --base-tree --skeleton-tree --extra-tree --remove-package --remove-files --clean-package-metadata --source-date-epoch --sync-script --prepare-script --build-script --postinst-script --finalize-script --postoutput-script --build-sources --build-sources-ephemeral --environment --env-file --without-tests --with-network --bootable --bootloader --bios-bootloader --shim-bootloader --unified-kernel-images --unified-kernel-image-format --initrd --microcode-host --initrd-package --initrd-volatile-package --kernel-command-line --kernel-modules-include --kernel-modules-include-host --kernel-modules-exclude --kernel-modules-initrd --kernel-modules-initrd-include --kernel-modules-initrd-include-host --kernel-modules-initrd-exclude --locale --locale-messages --keymap --timezone --hostname --root-password --root-shell --autologin --make-initrd --ssh --selinux-relabel --secure-boot --secure-boot-auto-enroll --secure-boot-key --secure-boot-key-source --secure-boot-certificate --secure-boot-sign-tool --verity-key --verity-key-source --verity-certificate --sign-expected-pcr --passphrase --checksum --sign --key --proxy-url --proxy-exclude --proxy-peer-certificate --proxy-client-certificate --proxy-client-key --incremental --settings --extra-search-path --ephemeral --credential --kernel-command-line-extra --acl --tools-tree --tools-tree-distribution --tools-tree-release --tools-tree-mirror --tools-tree-repository --tools-tree-package-manager-tree --tools-tree-package --tools-tree-certificates --runtime-tree --runtime-size --runtime-scratch --runtime-network --runtime-build-sources --unit-property --ssh-key --ssh-certificate --vmm --machine --forward-journal --qemu-gui --qemu-smp --qemu-mem --qemu-kvm --qemu-vsock --qemu-vsock-cid --qemu-swtpm --qemu-cdrom --qemu-firmware --qemu-firmware-variables --qemu-kernel --qemu-drive --qemu-args)

    _mkosi_nargs=([-f]=0 [-C]=0 [-B]=0 [-I]=1 [-d]=1 [-r]=1 [-m]=1 [-t]=1 [-o]=1 [-O]=1 [-p]=1 [-E]=1 [-T]='?' [-a]='?' [-i]='?' [--force]=0 [--directory]=0 [--debug]=0 [--debug-shell]=0 [--debug-workspace]=0 [--no-pager]=0 [--genkey-valid-days]=0 [--genkey-common-name]=0 [--auto-bump]=0 [--doc-format]=0 [--json]=0 [--include]=1 [--initrd-include]=1 [--profile]=1 [--dependency]=1 [--minimum-version]=1 [--configure-script]=1 [--pass-environment]=1 [--distribution]=1 [--release]=1 [--architecture]=1 [--mirror]=1 [--local-mirror]=1 [--repository-key-check]='?' [--repositories]=1 [--cache-only]=1 [--package-manager-tree]=1 [--format]=1 [--manifest-format]=1 [--output]=1 [--compress-output]='?' [--compress-level]=1 [--output-dir]=1 [--workspace-dir]=1 [--cache-dir]=1 [--package-cache-dir]=1 [--build-dir]=1 [--image-version]=1 [--image-id]=1 [--split-artifacts]='?' [--repart-dir]=1 [--sector-size]=1 [--repart-offline]=1 [--overlay]='?' [--use-subvolumes]='?' [--seed]=1 [--clean-script]=1 [--package]=1 [--build-package]=1 [--volatile-package]=1 [--package-directory]=1 [--volatile-package-directory]=1 [--with-recommends]='?' [--with-docs]='?' [--base-tree]=1 [--skeleton-tree]=1 [--extra-tree]=1 [--remove-package]=1 [--remove-files]=1 [--clean-package-metadata]=1 [--source-date-epoch]=1 [--sync-script]=1 [--prepare-script]=1 [--build-script]=1 [--postinst-script]=1 [--finalize-script]=1 [--postoutput-script]=1 [--build-sources]=1 [--build-sources-ephemeral]=1 [--environment]=1 [--env-file]=1 [--without-tests]='?' [--with-network]='?' [--bootable]='?' [--bootloader]=1 [--bios-bootloader]=1 [--shim-bootloader]=1 [--unified-kernel-images]=1 [--unified-kernel-image-format]=1 [--initrd]=1 [--microcode-host]='?' [--initrd-package]=1 [--initrd-volatile-package]=1 [--kernel-command-line]=1 [--kernel-modules-include]=1 [--kernel-modules-include-host]=1 [--kernel-modules-exclude]=1 [--kernel-modules-initrd]='?' [--kernel-modules-initrd-include]=1 [--kernel-modules-initrd-include-host]=1 [--kernel-modules-initrd-exclude]=1 [--locale]=1 [--locale-messages]=1 [--keymap]=1 [--timezone]=1 [--hostname]=1 [--root-password]=1 [--root-shell]=1 [--autologin]='?' [--make-initrd]='?' [--ssh]='?' [--selinux-relabel]=1 [--secure-boot]='?' [--secure-boot-auto-enroll]=1 [--secure-boot-key]=1 [--secure-boot-key-source]=1 [--secure-boot-certificate]=1 [--secure-boot-sign-tool]=1 [--verity-key]=1 [--verity-key-source]=1 [--verity-certificate]=1 [--sign-expected-pcr]=1 [--passphrase]=1 [--checksum]='?' [--sign]='?' [--key]=1 [--proxy-url]=1 [--proxy-exclude]=1 [--proxy-peer-certificate]=1 [--proxy-client-certificate]=1 [--proxy-client-key]=1 [--incremental]='?' [--settings]=1 [--extra-search-path]=1 [--ephemeral]='?' [--credential]=1 [--kernel-command-line-extra]=1 [--acl]='?' [--tools-tree]='?' [--tools-tree-distribution]=1 [--tools-tree-release]=1 [--tools-tree-mirror]=1 [--tools-tree-repository]=1 [--tools-tree-package-manager-tree]=1 [--tools-tree-package]=1 [--tools-tree-certificates]=1 [--runtime-tree]=1 [--runtime-size]=1 [--runtime-scratch]=1 [--runtime-network]=1 [--runtime-build-sources]=1 [--unit-property]=1 [--ssh-key]=1 [--ssh-certificate]=1 [--vmm]=1 [--machine]=1 [--forward-journal]=1 [--qemu-gui]='?' [--qemu-smp]=1 [--qemu-mem]=1 [--qemu-kvm]='?' [--qemu-vsock]='?' [--qemu-vsock-cid]=1 [--qemu-swtpm]='?' [--qemu-cdrom]='?' [--qemu-firmware]=1 [--qemu-firmware-variables]=1 [--qemu-kernel]=1 [--qemu-drive]=1 [--qemu-args]=1)

    _mkosi_choices=([-d]='fedora debian ubuntu arch opensuse mageia centos rhel rhel-ubi openmandriva rocky alma custom ' [-t]='confext cpio directory disk esp none portable sysext tar uki oci ' [--doc-format]='auto markdown man pandoc system' [--distribution]='fedora debian ubuntu arch opensuse mageia centos rhel rhel-ubi openmandriva rocky alma custom ' [--architecture]='alpha arc arm arm64 ia64 loongarch64 mips-le mips64-le parisc ppc ppc64 ppc64-le riscv32 riscv64 s390 s390x tilegx x86 x86-64 ' [--cache-only]='always auto none metadata never ' [--format]='confext cpio directory disk esp none portable sysext tar uki oci ' [--bootloader]='none uki systemd-boot grub ' [--bios-bootloader]='none grub ' [--shim-bootloader]='none signed unsigned ' [--secure-boot-sign-tool]='auto sbsign pesign ' [--tools-tree-distribution]='fedora debian ubuntu arch opensuse mageia centos rhel rhel-ubi openmandriva rocky alma custom ' [--runtime-network]='interface user none ' [--vmm]='qemu vmspawn ' [--qemu-firmware]='auto linux uefi uefi-secure-boot bios ')

    _mkosi_compgen=([-C]=_mkosi_compgen_dirs [--directory]=_mkosi_compgen_dirs)

    _mkosi_verbs=(build clean summary shell boot qemu ssh serve bump help genkey documentation journalctl coredumpctl burn dependencies completion)

    # completing_program="$1"
    local completing_word="$2"
    local completing_word_preceding="$3"

    if [[ "$completing_word" =~ ^- ]]  # completing an option
    then
        readarray -t COMPREPLY < <(compgen -W "${_mkosi_options[*]}" -- "${completing_word}")

    elif [[ "$completing_word_preceding" =~ ^- ]]  # the previous word was an option
    then
        current_option="${completing_word_preceding}"
        current_option_nargs="${_mkosi_nargs[${current_option}]}"
        current_option_choices="${_mkosi_choices[${current_option}]}"
        current_option_compgen="${_mkosi_compgen[${current_option}]}"

        if [[ -n "${current_option_compgen}" ]]
        then
            readarray -t COMPREPLY < <("${current_option_compgen}" "${completing_word}")
        fi
        readarray -t COMPREPLY -O "${#COMPREPLY[@]}" \
                  < <(compgen -W "${current_option_choices}" -- "${completing_word}")

        if [[ "${current_option_nargs}" == "?" ]]
        then
            readarray -t COMPREPLY -O "${#COMPREPLY[@]}" \
                      < <(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}")
        fi
    else
        # the preceding word wasn't an option, so we are doing position
        # arguments now and all of them are verbs
        readarray -t COMPREPLY < <(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}")
    fi
}

complete -o filenames -F _mkosi_completion mkosi
complete -o filenames -F _mkosi_completion python -m mkosi

