# Print an optspec for argparse to handle cmd's options that are independent of any subcommand.
function __fish_pibtool_global_optspecs
	string join \n h/help V/version
end

function __fish_pibtool_needs_command
	# Figure out if the current invocation already has a command.
	set -l cmd (commandline -opc)
	set -e cmd[1]
	argparse -s (__fish_pibtool_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_pibtool_using_subcommand
	set -l cmd (__fish_pibtool_needs_command)
	test -z "$cmd"
	and return 1
	contains -- $cmd[1] $argv
end

complete -c pibtool -n "__fish_pibtool_needs_command" -s h -l help -d 'Print help'
complete -c pibtool -n "__fish_pibtool_needs_command" -s V -l version -d 'Print version'
complete -c pibtool -n "__fish_pibtool_needs_command" -f -a "fetch-documents" -d 'Fetch documents from one or multiple authorities'
complete -c pibtool -n "__fish_pibtool_needs_command" -f -a "fetch-borders" -d 'Fetch the borders from the OpenStreetMap overpass API'
complete -c pibtool -n "__fish_pibtool_needs_command" -f -a "check" -d 'Check that the `pib.json` definition files in the current directory are correct'
complete -c pibtool -n "__fish_pibtool_needs_command" -f -a "generate-website-data" -d 'Generate website data'
complete -c pibtool -n "__fish_pibtool_needs_command" -f -a "generate-shell-completion" -d 'Generate shell comletion'
complete -c pibtool -n "__fish_pibtool_needs_command" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-documents" -l only-recent-years -d 'Set the number of recent years which should be checked for new documents' -r
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-documents" -l force-download -d 'Force a (re-)download, even if the file is already known to `pibtool`'
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-documents" -l recursive -d 'Recurse into subdirectories and download for all authorities found there'
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-documents" -s v -l verbose -d 'Switch to a more verbose mode. By default, only `info`, `warn` and `error` log levels are printed. Given once, `debug` log level is turned on. Given twice or more, `trace` log level is turned on as well'
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-documents" -s h -l help -d 'Print help'
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-borders" -l geojson-converter -d 'Convert the download OSM JSON file to GeoJson with this command if present' -r
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-borders" -l simplify-tolerance -d 'Simplify the downloaded borders using the Douglas-Peucker algorithm with the given tolerance' -r
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-borders" -l force-download -d 'Force a (re-)download,  even if the file is already present'
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-borders" -l recursive -d 'Recurse into subdirectories and download for all authorities found there'
complete -c pibtool -n "__fish_pibtool_using_subcommand fetch-borders" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c pibtool -n "__fish_pibtool_using_subcommand check" -s h -l help -d 'Print help'
complete -c pibtool -n "__fish_pibtool_using_subcommand generate-website-data" -s h -l help -d 'Print help'
complete -c pibtool -n "__fish_pibtool_using_subcommand generate-shell-completion" -s h -l help -d 'Print help'
complete -c pibtool -n "__fish_pibtool_using_subcommand help; and not __fish_seen_subcommand_from fetch-documents fetch-borders check generate-website-data generate-shell-completion help" -f -a "fetch-documents" -d 'Fetch documents from one or multiple authorities'
complete -c pibtool -n "__fish_pibtool_using_subcommand help; and not __fish_seen_subcommand_from fetch-documents fetch-borders check generate-website-data generate-shell-completion help" -f -a "fetch-borders" -d 'Fetch the borders from the OpenStreetMap overpass API'
complete -c pibtool -n "__fish_pibtool_using_subcommand help; and not __fish_seen_subcommand_from fetch-documents fetch-borders check generate-website-data generate-shell-completion help" -f -a "check" -d 'Check that the `pib.json` definition files in the current directory are correct'
complete -c pibtool -n "__fish_pibtool_using_subcommand help; and not __fish_seen_subcommand_from fetch-documents fetch-borders check generate-website-data generate-shell-completion help" -f -a "generate-website-data" -d 'Generate website data'
complete -c pibtool -n "__fish_pibtool_using_subcommand help; and not __fish_seen_subcommand_from fetch-documents fetch-borders check generate-website-data generate-shell-completion help" -f -a "generate-shell-completion" -d 'Generate shell comletion'
complete -c pibtool -n "__fish_pibtool_using_subcommand help; and not __fish_seen_subcommand_from fetch-documents fetch-borders check generate-website-data generate-shell-completion help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
