# 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 -w -I -d -r -m -t -o -O -p -a -i -E -T --force --directory --debug --debug-shell --debug-workspace --debug-sandbox --no-pager --genkey-valid-days --genkey-common-name --auto-bump --doc-format --json --wipe-build-dir --include --profile --dependency --minimum-version --configure-script --pass-environment --distribution --release --architecture --mirror --local-mirror --repository-key-check --repository-key-fetch --repositories --format --manifest-format --output --compress-output --compress-level --output-directory --output-mode --image-version --image-id --split-artifacts --repart-directory --sector-size --overlay --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 --bootable --bootloader --bios-bootloader --shim-bootloader --unified-kernel-images --unified-kernel-image-format --uki-profile --initrd --microcode-host --initrd-package --initrd-volatile-package --devicetree --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 --machine-id --autologin --make-initrd --ssh --selinux-relabel --secure-boot --secure-boot-auto-enroll --secure-boot-key --secure-boot-key-source --secure-boot-certificate --secure-boot-certificate-source --secure-boot-sign-tool --verity --verity-key --verity-key-source --verity-certificate --verity-certificate-source --sign-expected-pcr --sign-expected-pcr-key --sign-expected-pcr-key-source --sign-expected-pcr-certificate --sign-expected-pcr-certificate-source --passphrase --checksum --sign --key --openpgp-tool --tools-tree --tools-tree-distribution --tools-tree-release --tools-tree-mirror --tools-tree-repository --tools-tree-sandbox-tree --tools-tree-package --tools-tree-package-directory --tools-tree-certificates --extra-search-path --incremental --cache-only --sandbox-tree --workspace-directory --cache-directory --package-cache-directory --build-directory --use-subvolumes --repart-offline --history --build-sources --build-sources-ephemeral --environment --env-file --without-tests --with-network --proxy-url --proxy-exclude --proxy-peer-certificate --proxy-client-certificate --proxy-client-key --settings --ephemeral --credential --kernel-command-line-extra --runtime-tree --runtime-size --runtime-scratch --runtime-network --runtime-build-sources --runtime-home --unit-property --ssh-key --ssh-certificate --vmm --machine --forward-journal --sysupdate-directory --console --cpus --ram --kvm --vsock --vsock-cid --tpm --cdrom --removable --firmware --firmware-variables --linux --drive --qemu-args --register)

    _mkosi_nargs=([-f]=0 [-C]=0 [-B]=0 [-w]=0 [-I]=1 [-d]=1 [-r]=1 [-m]=1 [-t]=1 [-o]=1 [-O]=1 [-p]=1 [-a]='?' [-i]='?' [-E]=1 [-T]='?' [--force]=0 [--directory]=0 [--debug]=0 [--debug-shell]=0 [--debug-workspace]=0 [--debug-sandbox]=0 [--no-pager]=0 [--genkey-valid-days]=0 [--genkey-common-name]=0 [--auto-bump]=0 [--doc-format]=0 [--json]=0 [--wipe-build-dir]=0 [--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]='?' [--repository-key-fetch]='?' [--repositories]=1 [--format]=1 [--manifest-format]=1 [--output]=1 [--compress-output]='?' [--compress-level]=1 [--output-directory]=1 [--output-mode]=1 [--image-version]=1 [--image-id]=1 [--split-artifacts]='?' [--repart-directory]=1 [--sector-size]=1 [--overlay]='?' [--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 [--bootable]='?' [--bootloader]=1 [--bios-bootloader]=1 [--shim-bootloader]=1 [--unified-kernel-images]=1 [--unified-kernel-image-format]=1 [--uki-profile]=1 [--initrd]=1 [--microcode-host]='?' [--initrd-package]=1 [--initrd-volatile-package]=1 [--devicetree]=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 [--machine-id]=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-certificate-source]=1 [--secure-boot-sign-tool]=1 [--verity]=1 [--verity-key]=1 [--verity-key-source]=1 [--verity-certificate]=1 [--verity-certificate-source]=1 [--sign-expected-pcr]=1 [--sign-expected-pcr-key]=1 [--sign-expected-pcr-key-source]=1 [--sign-expected-pcr-certificate]=1 [--sign-expected-pcr-certificate-source]=1 [--passphrase]=1 [--checksum]='?' [--sign]='?' [--key]=1 [--openpgp-tool]=1 [--tools-tree]='?' [--tools-tree-distribution]=1 [--tools-tree-release]=1 [--tools-tree-mirror]=1 [--tools-tree-repository]=1 [--tools-tree-sandbox-tree]=1 [--tools-tree-package]=1 [--tools-tree-package-directory]=1 [--tools-tree-certificates]=1 [--extra-search-path]=1 [--incremental]='?' [--cache-only]=1 [--sandbox-tree]=1 [--workspace-directory]=1 [--cache-directory]=1 [--package-cache-directory]=1 [--build-directory]=1 [--use-subvolumes]='?' [--repart-offline]=1 [--history]=1 [--build-sources]=1 [--build-sources-ephemeral]='?' [--environment]=1 [--env-file]=1 [--without-tests]='?' [--with-network]='?' [--proxy-url]=1 [--proxy-exclude]=1 [--proxy-peer-certificate]=1 [--proxy-client-certificate]=1 [--proxy-client-key]=1 [--settings]=1 [--ephemeral]='?' [--credential]=1 [--kernel-command-line-extra]=1 [--runtime-tree]=1 [--runtime-size]=1 [--runtime-scratch]=1 [--runtime-network]=1 [--runtime-build-sources]=1 [--runtime-home]=1 [--unit-property]=1 [--ssh-key]=1 [--ssh-certificate]=1 [--vmm]=1 [--machine]=1 [--forward-journal]=1 [--sysupdate-directory]=1 [--console]='?' [--cpus]=1 [--ram]=1 [--kvm]='?' [--vsock]='?' [--vsock-cid]=1 [--tpm]='?' [--cdrom]='?' [--removable]='?' [--firmware]=1 [--firmware-variables]=1 [--linux]=1 [--drive]=1 [--qemu-args]=1 [--register]=1)

    _mkosi_choices=([-d]='fedora debian kali ubuntu arch opensuse mageia centos rhel rhel-ubi openmandriva rocky alma azure custom ' [-t]='confext cpio directory disk esp none portable sysext tar uki oci addon ' [-i]='yes no strict' [--doc-format]='auto markdown man pandoc system' [--distribution]='fedora debian kali ubuntu arch opensuse mageia centos rhel rhel-ubi openmandriva rocky alma azure 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 ' [--format]='confext cpio directory disk esp none portable sysext tar uki oci addon ' [--bootloader]='none uki systemd-boot grub uki-signed systemd-boot-signed grub-signed ' [--bios-bootloader]='none grub ' [--shim-bootloader]='none signed unsigned ' [--secure-boot-sign-tool]='auto sbsign systemd-sbsign ' [--tools-tree-distribution]='fedora debian kali ubuntu arch opensuse mageia centos rhel rhel-ubi openmandriva rocky alma azure custom ' [--incremental]='yes no strict' [--cache-only]='always auto none metadata never ' [--build-sources-ephemeral]='yes no buildcache' [--runtime-network]='interface user none ' [--vmm]='qemu vmspawn ' [--firmware]='auto linux uefi uefi-secure-boot bios ')

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

    _mkosi_verbs=(build clean summary cat-config shell boot vm qemu ssh serve bump help genkey documentation journalctl coredumpctl burn dependencies completion sysupdate sandbox)

    # 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

