#compdef lsinitcpio mkinitcpio

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

_detect_kver() {
    local __ kver_validator='^[[:digit:]]+(\.[[:digit:]]+)+' offset
    offset="$(od -An -j0x20E -dN2 "$1")" || return
    read kver __ < \
        <(dd if="$1" bs=1 count=127 skip=$(( offset + 0x200 )) 2>/dev/null)
    [[ "$kver" =~ $kver_validator ]] && printf "$kver"
}

_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=(/usr/lib/initcpio/install /lib/initcpio/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]' \
            ':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: /etc/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]' \
            '(-P --allpresets)'{-P,--allpresets}'[Process all presets in /etc/mkinitcpio.d]' \
            '(-p --preset)'{-p,--preset}'[Build specified preset from /etc/mkinitcpio.d]:presets:_files -g "*(\:r)" -W /etc/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' \
            '--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' \
        ;;
    *)
        return 1;
        ;;
esac

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