#compdef cabal
# ------------------------------------------------------------------------------
# Copyright (c) 2012 Github zsh-users - http://github.com/zsh-users
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the zsh-users nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for cabal-install (http://hackage.haskell.org/trac/hackage/wiki/CabalInstall)
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Gérard Milmeister
# * Matvey Aksenov <matvey.aksenov@gmail.com>
#
# ------------------------------------------------------------------------------

local WORDS

_cabal ()
{
  WORDS=()
  for w in $words[1,(($CURRENT - 1))]; do
    if [[ $w != -* ]]; then WORDS+=$w; fi
  done

  if (( $#WORDS == 1 )); then
    _arguments \
    {-h,--help}'[show help]' \
    {-V,--version}'[print version information]' \
    '--numeric-version[print just the version number]' \
    '--config-file=[set an alternate location for the config file]:config file:_files' \
    '--sandbox-config-file=[set an alternate location for the sandbox config file]:config file:_files' \
    '--require-sandbox[enable requiring the presence of a sandbox for sandbox-aware commands]' \
    '--no-require-sandbox[disable requiring the presence of a sandbox for sandbox-aware commands]' \
    '--ignore-sandbox[ignore any existing sandbox]' \
    '*::command:_cabal_command'
  else
    _arguments '*::command:_cabal_command'
  fi
}

_cabal_command ()
{
  local -a _cabal_cmds
  _cabal_cmds=(
    'install:installs a list of packages'
    'update:updates list of known packages'
    'list:list packages matching a search string'
    'info:display detailed information about a particular package'
    'fetch:downloads packages for later installation'
    'freeze:freeze dependencies'
    "get:get a package's source code"
    'check:check the package for common mistakes'
    'sdist:generate a source distribution file (.tar.gz)'
    'upload:uploads source packages to Hackage'
    'report:upload build reports to a remote server'
    'run:runs the compiled executable'
    'init:interactively create a .cabal file'
    'configure:prepare to build the package'
    'build:compile all targets or specific targets'
    'repl:open an interpreter session for the given target'
    'sandbox:create/modify/delete a sandbox'
    'haddock:generate Haddock HTML documentation'
    'exec:run a command with the cabal environment'
    'copy:copy the files into the install locations'
    'clean:clean up after a build'
    'hscolour:generate HsColour colourised code, in HTML format'
    'register:register this package with the compiler'
    'test:run the test suite, if any (configure with UserHooks)'
    'bench:run the benchmark, if any (configure with UserHooks)'
    'help:help about commands'
  )

  local -a _cabal_programs
  _cabal_programs=()

  programs=(alex ar c2hs cpphs ffihugs gcc ghc ghc-pkg greencard haddock
    happy hmake hpc hsc2hs hscolour hugs jhc ld lhc lhc-pkg nhc98 pkg-config
    ranlib strip tar uhc)

  for program in $programs; do
    _cabal_programs+=(
      "--with-${program}=[give the path to ${program}]:file:_files"
      "--${program}-options=[give extra options to ${program}]"
      "--${program}-option=[give an extra option to ${program}]"
    )
  done

  if (( CURRENT == 1 )) then
    _describe -t commands 'command' _cabal_cmds || compadd "$@"
  else
    local curcontext="$curcontext"
    cmd="${${_cabal_cmds[(r)$WORDS[2]:*]%%:*}}"
    if (( $#cmd )); then
      _call_function ret _cabal_$cmd
    else
      _message "unknown cabal command: $WORDS[2]"
    fi
  fi
}

_cabal_bench ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated build files]:directory:_files -/' \
    '--benchmark-options=[give extra options to benchmark executables]' \
    '--benchmark-option=[give an extra option to benchmark executables (no need to quote options containing spaces)]' \
    {-j,--jobs=}'[run NUM jobs simultaneously]' \
    $_cabal_programs \
    "--only[don't reinstall add-source dependencies (sandbox-only)]"
}

_cabal_build ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated build files]:directory:_files -/' \
    $_cabal_programs \
    {-j,--jobs=}'[run NUM jobs simultaneously]' \
    "--only[don't reinstall add-source dependencies (sandbox-only)]"
}

_cabal_check ()
{
  _arguments {-h,--help}'[show help]'
}

_cabal_clean ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated build files]:directory:_files -/' \
    {-s,--save-configure}'[do not remove the configuration file]'
}

_cabal_configure ()
{
  local context state state_descr line
  typeset -A opt_args

  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated build files]:directory:_files -/' \
    {-g,--ghc}'[compile with GHC]' \
    '--nhc98[compile with NHC]' \
    '--jhc[compile with JHC]' \
    '--lhc[compile with LHC]' \
    '--hugs[compile with Hugs]' \
    '--uhc[compile with UHC]' \
    '--haskell-suite[compile with a haskell-suite compiler]' \
    {-w,--with-compiler=}'[give the path to a particular compiler]:file:_files' \
    '--with-hc-pkg=[give the path to the package tool]:file:_files' \
    '--prefix=[take this prefix in preparation of installation]:directory:_files -/' \
    '--bindir=[installation directory for executables]:directory:_files -/' \
    '--libdir=[installation directory for libraries]:directory:_files -/' \
    '--libsubdir=[subdirectory of libdir in which libs are installed]:directory:_files -/' \
    '--libexecdir=[iinstallation directory for program executables]:directory:_files -/' \
    '--datadir=[installation directory for read-only data]:directory:_files -/' \
    '--datasubdir=[subdirectory of datadir in which data files are installed]:directory:_files -/' \
    '--docdir=[installation directory for documentation]:directory:_files -/' \
    '--htmldir=[installation directory for HTML]:directory:_files -/' \
    '--haddockdir=[installation directory for haddock interfaces]:directory:_files -/' \
    '--sysconfdir=[installation directory for configuration files]:directory:_files -/' \
    {-b,--scratchdir=}'[directory to receive the built package]:directory:_files -/' \
    '--program-prefix=[prefix to be applied to installed executables]' \
    '--program-suffix=[suffix to be applied to installed executables]' \
    '--enable-library-vanilla[enable Vanilla libraries]' \
    '--disable-library-vanilla[disable Vanilla libraries]' \
    {-p,--enable-library-profiling}'[enable Library profiling]' \
    '--disable-library-profiling[disable Library profiling]' \
    '--enable-shared[enable Shared library]' \
    '--disable-shared[disable Shared library]' \
    '--enable-executable-dynamic[enable Executable dynamic linking]' \
    '--disable-executable-dynamic[disable Executable dynamic linking]' \
    '--enable-executable-profiling[enable Executable profiling]' \
    '--disable-executable-profiling[disable Executable profiling]' \
    {-O-,--enable-optimization=}'[build with optimization]:level:(0 1 2)' \
    '--disable-optimization[build without optimization]' \
    '--enable-library-for-ghci[enable compile library for use with GHCi]' \
    '--disable-library-for-ghci[disable compile library for use with GHCi]' \
    '--enable-split-objs[enable split library into smaller objects]' \
    '--disable-split-objs[disable split library into smaller objects]' \
    '--enable-executable-stripping[enable strip executables upon installation]' \
    '--disable-executable-stripping[disable strip executables upon installation]' \
    '--configure-option=[extra option for configure]' \
    '--user[enable doing a per-user installation]' \
    '--global[disable doing a per-user installation]' \
    '--package-db=[use a given package database]:: :->default-db-or-filepath' \
    {-f,--flags=}'[force values for the given flags]:flags:' \
    '--extra-include-dirs=[a list of directories to search for header files]:directory:_files -/' \
    '--extra-lib-dirs=[a list of directories to search for externallibraries]:directory:_files -/' \
    '--extra-prog-path=[A list of directories to search for required programs]:directory:_files -/' \
    '--enable-tests[enable dependency checking and compilation for test suites listed in the package description file]' \
    '--disable-tests[disable dependency checking and compilation for test suites listed in the package description file]' \
    '--enable-library-coverage[enable build library and test suites with, Haskell Program Coverage enabled. (GHC only)]' \
    '--disable-library-coverage[disable build library and test suites with, Haskell Program Coverage enabled. (GHC only)]' \
    '--enable-benchmarks[enable dependency checking and compilation, for benchmarks listed in the package]' \
    '--disable-benchmarks[disable dependency checking and compilation, for benchmarks listed in the package]' \
    $_cabal_programs \
    '--cabal-lib-version=[select which version of the Cabal lib to use]' \
    '--constraint=[a list of additional constraints on the dependencies]' \
    '--preference=[specify preferences on the version of a package]' \
    '--solver=[select dependency solver to use]:solver:(topdown modular choose)' \
    '--allow-newer=[ignore upper bounds in dependencies on some or all packages]'

  case $state in
    (default-db-or-filepath)
      _alternative \
        ':default db:(global user clear)' \
        ':filepath:_files'
    ;;
  esac
}

_cabal_copy ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated build]:directory:_files -/' \
    '--destdir=[directory to copy files to]:directory:_files -/'
}

_cabal_exec ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)'
}

_cabal_fetch ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--dependencies[resolve and fetch dependencies]' \
    '--no-dependencies[ignore dependencies]' \
    '--dry-run[do not install anything, only print what would be installed]' \
    '--solver=[select dependency solver to use]:solver:(topdown modular choose)]' \
    '--max-backjumps=[maximum number of backjumps allowed while solving dependencies]' \
    '--reorder-goals[try to reorder goals according to certain heuristics]' \
    '--shadow-installed-packages[if multiple package instances of the same version are installed, treat all but one as shadowed]' \
    '*:package:_cabal_list_packages'
}

_cabal_freeze ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--dry-run[do not freeze anything, only print what would be frozen]' \
    '--solver=[select dependency solver to use]:solver:(topdown modular choose)]' \
    '--max-backjumps=[maximum number of backjumps allowed while solving dependencies]' \
    '--reorder-goals[try to reorder goals according to certain heuristics]' \
    '--shadow-installed-packages[if multiple package instances of the same version are installed, treat all but one as shadowed]'
}

_cabal_get ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    {-d-,--destdir=}'[where to unpack the packages]:directory:_files -/' \
    {-s,--source-repository=}"[copy the package's source repository]" \
    '--pristine[unpack the original pristine tarball, rather than updating the .cabal file with the latest revision from the package archive.]' \
    '*:package:_cabal_list_packages'
}

_cabal_haddock ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated files]:directory:_files -/' \
    '--keep-temp-files[keep temporary files]' \
    '--hoogle[generate a hoogle database]' \
    '--html[generate HTML documentation]' \
    '--html-location=[location of HTML documentation]:url:' \
    '--executables[run haddock for Executables targets]' \
    '--tests[run haddock for Test Suite targets]' \
    '--benchmarks[run haddock for Benchmark targets]' \
    '--all[run haddock for all targets]' \
    '--internal[run haddock for internal modules]' \
    '--css=[path to the haddock stylesheet]:file:_files' \
    '--hyperlink-source[hyperlink the documentation to the source code]' \
    '--hscolour-css=[path to the HsColour stylesheet]:file:_files' \
    '--contents-location=[bake URL in as the location for the contents page]:url:' \
    '--with-ghc=[path to ghc]:file:_files' \
    '--with-haddock=[path to haddock]:file:_files' \
    '--PROG-option=[give an extra option to PROG (no need to quote options containing spaces)]:option:' \
    '--ghc-options=[give extra options to ghc]:option:' \
    '--haddock-options=[give extra options to haddock]:option:' \
}

_cabal_help ()
{
  local -a cmds
  cmds=(install update list info fetch freeze get check sdist upload report run init
    configure build repl sandbox haddock exec copy clean hscolour register test bench help)
  _arguments \
    {-h,--help}'[Show help]' \
    '*::command:( $cmds )'
}

_cabal_hscolour ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated files]:directory:_files -/' \
    '--executables[run hscolour for Executables targets]' \
    '--tests[run hscolour for Test Suite targets]' \
    '--benchmarks[run hscolour for Benchmarks targets]' \
    '--all[run hscolour for all targets]' \
    '--css=[path to stylesheet]:file:_files'
}

_cabal_info ()
{
  local context state state_descr line
  typeset -A opt_args

  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--package-db=[use a given package database]:: :->default-db-or-filepath' \
    '*:package:_cabal_list_packages'

  case $state in
    (default-db-or-filepath)
      _alternative \
        ':default db:(global user clear)' \
        ':filepath:_files'
    ;;
  esac
}

_cabal_install ()
{
  local context state state_descr line
  typeset -A opt_args

  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated build files]:directory:_files -/' \
    {-g,--ghc}'[compile with GHC]' \
    '--nhc98[compile with NHC]' \
    '--jhc[compile with JHC]' \
    '--lhc[compile with LHC]' \
    '--hugs[compile with Hugs]' \
    '--uhc[compile with UHC]' \
    '--haskell-suite[compile with a haskell-suite compiler]' \
    {-w,--with-compiler=}'[give the path to a particular compiler]:file:_files' \
    '--with-hc-pkg=[give the path to the package tool]:file:_files' \
    '--prefix=[take this prefix in preparation of installation]:directory:_files -/' \
    '--bindir=[installation directory for executables]:directory:_files -/' \
    '--libdir=[installation directory for libraries]:directory:_files -/' \
    '--libsubdir=[subdirectory of libdir in which libs are installed]:directory:_files -/' \
    '--libexecdir=[installation directory for program executables]:directory:_files -/' \
    '--datadir=[installation directory for read-only data]:directory:_files -/' \
    '--datasubdir=[subdirectory of datadir in which data files are installed]:directory:_files -/' \
    '--docdir=[installation directory for documentation]:directory:_files -/' \
    '--htmldir=[installation directory for HTML]:directory:_files -/' \
    '--haddockdir=[installation directory for haddock interfaces]:directory:_files -/' \
    '--sysconfdir=[installation directory for configuration files]:directory:_files -/' \
    {-b,--scratchdir=}'[directory to receive the built package]:directory:_files -/' \
    '--program-prefix=[prefix to be applied to installed executables]' \
    '--program-suffix=[suffix to be applied to installed executables]' \
    '--enable-library-vanilla[enable Vanilla libraries]' \
    '--disable-library-vanilla[disable Vanilla libraries]' \
    {-p,--enable-library-profiling}'[enable Library profiling]' \
    '--disable-library-profiling[disable Library profiling]' \
    '--enable-shared[enable Shared library]' \
    '--disable-shared[disable Shared library]' \
    '--enable-executable-dynamic[enable Executable dynamic linking]' \
    '--disable-executable-dynamic[disable Executable dynamic linking]' \
    '--enable-executable-profiling[enable Executable profiling]' \
    '--disable-executable-profiling[disable Executable profiling]' \
    {-O-,--enable-optimization=}'[build with optimization]:level:(0 1 2)' \
    '--disable-optimization[build without optimization]' \
    '--enable-library-for-ghci[enable compile library for use with GHCi]' \
    '--disable-library-for-ghci[disable compile library for use with GHCi]' \
    '--enable-split-objs[enable split library into smaller objects]' \
    '--disable-split-objs[disable split library into smaller objects]' \
    '--enable-executable-stripping[enable strip executables upon installation]' \
    '--disable-executable-stripping[disable strip executables upon installation]' \
    '--configure-option=[extra option for configure]' \
    '--user[enable doing a per-user installation]' \
    '--global[disable doing a per-user installation]' \
    '--package-db=[use a given package database]:: :->default-db-or-filepath' \
    {-f,--flags=}'[force values for the given flags]:flags:' \
    '--extra-include-dirs=[a list of directories to search for header files]:directory:_files -/' \
    '--extra-lib-dirs=[a list of directories to search for external libraries]:directory:_files -/' \
    '--extra-prog-path=[a list of directories to search for required programs]:directory:_files -/' \
    '--enable-tests[enable dependency checking and compilation for test suites listed in the package description file]' \
    '--disable-tests[disable dependency checking and compilation for test suites listed in the package description file]' \
    '--enable-library-coverage[enable build library and test suites with, Haskell Program Coverage enabled. (GHC only)]' \
    '--disable-library-coverage[disable build library and test suites with, Haskell Program Coverage enabled. (GHC only)]' \
    '--enable-benchmarks[enable dependency checking and compilation, for benchmarks listed in the package]' \
    '--disable-benchmarks[disable dependency checking and compilation, for benchmarks listed in the package]' \
    $_cabal_programs \
    '--cabal-lib-version=[select which version of the Cabal lib to use]' \
    '--constraint=[a list of additional constraints on the dependencies]' \
    '--preference=[specify preferences on the version of a package]' \
    '--solver=[select dependency solver to use]:solver:(topdown modular choose)' \
    '--allow-newer=[ignore upper bounds in dependencies on some or all packages]' \
    '--enable-documentation[enable building of documentation]' \
    '--disable-documentation[disable building of documentation]' \
    '--doc-index-file=[a central index of haddock API documentation]:file:_files' \
    '--dry-run[do not install anything]' \
    '--max-backjumps=[maximum number of backjumps allowed while solving dependencies]' \
    '--reorder-goals[try to reorder goals according to certain heuristics]' \
    '--shadow-installed-packages[if multiple package instances of the same version are installed, treat all but one as shadowed]' \
    '--reinstall[always install]' \
    '--avoid-reinstalls[do not select versions that would destructively overwrite installed packages]' \
    '--force-reinstalls[reinstall packages even if they will most likely break other installed packages]' \
    '--upgrade-dependencies[pick the latest version for all dependencies, rather than trying to pick an installed version]' \
    {--only-dependencies,--dependencies-only}'[install only the dependencies necessary to build the given packages]' \
    '--root-cmd=[command used to gain root privileges]::' \
    '--symlink-bindir=[add symlinks into this directory]:directory:_files -/' \
    '--build-summary=[save build summaries to file]:file:_files' \
    '--build-log=[log all builds to file]:file:_files' \
    '--remote-build-reporting=[generate build reports to send to a remote]:level:(none anonymous detailed)' \
    '--one-shot[do not record the packages in the world file]' \
    '--run-tests[run package test suites during installation]' \
    {-j,--jobs=}'[run NUM jobs simultaneously]' \
    '--haddock-hoogle[generate a hoogle database]' \
    '--haddock-html[generate HTML documentation]' \
    '--haddock-html-location=[location of HTML documentation]:url:' \
    '--haddock-executables[run haddock for Executables targets]' \
    '--haddock-tests[run haddock for Test Suite targets]' \
    '--haddock-benchmarks[run haddock for Benchmarks targets]' \
    '--haddock-all[run haddock for all targets]' \
    '--haddock-internal[run haddock for internal modules]' \
    '--haddock-css=[path to the haddock stylesheet]:file:_files' \
    '--haddock-hyperlink-source[hyperlink the documentation to the source code]' \
    '--haddock-hscolour-css=[path to the HsColour stylesheet]:file:_files' \
    '--haddock-contents-location=[bake URL in as the location for the contents page]:url:' \
    '*:: :->package-or-cabal-file'

  case $state in
    (default-db-or-filepath)
      _alternative \
        ':default db:(global user clear)' \
        ':filepath:_files'
    ;;
    (package-or-cabal-file)
      _alternative \
         ':package:_cabal_list_packages' \
         ':files:_files -g "*.cabal"'
    ;;
  esac
}

_cabal_list ()
{
  local context state state_descr line
  typeset -A opt_args

  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--installed[only print installed packages]' \
    '--simple-output[print in a easy-to-parse format]' \
    '--package-db=[use a given package database]:: :->default-db-or-filepath' \
    '*:package:_cabal_list_packages'

  case $state in
    (default-db-or-filepath)
      _alternative \
        ':default db:(global user clear)' \
        ':filepath:_files'
    ;;
  esac
}

_cabal_register ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated files]:directory:_files -/' \
    "--user[register in user's local package database]" \
    "--global[register in the system-wide package database]" \
    '--inplace[register in build location]' \
    '--gen-script[generate a script to register later]' \
    '--gen-pkg-config=[generate package registration file]'
}

_cabal_repl ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated build files]:directory:_files -/' \
    $_cabal_programs
}

_cabal_report ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    {-u,--username=}'[hackage username]' \
    {-p,--password=}'[hackage password]'
}

_cabal_run ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated build files]:directory:_files -/' \
    {-j,--jobs=}'[run NUM jobs simultaneously]' \
    $_cabal_programs \
    "--only[don't reinstall add-source dependencies (sandbox-only)]"
}

_cabal_sandbox ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--sandbox=[Sandbox location]:sandbox:_files -/'

  if (( CURRENT == 2 )); then
    local -a _cabal_sandbox_cmds
    _cabal_sandbox_cmds=(
      'init:initialize the sandbox'
      'delete:delete the sandbox'
      'add-source:add sources path to sandbox'
      'hc-pkg:call sandbox related haskell compiler package database'
      'list-sources:list added sources paths'
    )
    _describe -t sandbox-commands 'sandbox-command' _cabal_sandbox_cmds || compadd "$@"
  else
    case $WORDS[3] in
      add-source)
        _arguments \
          {-h,--help}'[show help]' \
          {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
          '--snapshot[take a snapshot instead of creating a link]' \
          '--sandbox=[Sandbox location]:sandbox:_files -/' \
          '*:sources:_files -/'
        ;;
      *)
        _message "unknown cabal sandbox command: $WORDS[3]"
        ;;
    esac
  fi
}

_cabal_sdist ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated files]:directory:_files -/' \
    "--list-sources=[just write a list of the package's sources to a file]:directory:_files -/" \
    '--snapshot[produce a snapshot source distribution]' \
    '--output-directory=[generate a source distribution in the given directory]:directory:_files -/' \
    '--targz[produce a .tar.gz format archive]' \
    '--zip[produce a .zip format archive]'
}

_cabal_test ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    '--builddir=[the directory where Cabal puts generated files]:directory:_files -/' \
    '--log=[log all test suite results to file]:file:_files' \
    '--machine-log=[produce a machine-readable log file]:file:_files' \
    '--show-details=[when to show results of individual test cases?]:filter:(always never failures)' \
    '--keep-tix-files[keep .tix files for HPC between test runs]' \
    '--test-options=[give extra options to test executables]' \
    '--test-option=[give an extra option to test executables]' \
    {-j,--jobs=}'[run NUM jobs simultaneously]' \
    $_cabal_programs \
    "--only[don't reinstall add-source dependencies (sandbox-only)]"
}

_cabal_update ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)'
}

_cabal_upload ()
{
  _arguments \
    {-h,--help}'[show help]' \
    {-v-,--verbose=}'[control verbosity]:level:(0 1 2 3)' \
    {-c,--check}'[do not upload, just do QA checks]' \
    {-u-,--username=}'[hackage username]' \
    {-p-,--password=}'[hackage password]' \
    '*:file:_files -g "*.tar.gz"'
}

_cabal_list_packages () {
  _cabal_get_available_packages
  _cabal_get_available_files=(*.cabal)
  compadd "$@" -a -- _cabal_available_packages _cabal_get_available_files
}

_cabal_get_available_packages ()
{
  if ( [[ ${+_cabal_available_packages} -eq 0 ]] || _cache_invalid CABAL_AVAILABLE_PACKAGES ) &&
  ! _retrieve_cache CABAL_AVAILABLE_PACKAGES;
  then
    _cabal_available_packages=( $(cabal list --simple-output | cut -d' ' -f1 | uniq) )
    _store_cache CABAL_AVAILABLE_PACKAGES _cabal_available_packages
  fi
}

_cabal "$@"
