#!/bin/zsh

### Parse antidote's bundle DSL.
#function __antidote_parse_bundles {
  emulate -L zsh; setopt local_options $_adote_funcopts
  $__adote_awkcmd '
    BEGIN { RS="[\r\n]" }

    # skip comments and empty lines
    /^ *$/ || /^ *#/ {next}

    # strip trailing comments
    { sub(/[ \t]#.*$/,"",$0) }

    # escape leading $ variables
    { sub(/^\$/,"\\$",$0) }

    # handle extension functionality (eg :use ohmyzsh)
    $1~/^:/ {
      sub(/^:/,"",$1)
      printf "antidote-script-" $1
      for (i=2; i<=NF; i++) {
        printf " %s",$i
      }
      printf "\n"
      next
    }

    # move flags to front and call antidote-script
    {
      sub(/ #.*$/,"",$0)
      printf "antidote-script"
      for (i=2; i<=NF; i++) {
        sub(/^/,"--",$i)
        sub(/:/," ",$i)
        printf " %s",$i
      }
      printf " %s\n",$1
    }
  ' "$@"
#}
