# bash completion for aaru5                                     -*- shell-script -*-
# shellcheck shell=bash
#
# GENERATED by scripts/completions/gen.py from aaru-dps/Aaru at v5.4.2.
# Do not edit: change the generator, then run `scripts/completions/gen.py write`.
#
# aaru5 parses with System.CommandLine 2.0.0-beta1.20104.2. --verbose and
# --debug work before and after the command; --pause only before it (after it
# aaru5 parses it and does not act on it), so it is offered there only. A
# global option given at two levels makes System.CommandLine throw, so one
# that is already on the line is not offered again. A flag that is on by
# default (27 of them) is turned off with `false`, which is offered after it.
#
# Not offered: the options aaru5 parses but does not pass to the command, so
# they do nothing (image convert --drive-revision, image convert
# --drive-serial, image convert --media-lastsequence, image convert
# --media-serial, image create-sidecar --encoding, image decode --start); the
# values of --encoding, --format, --namespace (aaru5 builds those lists at run
# time; see aaru5 formats, list-encodings and list-namespaces); the help
# aliases -?, /h, /?, the directives [parse], [suggest], [debug],
# [env:NAME=VALUE], response files (@file), bundled short options (-cv) and
# the --option:value spelling.

# _mediapres_aaru5_child <node> <word>: the subcommand the word names, in REPLY
_mediapres_aaru5_child()
{
    case $1/$2 in
        /database|/db) REPLY=database ;;
        /device|/dev) REPLY=device ;;
        /filesystem|/fi|/fs) REPLY=filesystem ;;
        /image|/i) REPLY=image ;;
        /media|/m) REPLY=media ;;
        /configure) REPLY=configure ;;
        /formats) REPLY=formats ;;
        /list-encodings) REPLY=list-encodings ;;
        /list-namespaces) REPLY=list-namespaces ;;
        /remote) REPLY=remote ;;
        database/stats) REPLY='database stats' ;;
        database/update) REPLY='database update' ;;
        device/info) REPLY='device info' ;;
        device/report) REPLY='device report' ;;
        device/list) REPLY='device list' ;;
        filesystem/info) REPLY='filesystem info' ;;
        filesystem/options) REPLY='filesystem options' ;;
        filesystem/extract) REPLY='filesystem extract' ;;
        filesystem/list|filesystem/ls) REPLY='filesystem list' ;;
        image/checksum|image/chk) REPLY='image checksum' ;;
        image/compare|image/cmp) REPLY='image compare' ;;
        image/convert) REPLY='image convert' ;;
        image/create-sidecar) REPLY='image create-sidecar' ;;
        image/decode) REPLY='image decode' ;;
        image/entropy) REPLY='image entropy' ;;
        image/info) REPLY='image info' ;;
        image/options) REPLY='image options' ;;
        image/print) REPLY='image print' ;;
        image/verify) REPLY='image verify' ;;
        media/dump) REPLY='media dump' ;;
        media/info) REPLY='media info' ;;
        media/scan) REPLY='media scan' ;;
        *) REPLY= ;;
    esac
}

# _mediapres_aaru5_node <node>: what it takes (subcommands, the options offered, the
# options that take a value, the flags that are on by default, the kinds of
# its arguments)
_mediapres_aaru5_node()
{
    subs='' opts='' vopts='' tflags='' args=''
    case $1 in
    database)
        subs='stats update'
        opts='--verbose -v --debug -d -h --help'
        ;;
    'database stats')
        opts='--verbose -v --debug -d -h --help'
        ;;
    'database update')
        opts='--clear --clear-all --verbose -v --debug -d -h --help'
        ;;
    device)
        subs='info report list'
        opts='--verbose -v --debug -d -h --help'
        ;;
    'device info')
        opts='--output-prefix -w --verbose -v --debug -d -h --help'
        vopts='--output-prefix -w'
        args='device'
        ;;
    'device report')
        opts='--trap-disc -t --verbose -v --debug -d -h --help'
        args='device'
        ;;
    'device list')
        opts='--verbose -v --debug -d -h --help'
        args='host'
        ;;
    filesystem)
        subs='info options extract list ls'
        opts='--verbose -v --debug -d -h --help'
        ;;
    'filesystem info')
        opts='--encoding -e --filesystems -f --partitions -p --verbose -v --debug -d -h --help'
        vopts='--encoding -e'
        tflags='--filesystems -f --partitions -p'
        args='file'
        ;;
    'filesystem options')
        opts='--verbose -v --debug -d -h --help'
        ;;
    'filesystem extract')
        opts='--encoding -e --options -O --xattrs -x --namespace -n --verbose -v --debug -d -h
              --help'
        vopts='--encoding -e --options -O --namespace -n'
        args='file dir'
        ;;
    'filesystem list')
        opts='--encoding -e --long-format -l --options -O --namespace -n --verbose -v --debug -d -h
              --help'
        vopts='--encoding -e --options -O --namespace -n'
        tflags='--long-format -l'
        args='file'
        ;;
    image)
        subs='checksum chk compare cmp convert create-sidecar decode entropy info options print
              verify'
        opts='--verbose -v --debug -d -h --help'
        ;;
    'image checksum')
        opts='--adler32 -a --crc16 --crc32 -c --crc64 --fletcher16 --fletcher32 --md5 -m
              --separated-tracks -t --sha1 -s --sha256 --sha384 --sha512 --spamsum -f --whole-disc
              -w --verbose -v --debug -d -h --help'
        tflags='--crc16 --crc32 -c --crc64 --md5 -m --separated-tracks -t --sha1 -s --sha512
                --spamsum -f --whole-disc -w'
        args='file'
        ;;
    'image compare')
        opts='--verbose -v --debug -d -h --help'
        args='file file'
        ;;
    'image convert')
        opts='--cicm-xml -x --comments --count -c --creator --drive-manufacturer --drive-model
              --force -f --format -p --media-barcode --media-manufacturer --media-model
              --media-partnumber --media-sequence --media-title --options -O --resume-file -r
              --geometry -g --fix-subchannel-position --fix-subchannel --fix-subchannel-crc
              --generate-subchannels --verbose -v --debug -d -h --help'
        vopts='--cicm-xml -x --comments --count -c --creator --drive-manufacturer --drive-model
               --drive-revision --drive-serial --format -p --media-barcode --media-lastsequence
               --media-manufacturer --media-model --media-partnumber --media-sequence --media-serial
               --media-title --options -O --resume-file -r --geometry -g'
        tflags=--fix-subchannel-position
        args='file file'
        ;;
    'image create-sidecar')
        opts='--block-size -b --tape -t --verbose -v --debug -d -h --help'
        vopts='--block-size -b --encoding -e'
        args='file'
        ;;
    'image decode')
        opts='--disk-tags -f --length -l --sector-tags -p --verbose -v --debug -d -h --help'
        vopts='--length -l --start -s'
        tflags='--disk-tags -f --sector-tags -p'
        args='file'
        ;;
    'image entropy')
        opts='--duplicated-sectors -p --separated-tracks -t --whole-disc -w --verbose -v --debug -d
              -h --help'
        tflags='--duplicated-sectors -p --separated-tracks -t --whole-disc -w'
        args='file'
        ;;
    'image info')
        opts='--verbose -v --debug -d -h --help'
        args='file'
        ;;
    'image options')
        opts='--verbose -v --debug -d -h --help'
        ;;
    'image print')
        opts='--length -l --long-sectors -r --start -s --width -w --verbose -v --debug -d -h --help'
        vopts='--length -l --start -s --width -w'
        args='file'
        ;;
    'image verify')
        opts='--verify-disc -w --verify-sectors -s --verbose -v --debug -d -h --help'
        tflags='--verify-disc -w --verify-sectors -s'
        args='file'
        ;;
    media)
        subs='dump info scan'
        opts='--verbose -v --debug -d -h --help'
        ;;
    'media dump')
        opts='--cicm-xml -x --encoding -e --first-pregap --fix-offset --force -f --format -t
              --metadata --trim --options -O --persistent --resume -r --retry-passes -p --skip -k
              --stop-on-error -s --subchannel --speed --private --fix-subchannel-position
              --retry-subchannel --fix-subchannel --fix-subchannel-crc --generate-subchannels
              --skip-cdiready-hole --eject --max-blocks --use-buffered-reads --store-encrypted
              --title-keys --verbose -v --debug -d -h --help'
        vopts='--cicm-xml -x --encoding -e --format -t --options -O --retry-passes -p --skip -k
               --subchannel --speed --max-blocks'
        tflags='--fix-offset --metadata --trim --resume -r --fix-subchannel-position
                --retry-subchannel --skip-cdiready-hole --use-buffered-reads --store-encrypted
                --title-keys'
        args='device file'
        ;;
    'media info')
        opts='--output-prefix -w --verbose -v --debug -d -h --help'
        vopts='--output-prefix -w'
        args='device'
        ;;
    'media scan')
        opts='--mhdd-log -m --ibg-log -b --use-buffered-reads --verbose -v --debug -d -h --help'
        vopts='--mhdd-log -m --ibg-log -b'
        tflags=--use-buffered-reads
        args='device'
        ;;
    configure)
        opts='--verbose -v --debug -d -h --help'
        ;;
    formats)
        opts='--verbose -v --debug -d -h --help'
        ;;
    list-encodings)
        opts='--verbose -v --debug -d -h --help'
        ;;
    list-namespaces)
        opts='--verbose -v --debug -d -h --help'
        ;;
    remote)
        opts='--verbose -v --debug -d -h --help'
        args='host'
        ;;
    '')
        subs='database db device dev filesystem fi fs image i media m configure formats
              list-encodings list-namespaces remote'
        opts='--verbose -v --debug -d -h --help --pause --version'
        ;;
    esac
}

# _mediapres_aaru5_value <node> <option>: complete the option's value; numbers, free
# text and the lists aaru5 only knows at run time get nothing
_mediapres_aaru5_value()
{
    case $1/$2 in
        'device info/--output-prefix'|'device info/-w'|'image convert/--cicm-xml'|'image convert/-x'|'image convert/--resume-file'|'image convert/-r'|'media dump/--cicm-xml'|'media dump/-x'|'media info/--output-prefix'|'media info/-w'|'media scan/--mhdd-log'|'media scan/-m'|'media scan/--ibg-log'|'media scan/-b')
            _filedir
            ;;
        'image decode/--length'|'image decode/-l')
            mapfile -t COMPREPLY < <(compgen -W all -- "${cur,,}")
            ;;
        'media dump/--subchannel')
            mapfile -t COMPREPLY < <(compgen -W 'any rw rw-or-pq pq none' -- "${cur,,}")
            ;;
    esac
}

# _mediapres_aaru5_global <option>: all names of the global option, in REPLY
_mediapres_aaru5_global()
{
    case $1 in
        --verbose|-v) REPLY='--verbose -v' ;;
        --debug|-d) REPLY='--debug -d' ;;
        --pause) REPLY=--pause ;;
        *) return 1 ;;
    esac
}

_mediapres_aaru5_devices()
{
    local d
    COMPREPLY=()
    for d in /dev/mmcblk* /dev/sd* /dev/sg* /dev/sr* /dev/st*; do
        [[ -e $d && $d == "$cur"* ]] && COMPREPLY+=("$d")
    done
    ((${#COMPREPLY[@]})) || _filedir
}

_mediapres_aaru5()
{
    local cur prev words cword split
    _init_completion -s -n : || return

    local node='' skip='' last='' used=' ' w lw c i j dirs=0 REPLY subs opts vopts tflags args
    local -a pos=() kinds=() offer=()
    for ((i = 1; i < cword; i++)); do
        w=${words[i]}
        if [[ $skip == value ]]; then
            skip=''
            continue
        fi
        lw=${w,,}
        if [[ $skip == flag ]]; then
            skip=''
            [[ $lw == true || $lw == false ]] && continue
        fi
        # directives ([parse] ...) come first
        if ((i == dirs + 1)) && [[ $w == \[*\] ]]; then
            dirs=$i
            continue
        fi
        case $w in
            --)
                # aaru5 ignores what follows
                return
                ;;
            @?*)
                # a response file: what it holds is unknown
                return
                ;;
            --*)
                _mediapres_aaru5_global "${w%%[=:]*}" && used+="$REPLY "
                [[ $w == *[=:]* ]] && continue
                _mediapres_aaru5_node "$node"
                last=$w
                if [[ " $vopts " == *[[:space:]]"$w"[[:space:]]* ]]; then skip=value; else skip=flag; fi
                ;;
            -?*)
                # short options, maybe bundled: the first that takes a value
                # takes the rest of the word, or the next word
                _mediapres_aaru5_node "$node"
                skip=flag
                for ((j = 1; j < ${#w}; j++)); do
                    c=-${w:j:1}
                    last=$c
                    _mediapres_aaru5_global "$c" && used+="$REPLY "
                    if [[ " $vopts " == *[[:space:]]"$c"[[:space:]]* ]]; then
                        if ((j == ${#w} - 1)); then skip=value; else skip=''; fi
                        break
                    fi
                done
                ;;
            *)
                _mediapres_aaru5_node "$node"
                if [[ $subs ]]; then
                    _mediapres_aaru5_child "$node" "$w"
                    [[ $REPLY ]] || return
                    node=$REPLY
                else
                    pos+=("$w")
                fi
                ;;
        esac
    done

    _mediapres_aaru5_node "$node"
    if [[ $skip == value ]]; then
        _mediapres_aaru5_value "$node" "$last"
        return
    fi
    if $split; then
        if [[ " $vopts " == *[[:space:]]"$prev"[[:space:]]* ]]; then
            _mediapres_aaru5_value "$node" "$prev"
        else
            mapfile -t COMPREPLY < <(compgen -W 'false true' -- "${cur,,}")
        fi
        return
    fi
    if [[ $skip == flag && " $tflags " == *[[:space:]]"$last"[[:space:]]* && $cur != -* ]] &&
        [[ false == "${cur,,}"* || true == "${cur,,}"* ]]; then
        mapfile -t COMPREPLY < <(compgen -W 'false true' -- "${cur,,}")
        return
    fi
    if [[ $cur == -* ]]; then
        read -rd '' -a kinds <<<"$opts"
        for w in "${kinds[@]}"; do
            [[ $used == *[[:space:]]"$w"[[:space:]]* ]] || offer+=("$w")
        done
        mapfile -t COMPREPLY < <(compgen -W "${offer[*]}" -- "$cur")
        return
    fi
    if [[ $subs ]]; then
        mapfile -t COMPREPLY < <(compgen -W "$subs" -- "$cur")
        return
    fi
    read -ra kinds <<<"$args"
    case ${kinds[${#pos[@]}]-} in
        file) _filedir ;;
        dir) _filedir -d ;;
        device) _mediapres_aaru5_devices ;;
        host) _known_hosts_real -- "$cur" ;;
    esac
} &&
    complete -F _mediapres_aaru5 aaru5

# ex: filetype=sh
