_attic() {
    local curcontext="$curcontext" state line
    typeset -A opt_args

	_arguments -C \
        {-h,--help}'[Show Help]'\
        ':command:->command'\
        '*::options:->options'

	case "$state" in
		(command)
			local -a subcommands
            subcommands=(
                'init:initialize empty repository'
                'check:verify repository'
                'change-passphrase:change repository passphrase'
                'create:create backup'
                'extract:extract archive contents'
                'delete:delete archive'
                'list:list archive or repository contents'
                'mount:mount repository'
                'info:show archive information'
                'prune:prune archives'
            )
            _describe -t commands '' subcommands
            ;;
        (options)
            case $line[1] in
                (init)
                    _arguments \
                        '1:repository to create:()'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                        {-e,--encryption}='[select encryption method]:method:(none passphrase keyfile)'\
                    ;;
                (create)
                    _arguments \
                        '1:first archive:__attic_archive'\
                        '*:next path:_files'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                        {-s,--stats}'[print statistics for the created archives]'\
                        {-e,--exclude}='[exclude by Pattern]:pattern'\
                        --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:excludefile:_files'\
                        --exclude-caches'[exclude directory that contain a CACHEDIR.TAG file]'\
                        {-c,--checkpoint-interval}='[write checkpoint every X seconds]:time'\
                        --do-not-cross-mountpoints'[Do not cross mount points]'\
                        --numeric-owner'[only store numeric user and group identifiers]'\
                    ;;
                (extract)
                    _arguments \
                        '1:first archive:__attic_archive'\
                        '*:next arg[Path to archive]'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                        {-n,--dry-run}'[do not create a backup]'\
                        {-e,--exclude}'[exclude by Pattern]:pattern'\
                        --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:exclude-from:_files'\
                        --numeric-owner'[only store numeric user and group identifiers]'\
                        --strip-components='[strip N number of leading path elements.]:number'\
                    ;;
                (check)
                    _arguments \
                        '1:first archive:__attic_archive'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                        --repository-only'[only perform repository checks]'\
                        --archives-only'[only perform archive checks]'\
                        --repair'[attempt to repair an inconsistancies found]'\
                    ;;
                (delete)
                    _arguments \
                        '1:first archive:__attic_archive'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                        {-s,--stats}'[print statistics for the deleted archives]'\
                    ;;
                (list)
                    _arguments \
                        '1:first archive:__attic_archive'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                    ;;
                (prune)
                    _arguments \
                        '1:first repo:_files'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                        {-n,--dry-run}'[do not change the repository]'\
                        {-s,--stats}'[print statistics for the deleted archives]'\
                        --keep-within='[keep all archvie within this time interval]:interval'\
                        {-H,--keep-hourly}='[number of hourly backups to keep]:time'\
                        {-d,--keep-daily}='[number of daily backups to keep]:time'\
                        {-w,--keep-weekly}='[number of weekly backups to keep]:time'\
                        {-m,--keep-monthly}='[number of monthly backups to keep]:time'\
                        {-y,--keep-yearly}='[number of yearly backups to keep]:time'\
                        {-p,--prefix}='[only consider archive names starting with this prefix]:prefix:_files'\
                    ;;
                (info)
                    _arguments \
                        '1:first archive:__attic_archive'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                    ;;
                (mount)
                    _arguments \
                        '1:first archive:__attic_archive'\
                        '2:mount point:_dir_list'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                        {-f,--foreground}'[stay in foreground, do not daemonize]'\
                        -o'[Extra Mount Options]'\
                    ;;
                (change-passphrase)
                    _arguments \
                        '1:first repo:_files'\
                        {-h,--help}'[show this help message and exit]'\
                        {-v,--verbose}'[verbose output]'\
                    ;;
            esac
            ;;
    esac
}


__attic_archive() {
    local -a cpath
    cpath=$line[1]
    cpath=${cpath%::*}

    if [[ -d "${cpath}" && -f "${cpath}/README" && "$(cat ${cpath}/README)" == "This is an Attic repository" ]]; then
        local items
        items=("${(@f)$(attic list ${cpath} 2>/dev/null | cut -d ' ' -f 1 | sed "s#^#${cpath}::#" )}")
        _wanted archives expl 'archive' compadd $items
    else
        _files -/
    fi

}


compdef _attic attic
