# bash completion for aaru                                      -*- shell-script -*-
# shellcheck shell=bash
#
# GENERATED by scripts/completions/gen.py from aaru-dps/Aaru at v6.0.0-beta.2.
# Do not edit: change the generator, then run `scripts/completions/gen.py write`.
#
# aaru parses with Spectre.Console.Cli 0.55.0. Offered although the help text
# does not show them: `gui` (only as the sole argument) and the `cli` branch
# (version, xmldoc, explain, opencli).
#
# -v, -d, --logfile and --pause of a branch go between the branch and its
# subcommand (`aaru image -d convert ...`); after the subcommand aaru drops
# them. A flag that is on by default (30 of them) is turned off with `false`,
# which is offered after it.
#
# Not offered: the values of --encoding, --format, --namespace (aaru builds
# those lists at run time; see aaru formats, list-encodings and
# list-namespaces), the help alias -?, the root's -V and --VERSION, and the
# --option:value spelling.

# _mediapres_aaru_child <node> <word>: the subcommand the word names, in REPLY
_mediapres_aaru_child()
{
    case $1/$2 in
        /archive|/arc) REPLY=archive ;;
        /device|/dev) REPLY=device ;;
        /filesystem|/fs|/fi) REPLY=filesystem ;;
        /image|/i|/img) REPLY=image ;;
        /media|/m) REPLY=media ;;
        /database|/db) REPLY=database ;;
        /configure|/cfg) REPLY=configure ;;
        /formats|/fmt) REPLY=formats ;;
        /list-encodings|/le) REPLY=list-encodings ;;
        /list-namespaces|/ln) REPLY=list-namespaces ;;
        /remote|/rem) REPLY=remote ;;
        /metadata-schema) REPLY=metadata-schema ;;
        /cli) REPLY=cli ;;
        archive/list|archive/l|archive/ls) REPLY='archive list' ;;
        archive/extract|archive/x) REPLY='archive extract' ;;
        archive/info|archive/i) REPLY='archive info' ;;
        device/report) REPLY='device report' ;;
        device/info|device/i) REPLY='device info' ;;
        device/list|device/l|device/ls) REPLY='device list' ;;
        filesystem/extract|filesystem/x) REPLY='filesystem extract' ;;
        filesystem/info|filesystem/i) REPLY='filesystem info' ;;
        filesystem/list|filesystem/ls) REPLY='filesystem list' ;;
        filesystem/options) REPLY='filesystem options' ;;
        image/analyze) REPLY='image analyze' ;;
        image/checksum|image/chk) REPLY='image checksum' ;;
        image/compare|image/cmp) REPLY='image compare' ;;
        image/convert|image/cvt) REPLY='image convert' ;;
        image/create-sidecar|image/cs) REPLY='image create-sidecar' ;;
        image/decode) REPLY='image decode' ;;
        image/entropy) REPLY='image entropy' ;;
        image/info|image/i) REPLY='image info' ;;
        image/options) REPLY='image options' ;;
        image/print-hex|image/ph) REPLY='image print-hex' ;;
        image/verify|image/v) REPLY='image verify' ;;
        image/merge) REPLY='image merge' ;;
        image/write-metadata) REPLY='image write-metadata' ;;
        media/info|media/i) REPLY='media info' ;;
        media/scan|media/s) REPLY='media scan' ;;
        media/dump|media/d) REPLY='media dump' ;;
        media/dpm) REPLY='media dpm' ;;
        database/stats) REPLY='database stats' ;;
        database/update) REPLY='database update' ;;
        cli/version) REPLY='cli version' ;;
        cli/xmldoc) REPLY='cli xmldoc' ;;
        cli/explain) REPLY='cli explain' ;;
        cli/opencli) REPLY='cli opencli' ;;
        *) REPLY= ;;
    esac
}

# _mediapres_aaru_node <node>: what it takes (subcommands, options, the options with a
# value, the flags that are on by default, the kinds of its arguments)
_mediapres_aaru_node()
{
    subs='' opts='' vopts='' tflags='' args=''
    case $1 in
    '')
        subs='archive arc device dev filesystem fs fi image i img media m database db configure cfg
              formats fmt list-encodings le list-namespaces ln remote rem metadata-schema cli'
        opts='-h --help -v --version'
        ;;
    archive)
        subs='list l ls extract x info i'
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    'archive list')
        opts='-e --encoding -l --long-format -h --help'
        vopts='-e --encoding'
        args='file'
        ;;
    'archive extract')
        opts='-e --encoding -x --xattrs -h --help'
        vopts='-e --encoding'
        args='file dir'
        ;;
    'archive info')
        opts='-e --encoding -h --help'
        vopts='-e --encoding'
        args='file'
        ;;
    device)
        subs='report info i list l ls'
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    'device report')
        opts='-h --help'
        args='device'
        ;;
    'device info')
        opts='-w --output-prefix -h --help'
        vopts='-w --output-prefix'
        args='device'
        ;;
    'device list')
        opts='-h --help'
        args='host'
        ;;
    filesystem)
        subs='extract x info i list ls options'
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    'filesystem extract')
        opts='-e --encoding -O --options -x --xattrs --volume -n --namespace -h --help'
        vopts='-e --encoding -O --options --volume -n --namespace'
        args='file dir'
        ;;
    'filesystem info')
        opts='-e --encoding -p --partitions -f --filesystems -h --help'
        vopts='-e --encoding'
        tflags='-p --partitions -f --filesystems'
        args='file'
        ;;
    'filesystem list')
        opts='-e --encoding -O --options -n --namespace --volume -h --help'
        vopts='-e --encoding -O --options -n --namespace --volume'
        args='file'
        ;;
    'filesystem options')
        opts='-h --help'
        ;;
    image)
        subs='analyze checksum chk compare cmp convert cvt create-sidecar cs decode entropy info i
              options print-hex ph verify v merge write-metadata'
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    'image analyze')
        opts='-e --encoding -O --options -n --namespace --ignore-subchannel --ignore-subchannels -r
              --resume-file -h --help'
        vopts='-e --encoding -O --options -n --namespace -r --resume-file'
        args='file'
        ;;
    'image checksum')
        opts='-a --adler32 --crc16 -c --crc32 --crc64 --fletcher16 --fletcher32 -r --long-sectors -m
              --md5 -s --sha1 --sha256 --sha384 --sha512 -f --spamsum -w --whole-disc -t
              --separated-tracks -h --help'
        tflags='--crc16 -c --crc32 --crc64 -m --md5 -s --sha1 --sha512 -f --spamsum -w --whole-disc
                -t --separated-tracks'
        args='file'
        ;;
    'image compare')
        opts='-r --long-sectors -h --help'
        args='file file'
        ;;
    'image convert')
        opts='-x --cicm-xml --comments -c --count --creator --drive-manufacturer --drive-model
              --drive-revision --drive-serial -f --force -p --format --media-barcode
              --media-lastsequence --media-manufacturer --media-model --media-partnumber
              --media-sequence --media-serial --media-title -O --options -r --resume-file -g
              --geometry --fix-subchannel-position --fix-subchannel --fix-subchannel-crc
              --generate-subchannels --decrypt --bypass-ps3-decryption --bypass-wiiu-decryption
              --bypass-wii-decryption -m --aaru-metadata --ignore-negative-sectors
              --ignore-overflow-sectors --error-recovery -h --help'
        vopts='-x --cicm-xml --comments -c --count --creator --drive-manufacturer --drive-model
               --drive-revision --drive-serial -p --format --media-barcode --media-lastsequence
               --media-manufacturer --media-model --media-partnumber --media-sequence --media-serial
               --media-title -O --options -r --resume-file -g --geometry -m --aaru-metadata
               --error-recovery'
        tflags=--fix-subchannel-position
        args='file file'
        ;;
    'image create-sidecar')
        opts='-b --block-size -e --encoding -t --tape --enable-spamsum -h --help'
        vopts='-b --block-size -e --encoding'
        args='file'
        ;;
    'image decode')
        opts='-f --disk-tags -l --length -p --sector-tags -s --start -h --help'
        vopts='-l --length -s --start'
        tflags='-f --disk-tags -p --sector-tags'
        args='file'
        ;;
    'image entropy')
        opts='-p --duplicated-sectors -t --separated-tracks -w --whole-disc -h --help'
        tflags='-p --duplicated-sectors -t --separated-tracks -w --whole-disc'
        args='file'
        ;;
    'image info')
        opts='-h --help'
        args='file'
        ;;
    'image options')
        opts='-h --help'
        ;;
    'image print-hex')
        opts='-l --length -r --long-sectors -s --start -w --width -h --help'
        vopts='-l --length -s --start -w --width'
        args='file'
        ;;
    'image verify')
        opts='-w --verify-disc -s --verify-sectors -g --create-graph -d --dimensions -t --data-only
              -h --help'
        vopts='-d --dimensions'
        tflags='-w --verify-disc -s --verify-sectors -g --create-graph -t --data-only'
        args='file'
        ;;
    'image merge')
        opts='--secondary-tags --sectors-file --ignore-media-type --ignore-sector-count --comments
              -c --count --creator --drive-manufacturer --drive-model --drive-revision
              --drive-serial -p --format --media-barcode --media-lastsequence --media-manufacturer
              --media-model --media-partnumber --media-sequence --media-serial --media-title -O
              --options --primary-resume --secondary-resume -g --geometry --fix-subchannel-position
              --fix-subchannel --fix-subchannel-crc --generate-subchannels --decrypt
              --ignore-negative-sectors --ignore-overflow-sectors --ignore-sector-not-found
              --error-recovery -h --help'
        vopts='--sectors-file --comments -c --count --creator --drive-manufacturer --drive-model
               --drive-revision --drive-serial -p --format --media-barcode --media-lastsequence
               --media-manufacturer --media-model --media-partnumber --media-sequence --media-serial
               --media-title -O --options --primary-resume --secondary-resume -g --geometry
               --error-recovery'
        tflags=--fix-subchannel-position
        args='file file file'
        ;;
    'image write-metadata')
        opts='--comments --creator --drive-manufacturer --drive-model --drive-revision
              --drive-serial --media-barcode --media-lastsequence --media-manufacturer --media-model
              --media-partnumber --media-sequence --media-serial --media-title -h --help'
        vopts='--comments --creator --drive-manufacturer --drive-model --drive-revision
               --drive-serial --media-barcode --media-lastsequence --media-manufacturer
               --media-model --media-partnumber --media-sequence --media-serial --media-title'
        args='file'
        ;;
    media)
        subs='info i scan s dump d dpm'
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    'media info')
        opts='-w --output-prefix -h --help'
        vopts='-w --output-prefix'
        args='device'
        ;;
    'media scan')
        opts='-m --mhdd-log -b --ibg-log --use-buffered-reads -h --help'
        vopts='-m --mhdd-log -b --ibg-log'
        tflags=--use-buffered-reads
        args='device'
        ;;
    'media dump')
        opts='-x --cicm-xml -e --encoding --first-pregap --fix-offset -f --force -t --format
              --metadata --trim -O --options --persistent -r --resume -p --retry-passes -k --skip -s
              --stop-on-error --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
              --bypass-wii-decryption --title-keys --ignore-cdr-runouts -g --create-graph
              --dimensions --aaru-metadata --paranoia --c2-repair --dpm --cure-paranoia --raw
              --start-reverse --error-recovery --hyper-speed --ludicrous-speed --lead-out
              --skip-safedisc -h --help'
        vopts='-x --cicm-xml -e --encoding -t --format -O --options -p --retry-passes -k --skip
               --subchannel --speed --max-blocks --ignore-cdr-runouts --dimensions --aaru-metadata
               --error-recovery'
        tflags='--fix-offset --metadata --trim -r --resume --fix-subchannel-position
                --retry-subchannel --skip-cdiready-hole --use-buffered-reads --store-encrypted
                --title-keys -g --create-graph --c2-repair --dpm'
        args='device file'
        ;;
    'media dpm')
        opts='-f --force --spacing --speed --start --end -h --help'
        vopts='--spacing --speed --start --end'
        args='device file'
        ;;
    database)
        subs='stats update'
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    'database stats')
        opts='-h --help'
        ;;
    'database update')
        opts='--clear --clear-all -h --help'
        ;;
    configure)
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    formats)
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    list-encodings)
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    list-namespaces)
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        ;;
    remote)
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        args='host'
        ;;
    metadata-schema)
        opts='-v --verbose -d --debug --logfile --pause -h --help'
        vopts=--logfile
        args='file'
        ;;
    cli)
        subs='version xmldoc explain opencli'
        opts='-h --help'
        ;;
    'cli version')
        opts='-h --help'
        ;;
    'cli xmldoc')
        opts='-h --help'
        ;;
    'cli explain')
        opts='-d --detailed --hidden -h --help'
        args='commands'
        ;;
    'cli opencli')
        opts='-h --help'
        ;;
    esac
}

# _mediapres_aaru_value <node> <option>: complete the option's value; numbers, free
# text and the lists aaru only knows at run time get nothing
_mediapres_aaru_value()
{
    case $1/$2 in
        archive/--logfile|device/--logfile|'device info/-w'|'device info/--output-prefix'|filesystem/--logfile|image/--logfile|'image analyze/-r'|'image analyze/--resume-file'|'image convert/-x'|'image convert/--cicm-xml'|'image convert/-r'|'image convert/--resume-file'|'image convert/-m'|'image convert/--aaru-metadata'|'image merge/--sectors-file'|'image merge/--primary-resume'|'image merge/--secondary-resume'|media/--logfile|'media info/-w'|'media info/--output-prefix'|'media scan/-m'|'media scan/--mhdd-log'|'media scan/-b'|'media scan/--ibg-log'|'media dump/-x'|'media dump/--cicm-xml'|'media dump/--aaru-metadata'|database/--logfile|configure/--logfile|formats/--logfile|list-encodings/--logfile|list-namespaces/--logfile|remote/--logfile|metadata-schema/--logfile)
            _filedir
            ;;
        'image decode/-l'|'image decode/--length')
            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_aaru_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_aaru()
{
    local cur prev words cword split
    _init_completion -s -n : || return

    local node='' skip='' w lw i REPLY subs opts vopts tflags args
    local -a pos=() kinds=()
    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
        case $w in
            --)
                return
                ;;
            -*)
                # before the first command: help, version or an error
                [[ -z $node ]] && return
                [[ $w == *=* ]] && continue
                _mediapres_aaru_node "$node"
                if [[ " $vopts " == *[[:space:]]"$w"[[:space:]]* ]]; then skip=value; else skip=flag; fi
                ;;
            *)
                [[ -z $node && $lw == gui ]] && return
                _mediapres_aaru_node "$node"
                if [[ $subs ]]; then
                    _mediapres_aaru_child "$node" "$w"
                    [[ $REPLY ]] || return
                    node=$REPLY
                else
                    pos+=("$w")
                fi
                ;;
        esac
    done

    _mediapres_aaru_node "$node"
    if [[ $skip == value ]] || { $split && [[ " $vopts " == *[[:space:]]"$prev"[[:space:]]* ]]; }; then
        _mediapres_aaru_value "$node" "$prev"
        return
    fi
    if $split || { [[ $skip == flag && " $tflags " == *[[:space:]]"$prev"[[:space:]]* && $cur != -* ]] &&
        [[ false == "${cur,,}"* || true == "${cur,,}"* ]]; }; then
        mapfile -t COMPREPLY < <(compgen -W 'false true' -- "${cur,,}")
        return
    fi
    if [[ $cur == -* ]]; then
        mapfile -t COMPREPLY < <(compgen -W "$opts" -- "$cur")
        return
    fi
    if [[ $subs ]]; then
        [[ -z $node ]] && ((cword == 1)) && subs+=" gui"
        mapfile -t COMPREPLY < <(compgen -W "$subs" -- "$cur")
        return
    fi
    read -ra kinds <<<"$args"
    w=${kinds[${#pos[@]}]-}
    ((${#kinds[@]})) && [[ -z $w && ${kinds[-1]} == commands ]] && w=commands
    case $w in
        file) _filedir ;;
        dir) _filedir -d ;;
        device) _mediapres_aaru_devices ;;
        host) _known_hosts_real -- "$cur" ;;
        commands)
            # a command path: aaru cli explain image convert
            node=''
            for w in "${pos[@]}"; do
                _mediapres_aaru_child "$node" "$w"
                [[ $REPLY ]] || return
                node=$REPLY
            done
            _mediapres_aaru_node "$node"
            mapfile -t COMPREPLY < <(compgen -W "$subs" -- "$cur")
            ;;
    esac
} &&
    complete -F _mediapres_aaru aaru

# ex: filetype=sh
