#compdef tar gtar star


local _tar_cmd tf tmp tmpb del index


if _pick_variant gnu=GNU unix --version; then
  case "$($service --version)" in
    ("tar (GNU tar) "(#b)([0-9.-]##)*)
    autoload -z is-at-least
    is-at-least 1.14.91 "$match[1]" || _cmd_variant[$service]="gnu-old"
    ;;
  esac
fi

tmp=("${(@M)words:#-[^-]*}")
_tar_cmd="${(j::)tmp#-}"

(( $words[(I)--(un|)gzip] ))     && _tar_cmd="z$_tar_cmd"
(( $words[(I)--(un|)compress] )) && _tar_cmd="Z$_tar_cmd"
(( $words[(I)--bzip2] ))         && _tar_cmd="j$_tar_cmd"
(( $words[(I)--xz] ))            && _tar_cmd="J$_tar_cmd"
(( $words[(I)--list] ))          && _tar_cmd="t$_tar_cmd"
(( $words[(I)--(extract|get)] )) && _tar_cmd="x$_tar_cmd"
(( $words[(I)--create] ))        && _tar_cmd="c$_tar_cmd"

if [[ "$words[2]" = *[txcdruA]*~-* ]]; then
  _tar_cmd="$words[2]$_tar_cmd"
elif [[ $_tar_cmd != *[txcdruA]* && CURRENT -gt 2 ]]; then
  (( $words[(I)--(diff|compare)] )) && _tar_cmd="d$_tar_cmd"
  (( $words[(I)--append] ))         && _tar_cmd="r$_tar_cmd"
  (( $words[(I)--update] ))         && _tar_cmd="u$_tar_cmd"
  (( $words[(I)--(con|)catenate] )) && _tar_cmd="A$_tar_cmd"
  (( $words[(I)--delete] ))         && del=1
fi


tmp="$words[(I)--file=*]"
tmpb="$words[(I)-*Cf*~--*]"

if (( tmp )); then
  tf=${~words[tmp][8,-1]}
  _tar_cmd="f$_tar_cmd"
elif [[ "$words[2]" != -* && "$words[2]" = *f* ]]; then
  tf=${~words[3]}
  _tar_cmd="f$_tar_cmd"
elif (( tmpb )); then
  tf=${~words[tmpb+2]}
  wdir=${~words[tmpb+1]}
  _tar_cmd="Cf$_tar_cmd"
else
  tmp="${words[(I)-*f*~--*]}"
  if (( tmp )); then
    tf=${~words[tmp+1]}
    _tar_cmd="f$_tar_cmd"
  fi
fi



tmp=${words[(r)--dir[a-z]#=*]}

if [[ -n $tmp ]]; then
  eval "wdir=(${tmp#*=})"
fi


if [[ "$PREFIX" = --* ]]; then


  _arguments -- '--owner*:user:_users' \
                 '*=(PROG|COMMAND)*:program:_command_names -e' \
		 '*=ARCHIVE*:archive: _tar_archive' \
		 '*=NAME*:file:_files' \
		 '*=DIR*:directory:_files -/' \
		 '*=CONTROL*::version control:(t numbered nil existing never simple)'

elif [[ ( CURRENT -gt 2 && "$words[CURRENT-1]" = -[^C]#f* &&
          "$words[CURRENT-1]" != --* ) ||
        ( CURRENT -eq 3 && "$words[2]" = [^C]#f* && "$words[2]" != -* ) ||
        ( CURRENT -gt 2 && "$words[CURRENT-2]" = -*C*f* &&
          "$words[CURRENT-2]" != --* && "$words[CURRENT-1]" != --* ) ||
        ( CURRENT -eq 4 && "$words[2]" = *C*f* && "$words[2]" != -* ) ]]; then


  _tar_archive

elif [[ ( CURRENT -gt 2 && "$words[CURRENT-1]" = -[^f]#C*) ||
        ( CURRENT -eq 3 && "$words[2]" = [^f]#C* ) ]]; then


  _directories

elif [[ ( "$_tar_cmd" = *[xt]* || -n $del ) && -n "$tf" ]]; then


  local largs=-tf expl

  if [[ $_tar_cmd = *z* ]]; then
    largs=-tzf
  elif [[ $_tar_cmd = *j* ]]; then
    largs=-tjf
  elif [[ $_tar_cmd = *y* ]]; then
    largs=-tyf
  elif [[ $_tar_cmd = *Z* ]]; then
    largs=-tZf
  elif [[ $_tar_cmd = *I* ]]; then
    largs=-tIf
  elif [[ $_tar_cmd = *J* ]]; then
    largs=-tJf
  else
    tmp="${words[(r)--use-comp*]}"
    [[ -n $tmp ]] && largs=($tmp -tf)
  fi

  if [[ $tf != $_tar_cache_name && -f $tf ]]; then
    _tar_cache_list=("${(@f)$($words[1] $largs $tf)}")
    _tar_cache_name=$tf
  fi

  _wanted files expl 'file from archive' _multi_parts / _tar_cache_list
elif (( CURRENT == 2 )); then
  compset -P -
  _values -s '' 'tar function' \
    '(c t u x)A[append to an archive]' \
    '(A t u x)c[create a new archive]' \
    '(A c u x)t[list archive contents]' \
    '(A c t x)u[update archive]' \
    '(A c t u)x[extract files from an archive]' \
    'v[verbose output]' \
    'f[specify archive file or device]'
else
  if ! (( index=$words[(I)-*C*] )); then
    if [[ $words[2] = [^f]#C* ]]; then
      index=1
    elif [[ $words[2] = *f*C* ]]; then
      index=2
    fi
  fi
  if (( index )); then
    index=${~${(Q)words[index+1]}}
    [[ $index = (.|..|)/* ]] || index=~+/$index
    _files -W $index
  else
    _files
  fi
fi
