#!/bin/bash

exname=$(basename $0)

prefix_cmd="
. /etc/shlib

include common
include parse

. ../lib/common

get_challenge_type() {
    local i
    echo \"Calling get_challenge_type\" >&2
    ((i=0))
    for arg in \"\$@\"; do
        echo \"  arg\$((i++)):\"
        echo \"\$arg\" | prefix \"  | \"
    done >&2
    echo \"\$GET_CHALLENGE_TYPE\"
}
export -f get_challenge_type

"



try "
SERVICE_NAME='\$SERVICE_NAME'
GET_CHALLENGE_TYPE=foo
get_dc_env '' create bar
"
is errlvl 0
is err part "\
Calling get_challenge_type
  arg0:
  |
  arg1:
  | create
  arg2:
  | bar
" RTRIM
is out '$SERVICE_NAME:
  docker-compose:
    environment:
      CHALLENGE_TYPE: foo' RTRIM


try "
SERVICE_NAME='\$SERVICE_NAME'
GET_CHALLENGE_TYPE=foo
get_dc_env '
email: foo@example.com
' create bar
"
is errlvl 0
is err part "\
Calling get_challenge_type
  arg0:
  |
  | email: foo@example.com
  |
  arg1:
  | create
  arg2:
  | bar
" RTRIM
is out '$SERVICE_NAME:
  docker-compose:
    environment:
      LETSENCRYPT_USER_MAIL: foo@example.com
      CHALLENGE_TYPE: foo' RTRIM


try "
SERVICE_NAME='\$SERVICE_NAME'
GET_CHALLENGE_TYPE=foo
get_dc_env '
email: foo@example.com
env:
' create bar
" "environment def is empty"
is errlvl 0
is out '$SERVICE_NAME:
  docker-compose:
    environment:
      LETSENCRYPT_USER_MAIL: foo@example.com
      CHALLENGE_TYPE: foo' RTRIM

try "
SERVICE_NAME='\$SERVICE_NAME'
GET_CHALLENGE_TYPE=foo
get_dc_env '
email: foo@example.com
env:
  ignore: x
  ovh:
    foo: 1
    bar: 2
  wiz:
    foo: 1
' create bar
" "environment def without provider"
is errlvl 0
is out '$SERVICE_NAME:
  docker-compose:
    environment:
      LETSENCRYPT_USER_MAIL: foo@example.com
      LEXICON_OVH_FOO: 1
      LEXICON_OVH_BAR: 2
      LEXICON_WIZ_FOO: 1
      LEXICON_PROVIDER: ovh
      CHALLENGE_TYPE: foo' RTRIM


try "
SERVICE_NAME='\$SERVICE_NAME'
GET_CHALLENGE_TYPE=foo
get_dc_env '
email: foo@example.com
env:
  ignore: y
  ovh:
    foo: 1
    bar: 2
  wiz:
    foo: 1
  provider: wiz
' create bar
" "environment def with provider"
is errlvl 0
is out '$SERVICE_NAME:
  docker-compose:
    environment:
      LETSENCRYPT_USER_MAIL: foo@example.com
      LEXICON_OVH_FOO: 1
      LEXICON_OVH_BAR: 2
      LEXICON_WIZ_FOO: 1
      LEXICON_PROVIDER: wiz
      CHALLENGE_TYPE: foo' RTRIM