[buildout]
extends =
	../pygolang/buildout.cfg

parts =

[virtual-env-base]
recipe = slapos.recipe.build
name = ${:_buildout_section_name_}
default-instance = true
init =
  from zc.buildout.easy_install import working_set
  import os
  name = options['name']
  eggs = options.get('eggs')
  self.message = options.get('message')
  self.chain = options.get('chain')
  environment = options.get('environment')
  scripts = options.get('scripts')

  eggs_template = """
  [.%(name)s.install-eggs]
  recipe = zc.recipe.egg
  eggs =
    %(eggs)s
  %(scripts)s

  [.%(name)s.install-interpreter]
  <= python-interpreter
  eggs +=
    %(eggs)s
  """

  instance_template = """
  [.%(name)s.instance]
  recipe = slapos.recipe.template
  output = ${buildout:directory}/instance.cfg
  depends = $%(cookbook)s
  inline =
    [buildout]
    parts = publish

    eggs-directory = ${buildout:eggs-directory}
    develop-eggs-directory = ${buildout:develop-eggs-directory}

    [publish]
    recipe = slapos.cookbook:publish
    activate-script = %(location)s
  """

  if eggs:
    self.buildout.parse(eggs_template % {
      "eggs": "\n  ".join(e.strip() for e in eggs.splitlines()),
      "name": name,
      "scripts": "scripts = " + scripts if scripts else "",
    })

  if is_true(options.get('default-instance')):
    self.buildout.parse(instance_template % {
      "cookbook": "{slapos-cookbook:recipe}",
      "location": location,
      "name": name,
    })

  env = {
    "PATH": self.buildout['buildout']['bin-directory'] + ":\$PATH",
    "PS1": "\"(" + self.name + ") \$PS1\"",
  }

  if environment:
    for line in environment.splitlines():
      key, value = line.split("=", 1)
      env[key.strip()] = value.strip()

  self.env = env

install =
  message = ""
  if self.message:
    message = "echo " + "\n  echo ".join(
      "%r" % line for line in self.message.splitlines())
    message += "\n  echo \'\'"

  chain = ""
  if self.chain:
    chain = "source " + "\n  source ".join(
      "%r" % line for line in self.chain.splitlines())

  with open(location, "w") as f:
    f.write(options['template'] % {
      "env": " ".join("%s %s" % (k, v) for k, v in self.env.items()),
      "message": message,
      "chain": chain,
    })

# Template virtual env for bash shell in posix
[virtual-env-base:posix]
template =
  if type deactivate > /dev/null 2>&1
  then
    export _OLD_PARAM=( "$@")
    set %(env)s
    while [ "$1" ]; do
      if ! ( echo $_LIST_OLD_VENV | grep $1 ) > /dev/null 2>&1
      then
        eval "export _OLD_VENV_$1=\$$1"
        eval "export _LIST_OLD_VENV=\"$1 \$_LIST_OLD_VENV\""
      fi
      eval "export $1=\"$2\""
      shift
      shift
    done
    if [[ -n "$_OLD_PARAM" ]]; then
      set "$${_OLD_PARAM[@]}"
    fi
    unset _OLD_PARAM
    %(chain)s
    %(message)s
  else
    deactivate () {
      set $_LIST_OLD_VENV
      while [ "$1" ]; do
        eval "if [ \"\$_OLD_VENV_$1\" ]; then $1=\$_OLD_VENV_$1; else unset $1; fi; unset \$_OLD_VENV_$1"
        shift
      done
      unset -f deactivate
      unset _LIST_OLD_VENV
    }
    export _OLD_PARAM=( "$@" )
    set %(env)s
    while [ "$1" ]; do
      eval "_OLD_VENV_$1=\$$1"
      eval "export $1=\"$2\""
      eval "export _LIST_OLD_VENV=\"$1 \$_LIST_OLD_VENV\""
      shift
      shift
    done
    if [[ -n "$_OLD_PARAM" ]]; then
      set "$${_OLD_PARAM[@]}"
    fi
    unset _OLD_PARAM
    %(chain)s
    %(message)s
  fi