#compdef lsinitcpio mkinitcpio

# SPDX-License-Identifier: GPL-2.0-only

_detect_kver() {
    if (( MESON_TEST )); then
        './functions' run_mkinitcpio_func kver "$1" 2>/dev/null
    else
        '@INITCPIO_LIBDIR@/functions' run_mkinitcpio_func kver "$1" 2>/dev/null
    fi
}

_find_kernel_versions() {
    local -a version
    local filedata

    for f in /boot/*; do
        # only match regular files which pass validation
        if [[ ! -L $f && -f $f ]] && kver=$(_detect_kver "$f"); then
            version+=("$kver" "$f")
        fi
    done

    compadd ${(u)version}
}

local -a hookdirs=("@INITCPIO_LIBDIR@/install")

case $service in
    lsinitcpio)
        _arguments : \
            '(-a --analyze)'{-a,--analyze}'[analyze contents of image]' \
            '(-c --config)'{-c,--config}'[show configuration file image was built with]' \
            '(-l --list)'{-l,--list}'[list contents of the image (default)]' \
            '(-x --extract)'{-x,--extract}'[extract image to disk]' \
            '(-h --help)'{-h,--help}'[display this help]' \
            '(-n --nocolor)'{-n,--nocolor}'[disable colorized output]' \
            '(-V --version)'{-V,--version}'[display version information]' \
            '(-v --verbose)'{-v,--verbose}'[more verbose output]' \
            '--cpio[list or extract only the main CPIO image]' \
            '--early[list or extract only the early CPIO image]' \
            ':image file:_files'
        ;;
    mkinitcpio)
        _arguments : \
            '(-A --addhooks)'{-A,--addhooks}'[Add specified hooks, comma separated, to image]:hook(s):_sequence -s , _path_files -W hookdirs' \
            '(-c --config)'{-c,--config}'[Use alternate config file (default: @SYSCONFDIR@/mkinitcpio.conf)]:config file:_files' \
            '(-D --hookdir)'{-D,--hookdir}'[Specify where to look for hooks]:directory:_files -/' \
            '(-d --generatedir)'{-d,--generatedir}'[Specify where to build the initramfs]:directory:_files -/' \
            '(-g --generate)'{-g,--generate}'[Generate cpio image and write to specified path]:output filename:_files' \
            '(-H --hookhelp)'{-H,--hookhelp}'[Display help for given hook and exit]:hook:_path_files -W hookdirs' \
            '(-h --help)'{-h,--help}'[Display this message and exit]' \
            '(-k --kernel)'{-k,--kernel}'[Use specified kernel version]:kernel version:_find_kernel_versions' \
            '(-L --listhooks)'{-L,--listhooks}'[List all available hooks]' \
            '(-M --automods)'{-M,--automods}'[Display modules found via autodetection]' \
            '(-n --nocolor)'{-n,--nocolor}'[Disable colorized output messages]' \
            '--nopost[Disable post hooks]' \
            '(-P --allpresets)'{-P,--allpresets}'[Process all presets in @SYSCONFDIR@/mkinitcpio.d]' \
            '(-p --preset)'{-p,--preset}'[Build specified preset from @SYSCONFDIR@/mkinitcpio.d]:presets:_files -g "*(\:r)" -W @SYSCONFDIR@/mkinitcpio.d' \
            '--remove[Remove specified preset images]' \
            '(-r --moduleroot)'{-r,--moduleroot}'[Root directory for modules (default: /)]:directory:_files -/' \
            '(-S --skiphooks)'{-S,--skiphooks}'[Skip specified hooks, comma-separated, during build]:hook(s):_sequence -s , _path_files -W hookdirs' \
            '(-s --save)'{-s,--save}'[Save build directory (default: no)]' \
            '(-t --builddir)'{-t,--builddir}'[Use DIR as the temporary build directory]:directory:_files -/' \
            '(-V --version)'{-V,--version}'[Display version information and exit]' \
            '(-v --verbose)'{-v,--verbose}'[Verbose output (default: no)]' \
            '(-z --compress)'{-z,--compress}'[Use an alternate compressor on the image]' \
            '(-U --uki)'{-U,--uki}'[Build a unified kernel image]:path:_files' \
            '--cmdline[Specify kernel cmdline for the unified kernel image]:path:_files' \
            '--no-cmdline[Do not embed a cmdline in the unified kernel image]' \
            '--kernelimage[Include a kernel image for the unified kernel image]:path:_files' \
            '--osrelease[Include a os-release file for the unified kernel image]:path:_files' \
            '--splash[Bitmap splash image to include in unified kernel image]:path:_files' \
            '--uefistub[UEFI stub image used for unified kernel image generation]:path:_files' \
            '--ukiconfig[Config file for ukify for unified kernel image generation]:path:_files' \
            '--no-ukify[Do not use ukify for unified kernel image generation]:path:_files' \
        ;;
    *)
        return 1;
        ;;
esac

# vim: set et ts=4 sw=4 ft=zsh:
