# SPDX-License-Identifier: Unlicense

complete -c s6-rc-db -f
set -l comp complete -c s6-rc-db -n __fish_use_subcommand

$comp -s b -d 'blocking lock'
$comp -s c -r -F -d 'compiled dataspace path'
$comp -s d -d 'show data for bring services down'
$comp -s l -r -F -d 'live dataspace path'
$comp -s u -d 'show data for bring services up'

$comp -a help -d 'show help'
$comp -a check -d 'check the database consistency'
$comp -a list -d 'list all services of a given type'
$comp -a type -d 'print the type of a service'
$comp -a timeout -d 'print the timeout of a service'
$comp -a contents -d 'list atomic services in bundle'
$comp -a dependencies -d 'list direct dependencies'
$comp -a pipeline -d 'print pipeline a services is part of'
$comp -a script -d 'print up script for service'
$comp -a flags -d 'print flags for service'
$comp -a atomics -d 'print atomics contained within service'
$comp -a all-dependencies -d 'print all atomics needed by service'

complete -c s6-rc-db -n "__fish_seen_subcommand_from list" -a "all bundles longruns oneshots services"
complete -c s6-rc-db -n "__fish_seen_subcommand_from type" -r -a '(s6-rc-db (__fish_s6_db_args) list all 2>/dev/null)'
complete -c s6-rc-db -n "__fish_seen_subcommand_from contents" -r -a '(s6-rc-db (__fish_s6_db_args) list bundles 2>/dev/null)'
complete -c s6-rc-db -n "__fish_seen_subcommand_from dependencies" -r -a '(s6-rc-db (__fish_s6_db_args) list services 2>/dev/null)'
complete -c s6-rc-db -n "__fish_seen_subcommand_from pipeline" -r -a '(s6-rc-db (__fish_s6_db_args) list longruns 2>/dev/null)'
complete -c s6-rc-db -n "__fish_seen_subcommand_from script" -r -a '(s6-rc-db (__fish_s6_db_args) list oneshots 2>/dev/null)'
complete -c s6-rc-db -n "__fish_seen_subcommand_from flags" -r -a '(s6-rc-db (__fish_s6_db_args) list services 2>/dev/null)'
complete -c s6-rc-db -n "__fish_seen_subcommand_from atomics" -r -a '(s6-rc-db (__fish_s6_db_args) list all 2>/dev/null)'
complete -c s6-rc-db -n "__fish_seen_subcommand_from all-dependencies" -r -a '(s6-rc-db (__fish_s6_db_args) list all 2>/dev/null)'

function __fish_s6_db_args
    set -l tokens (commandline -xpc)
    set -e tokens[1]
    argparse -us 'c=' 'l=' -- $tokens
    or return 0
    set -q _flag_l
    and echo -- -l$_flag_l
    set -q _flag_c
    and echo -- -c$_flag_c
end
