#compdef darwin-debug
# vim:ft=zsh:et:sts=2:sw=2

# -----------------------------------------------------------------------------
# The BSD-3-Clause License
# Copyright (c) 2016, Koichi Shiraishi
# All rights reserved.
#
# NAME
#     darwin-debug -- posix spawn a process that is stopped at the entry point
#                     for debugging.
#
# SYNOPSIS
#     darwin-debug --unix-socket=<SOCKET> [--arch=<ARCH>] [--working-dir=<PATH>] [--disable-aslr] [--no-env] [--setsid] [--help] -- <PROGRAM> [<PROGRAM-ARG> <PROGRAM-ARG> ....]
#
# DESCRIPTION
#     darwin-debug will exec itself into a child process <PROGRAM> that is
#     halted for debugging. It does this by using posix_spawn() along with
#     darwin specific posix_spawn flags that allows exec only (no fork), and
#     stop at the program entry point. Any program arguments <PROGRAM-ARG> are
#     passed on to the exec as the arguments for the new process. The current
#     environment will be passed to the new process unless the "--no-env"
#     option is used. A unix socket must be supplied using the
#     --unix-socket=<SOCKET> option so the calling program can handshake with
#     this process and get its process id.
#
# EXAMPLE
#    darwin-debug --arch=i386 -- /bin/ls -al /tmp
#
# -----------------------------------------------------------------------------

function _darwin-debug() {
  local context curcontext=$curcontext state line ret=1
  declare -A opt_args

  _arguments -C \
    '--unix-socket=[socket]:SOCKET:_socket' \
    '--arch=[arch]:ARCH:(i386 x86_64)' \
    '--working-dir=[wording directory]:PATH:_directories' \
    '--disable-aslr[disable aslr]' \
    '--no-env[no set environment variable]' \
    '--setsid[set sid]' \
    '--help[show help]' \
    && ret=0
    # TODO(zchee): support PROGRAM arguments
    # -- <PROGRAM> [<PROGRAM-ARG> <PROGRAM-ARG> ....] 
    # '*:: :->args' \

  # case $state in
  #   (args)
  #     case $words[1] in
  #       subcommand1)
  #         _arguments \
  #           '-foo[subcommand2 -foo argument comment]'
  #           '*:file:_files'
  #         ;;
  #       subcommand2)
  #         _arguments \
  #           '-bar[subcommand2 -bar argument comment]' \
  #           '-baz[subcommand2 -baz argument comment]'
  #           '*:devices:_devices'
  #         ;;
  #     esac
  #     ;;
  # esac

  return ret
}

_darwin-debug "$@"
