#compdef osmium
#
# ZSH completion for Osmium tool.
#
# To test this file:
# 1) Comment out last line
# 2) Call: . ./_osmium
# 3) Call: compdef _osmium osmium
#
# To read more about what is happening here:
#  zshcompsys(1)
#  http://bewatermyfriend.org/p/2012/003/
#  http://zsh.sourceforge.net/Guide/zshguide06.html
#

osmium_file_glob="'*.(osm|osh|osc|o5m|o5c|pbf|osm.pbf) *.(osm|osh|osc|o5m|o5c).(bz2|gz)'"

_osmium() {
    local -a osmium_commands
    osmium_commands=(apply-changes cat changeset-filter check-refs fileinfo getid help merge-changes renumber sort time-filter)
    if (( CURRENT > 2 )); then
        # Remember the subcommand name
        local cmd=${words[2]}
        # Set the context for the subcommand.
        curcontext="${curcontext%:*:*}:osmium-$cmd"
        # Narrow the range of words we are looking at to exclude `osmium'
        (( CURRENT-- ))
        shift words
        # Run the completion for the subcommand
        _osmium-$cmd
    else
        _describe -t osmium-commands 'osmium subcommand' osmium_commands
    fi
}

_osmium-common-options() {
    echo '(--verbose)-v[set verbose mode]'
    echo '(-v)--verbose[set verbose mode]'
}

_osmium-single-input-options() {
    echo '(--input-format)-F[format of input OSM file]:OSM file format:_osmium_file_formats'
    echo '(-F)--input-format[format of input OSM file]:OSM file format:_osmium_file_formats'
    echo "1:input OSM files:_files -g ${osmium_file_glob}"
}

_osmium-multiple-inputs-options() {
    echo '(--input-format)-F[format of input OSM files]:OSM file format:_osmium_file_formats'
    echo '(-F)--input-format[format of input OSM files]:OSM file format:_osmium_file_formats'
    echo "*:input OSM files:_files -g ${osmium_file_glob}"
}

_osmium-output-options() {
    echo '--generator[generator setting for output file header]:generator:'
    echo "(--output)-o[output file name]:output OSM file:_files -g ${osmium_file_glob}"
    echo "(-o)--output[output file name]:output OSM file:_files -g ${osmium_file_glob}"
    echo '(--output-format)-f[format of output OSM file]:OSM file format:_osmium_file_formats'
    echo '(-f)--output-format[format of output OSM file]:OSM file format:_osmium_file_formats'
    echo '(--overwrite)-O[allow overwriting of existing output file]'
    echo '(-O)--overwrite[allow overwriting of existing output file]'
}

_osmium-apply-changes() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-multiple-inputs-options)"} \
        ${(f)"$(_osmium-output-options)"} \
        '(--remove-deleted)-r[remove deleted objects]' \
        '(-r)--remove-deleted[remove deleted objects]' \
        '(--simplify)-s[only write last version of any object]' \
        '(-s)--simplify[only write last version of any object]'
}

_osmium-cat() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-multiple-inputs-options)"} \
        ${(f)"$(_osmium-output-options)"} \
        '*-t[read only object of given output types]:OSM entity type:_osmium_entity_type' \
        '*--object-type[read only object of given output types]:OSM entity type:_osmium_entity_type'
}

_osmium-changeset-filter() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-single-input-options)"} \
        ${(f)"$(_osmium-output-options)"} \
        '(--with-discussions)-d[changesets with discussions]' \
        '(-d)--with-discussions[changesets with discussions]' \
        '(--without-discussions)-D[changesets without discussions]' \
        '(-D)--without-discussions[changesets without discussions]' \
        '(--with-changes)-c[changesets with changes]' \
        '(-c)--with-changes[changesets with changes]' \
        '(--without-changes)-C[changesets without changes]' \
        '(-C)--without-changes[changesets without changes]' \
        '--open[open changesets]' \
        '--closed[closed changesets]' \
        '(--user)-u[changesets by user]' \
        '(-u)--user[changesets by user]' \
        '(--uid)-U[changesets by user id]' \
        '(-U)--uid[changesets by user id]' \
        '-a[changesets closed after]:timestamp:' \
        '--after[changesets closed after]:timestamp:' \
        '-b[changesets opened before]:timestamp:' \
        '--before[changesets opened before]:timestamp:'
}

_osmium-check-refs() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-single-input-options)"} \
        '(--show-ids)-i[show ids of missing objects]' \
        '(-i)--show-ids[show ids of missing objects]' \
        '(--check-relations)-r[also check referential integrity of relations]' \
        '(-r)--check-relations[also check referential integrity of relations]'
}

_osmium-fileinfo() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-single-input-options)"} \
        '(--show-variables -G --extended)-e[show extended info (reads entire file)]' \
        '(--show-variables -G -e)--extended[show extended info (reads entire file)]' \
        '(--show-variables -G --json -j --get)-g[get value for one variable]:variable:_osmium_fileinfo_variables' \
        '(--show-variables -G --json -j -g)--get[get value for one variable]:variable:_osmium_fileinfo_variables' \
        '(--get -g --json)-j[output variables in JSON format]' \
        '(--get -g -j)--json[output variables in JSON format]' \
        '(--get -g --json -j --extended -e --show-variables)-G[show a list of all variable names]' \
        '(--get -g --json -j --extended -e -G)--show-variables[show a list of all variable names]'
}

_osmium-getid() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-single-input-options)"} \
        ${(f)"$(_osmium-output-options)"} \
        "*:IDs (format\: [nwr]NUM):"
}

_osmium-merge-changes() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-multiple-inputs-options)"} \
        ${(f)"$(_osmium-output-options)"} \
        '(--simplify)-s[only write last version of any object]' \
        '(-s)--simplify[only write last version of any object]'
}

_osmium-renumber() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-single-input-options)"} \
        ${(f)"$(_osmium-output-options)"} \
        '(--index-directory)-i[read/write index files in this directory]:directory:_path_files -/' \
        '(-i)--index-directory[read/write index files in this directory]:directory:_path_files -/'
}

_osmium-sort() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-multiple-inputs-options)"} \
        ${(f)"$(_osmium-output-options)"}
}

_osmium-time-filter() {
    _arguments : \
        ${(f)"$(_osmium-common-options)"} \
        ${(f)"$(_osmium-single-input-options)"} \
        ${(f)"$(_osmium-output-options)"} \
        "2::start time (format\: yyyy-mm-ddThh\:mm\:ssZ):" \
        "3::end time (format\: yyyy-mm-ddThh\:mm\:ssZ):"
}

_osmium_file_formats() {
    _values 'OSM file formats' \
        'osm[XML data format]' \
        'osm.gz[XML data format (compressed with gzip)]' \
        'osm.bz2[XML data format (compressed with bzip2)]' \
        'osh[XML history format]' \
        'osh.gz[XML history format (compressed with gzip)]' \
        'osh.bz2[XML history format (compressed with bzip2)]' \
        'osc[XML change format]' \
        'osc.gz[XML change format (compressed with gzip)]' \
        'osc.bz2[XML change format (compressed with bzip2)]' \
        'pbf[PBF format]'
}

_osmium_entity_type() {
    _values 'OSM entity type' \
        'node' \
        'way' \
        'relation' \
        'changeset'
}

_osmium_fileinfo_variables() {
    _values 'variable' $(osmium fileinfo --show-variables)
}

_osmium-help() {
    local -a osmium_help_topics
    osmium_help_topics=(apply-changes cat changeset-filter check-refs fileinfo getid help merge-changes renumber sort time-filter file-formats)
    _describe -t osmium-help-topics 'osmium help topics' osmium_help_topics
}

_osmium "$@"

