# Print an optspec for argparse to handle cmd's options that are independent of any subcommand.
function __fish_rqbit_global_optspecs
    string join \n v= log-file= log-file-rust-log= log-file-json i/tracker-refresh-interval= http-api-listen-addr= http-api-allow-create http-api-max-upload-size= enable-mdns s/single-thread-runtime disable-dht disable-dht-persistence dht-bootstrap-addrs= peer-connect-timeout= peer-read-write-timeout= peer-limit= t/worker-threads= disable-tcp-listen disable-tcp-connect experimental-enable-utp-listen listen-port= announce-port= listen-ip= disable-upnp-port-forward enable-upnp-server upnp-server-friendly-name= bind-device= ipv4-only max-blocking-threads= socks-url= concurrent-init-limit= umask= ratelimit-download= ratelimit-upload= blocklist-url= allowlist-url= trackers-filename= disable-lsd disable-trackers h/help V/version
end

function __fish_rqbit_needs_command
    # Figure out if the current invocation already has a command.
    set -l cmd (commandline -opc)
    set -e cmd[1]
    argparse -s (__fish_rqbit_global_optspecs) -- $cmd 2>/dev/null
    or return
    if set -q argv[1]
        # Also print the command, so this can be used to figure out what it is.
        echo $argv[1]
        return 1
    end
    return 0
end

function __fish_rqbit_using_subcommand
    set -l cmd (__fish_rqbit_needs_command)
    test -z "$cmd"
    and return 1
    contains -- $cmd[1] $argv
end

complete -c rqbit -n "__fish_rqbit_needs_command" -s v -d 'The console loglevel' -r -f -a "trace\t''
debug\t''
info\t''
warn\t''
error\t''"
complete -c rqbit -n "__fish_rqbit_needs_command" -l log-file -d 'The log filename to also write to in addition to the console' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l log-file-rust-log -d 'The value for RUST_LOG in the log file' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -s i -l tracker-refresh-interval -d 'The interval to poll trackers, e.g. 30s. Trackers send the refresh interval when we connect to them. Often this is pretty big, e.g. 30 minutes. This can force a certain value' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l http-api-listen-addr -d 'The listen address for HTTP API' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l http-api-max-upload-size -d 'Upload body size limit in bytes' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l dht-bootstrap-addrs -d 'Set DHT bootstrap addrs A comma separated list of host:port or ip:port' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l peer-connect-timeout -d 'The connect timeout, e.g. 1s, 1.5s, 100ms etc' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l peer-read-write-timeout -d 'The timeout for read() and write() operations, e.g. 1s, 1.5s, 100ms etc' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l peer-limit -d 'The maximum number of connected peers per torrent' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -s t -l worker-threads -d 'How many threads to spawn for the executor' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l listen-port -d 'The port to listen for incoming connections (applies to both TCP and uTP)' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l announce-port -d 'The port to advertise to trackers and DHT' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l listen-ip -d 'What\'s the IP to listen on. Default is to listen on all interfaces on IPv4 and IPv6' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l upnp-server-friendly-name -d 'UPnP server name that would be displayed on devices in your network' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l bind-device -d 'What network device to bind to for DHT, BT-UDP, BT-TCP, trackers and LSD. On OSX will use IP(V6)_BOUND_IF, on Linux will use SO_BINDTODEVICE' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l max-blocking-threads -d 'How many maximum blocking tokio threads to spawn to process disk reads/writes. This will indicate how many parallel reads/writes can happen at a moment in time. The higher the number, the more the memory usage' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l socks-url -d 'If set will use socks5 proxy for all outgoing connections. The format is socks5://[username:password]@host:port' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l concurrent-init-limit -d 'How many torrents can be initializing (rehashing) at the same time' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l umask -d 'Set the process umask to this value' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l ratelimit-download -d 'Limit download speed to bytes-per-second' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l ratelimit-upload -d 'Limit upload speed to bytes-per-second' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l blocklist-url -d 'Downloads a p2p blocklist from this url and blocks connections from/to those peers. Supports file:/// and http(s):// URLs. Format is newline-delimited "name:start_ip-end_ip" E.g. https://github.com/Naunter/BT_BlockLists/raw/refs/heads/master/bt_blocklists.gz' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l allowlist-url -d 'Downloads a p2p allowlist from this url and blocks ALL connections BUT from/to those peers. Supports file:/// and http(s):// URLs. Format is newline-delimited "name:start_ip-end_ip" E.g. https://github.com/Naunter/BT_BlockLists/raw/refs/heads/master/bt_blocklists.gz' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l trackers-filename -d 'The filename with tracker URLs to always use for each torrent. Newline-delimited' -r
complete -c rqbit -n "__fish_rqbit_needs_command" -l log-file-json
complete -c rqbit -n "__fish_rqbit_needs_command" -l http-api-allow-create -d 'Allow creating torrents via HTTP API'
complete -c rqbit -n "__fish_rqbit_needs_command" -l enable-mdns -d 'Advertise the HTTP API on the local network via mDNS/DNS-SD, so it can be reached at http://rqbit.local:PORT from other devices on your LAN'
complete -c rqbit -n "__fish_rqbit_needs_command" -s s -l single-thread-runtime -d 'Set this flag if you want to use tokio\'s single threaded runtime. It MAY perform better, but the main purpose is easier debugging, as time profilers work better with this one'
complete -c rqbit -n "__fish_rqbit_needs_command" -l disable-dht
complete -c rqbit -n "__fish_rqbit_needs_command" -l disable-dht-persistence -d 'Set this to disable DHT reading and storing it\'s state. For now this is a useful workaround if you want to launch multiple rqbit instances, otherwise DHT port will conflict'
complete -c rqbit -n "__fish_rqbit_needs_command" -l disable-tcp-listen -d 'Disable listening for incoming connections over TCP. Note that outgoing connections can still be made (--disable-tcp-connect to disable)'
complete -c rqbit -n "__fish_rqbit_needs_command" -l disable-tcp-connect -d 'Disable outgoing connections over TCP. Note that listening over TCP for incoming connections is enabled by default (--disable-tcp-listen to disable)'
complete -c rqbit -n "__fish_rqbit_needs_command" -l experimental-enable-utp-listen -d 'Enable to listen and connect over uTP'
complete -c rqbit -n "__fish_rqbit_needs_command" -l disable-upnp-port-forward -d 'By default, rqbit will try to publish LISTEN_PORT through UPnP on your router. This can disable it'
complete -c rqbit -n "__fish_rqbit_needs_command" -l enable-upnp-server -d 'If set, will run a UPnP Media server on RQBIT_HTTP_API_LISTEN_ADDR'
complete -c rqbit -n "__fish_rqbit_needs_command" -l ipv4-only -d 'Force IPv4 only'
complete -c rqbit -n "__fish_rqbit_needs_command" -l disable-lsd -d 'Disable local peer discovery (LSD). By default rqbit will announce torrents to LAN'
complete -c rqbit -n "__fish_rqbit_needs_command" -l disable-trackers -d 'Disable trackers (for debugging DHT, LSD and --initial-peers)'
complete -c rqbit -n "__fish_rqbit_needs_command" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c rqbit -n "__fish_rqbit_needs_command" -s V -l version -d 'Print version'
complete -c rqbit -n "__fish_rqbit_needs_command" -f -a "server" -d 'Start rqbit server with HTTP API'
complete -c rqbit -n "__fish_rqbit_needs_command" -f -a "share" -d 'Create a torrent from a given path and announce it. Stateless'
complete -c rqbit -n "__fish_rqbit_needs_command" -f -a "download" -d 'Download a single torrent, stateless'
complete -c rqbit -n "__fish_rqbit_needs_command" -f -a "completions" -d 'Shell completions. eval "$(rqbit completions bash)"'
complete -c rqbit -n "__fish_rqbit_needs_command" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and not __fish_seen_subcommand_from start help" -s h -l help -d 'Print help'
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and not __fish_seen_subcommand_from start help" -f -a "start" -d 'Start the server'
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and not __fish_seen_subcommand_from start help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and __fish_seen_subcommand_from start" -l persistence-location -d 'The folder to store session data in. By default uses OS specific folder. If starts with postgres://, will use postgres as the backend instead of JSON file' -r
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and __fish_seen_subcommand_from start" -l watch-folder -d 'The folder to watch for added .torrent files. All files in this folder will be automatically added to the session' -r
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and __fish_seen_subcommand_from start" -l disable-persistence -d 'Disable server persistence. It will not read or write its state to disk.'
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and __fish_seen_subcommand_from start" -l fastresume -d '[Experimental] if set, will try to resume quickly after restart and skip checksumming'
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and __fish_seen_subcommand_from start" -s h -l help -d 'Print help'
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and __fish_seen_subcommand_from help" -f -a "start" -d 'Start the server'
complete -c rqbit -n "__fish_rqbit_using_subcommand server; and __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c rqbit -n "__fish_rqbit_using_subcommand share" -s n -l name -d 'Optional torrent name to use in the torrent file and magnet' -r
complete -c rqbit -n "__fish_rqbit_using_subcommand share" -s h -l help -d 'Print help'
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -s o -l output-folder -d 'The output folder to write to. Defaults to current folder' -r
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -s s -l sub-folder -d 'The sub folder within output folder to write to. Useful when you have a server running with output_folder configured, and don\'t want to specify the full path every time' -r
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -s r -l filename-re -d 'If set, only the file whose filename matching this regex will be downloaded' -r
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -l initial-peers -d 'A comma-separated list of initial peers' -r
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -s l -l list -d 'Only list the torrent metadata contents, don\'t do anything else'
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -l overwrite -d 'Set if you are ok to write on top of existing files'
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -s e -l exit-on-finish -d 'Exit the program once the torrents complete download'
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -l disable-http-api -d 'Disable HTTP API entirely'
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -l upnp-port-forward -d 'Forward the listen port through UPnP on your router'
complete -c rqbit -n "__fish_rqbit_using_subcommand download" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c rqbit -n "__fish_rqbit_using_subcommand completions" -s h -l help -d 'Print help'
complete -c rqbit -n "__fish_rqbit_using_subcommand help; and not __fish_seen_subcommand_from server share download completions help" -f -a "server" -d 'Start rqbit server with HTTP API'
complete -c rqbit -n "__fish_rqbit_using_subcommand help; and not __fish_seen_subcommand_from server share download completions help" -f -a "share" -d 'Create a torrent from a given path and announce it. Stateless'
complete -c rqbit -n "__fish_rqbit_using_subcommand help; and not __fish_seen_subcommand_from server share download completions help" -f -a "download" -d 'Download a single torrent, stateless'
complete -c rqbit -n "__fish_rqbit_using_subcommand help; and not __fish_seen_subcommand_from server share download completions help" -f -a "completions" -d 'Shell completions. eval "$(rqbit completions bash)"'
complete -c rqbit -n "__fish_rqbit_using_subcommand help; and not __fish_seen_subcommand_from server share download completions help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c rqbit -n "__fish_rqbit_using_subcommand help; and __fish_seen_subcommand_from server" -f -a "start" -d 'Start the server'
