#!/bin/bash

exname=$(basename $0)

compose_core=$(which compose-core) || {
    echo "Requires compose-core executable to be in \$PATH." >&2
    exit 1
}

fetch-def() {
    local path="$1" fname="$2"
    ( . "$path" 1>&2 || {
          echo "Failed to load '$path'." >&2
          exit 1
      }
      declare -f "$fname"
    )
}

prefix_cmd="
. /etc/shlib

include common
include parse

. ../lib/common

$(fetch-def "$compose_core" yaml_get_values)
$(fetch-def "$compose_core" yaml_get_interpret)

" || {
    echo "Couldn't build prefix cmd" >&2
    exit 1
}

# mock
cfg-get-value() {
    local key="$1"
    shyaml get-value "$key" 2>/dev/null
}
export -f cfg-get-value

yaml_get_interpret() {
    shyaml get-value
}
export -f yaml_get_interpret


export state_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
trap "rm -rf \"$state_tmpdir\"" EXIT

##
## Tests
##

try "
get_domains '
'"
is errlvl 1
is err reg 'Error: .*domain option.*'
is out ''


try "
get_domains '
domain: toto
'"
noerror
is out 'toto
'


try "
get_domains '
domain: toto titi
'"
noerror
is out 'toto titi
'

try "
get_domains '
domain:
- toto
'"
noerror
is out 'toto
'


try "
get_domains '
server-aliases:
'"
is errlvl 1
is err part 'Error: '
is err part 'No domain name set'

try "
get_domains '
domain:
server-aliases:
'"
is errlvl 1
is err part 'Error: '
is err part 'No domain name set'


try "
get_domains '
domain:
server-aliases:
- toto
'"
is errlvl 1
is err part 'Error: '
is err part "You can't specify server aliases if you don't have a domain"

try "
get_domains '
domain: foo
server-aliases:
- bar
'"
noerror
is out 'foo bar
'


try "
get_domains '
domain: foo
server-aliases: bar
'"
noerror
is out 'foo bar
'


try "
get_domains '
domain:
- foo
server-aliases: bar
'"
noerror
is out 'foo bar
'

try "
get_domains '
domain:
- foo{1,2} bar
server-aliases: wiz
'"
noerror
is out 'foo1 foo2 bar wiz
'

try "
get_domains '
domain:
- foo{1,2} bar
server-aliases: foo1
'"
noerror
is out 'foo1 foo2 bar
'

try "
get_domains '
domain:
- foo{1,2} bar
- \"*.zoo\"
server-aliases: foo1
'"
noerror
is out 'foo1 foo2 bar *.zoo
'

try "
get_domains '
domain: foo+ bar
'"
is errlvl 1
is err part 'Error: '
is err part 'Invalid domain value'


try "
get_domains '
domain: 
' '
options.service-domain-map:
'" "empty service-domain-map"
is errlvl 1
is err part 'Error: '
is err part 'No domain name set'
is err part 'service-domain-map'


try "
BASE_SERVICE_NAME=foo
get_domains '
domain: 
' '
options:
  service-domain-map:
    wiz: bar
'" "no map matching in service-domain-map"
is errlvl 1
is err part 'Error: '
is err part 'No domain name set'
is err part 'service-domain-map'


try "
export BASE_SERVICE_NAME=wiz
get_domains '
domain: 
' '
options:
  service-domain-map:
    wiz: bar
'" "matching map in service-domain-map"
noerror
is out 'bar
'


try "
export BASE_SERVICE_NAME=wiz
get_domains '
domain: 
' '
options:
  service-domain-map:
    wiz?: bar
    wiz: bar2
'" "only first matching map in service-domain-map"
noerror
is out 'bar
'


try "
export BASE_SERVICE_NAME=wiz
get_domains '
domain: 
' '
options:
  service-domain-map:
    \"[w]i?zz?\": bar
'" "map are regex in service-domain-map"
noerror
is out 'bar
'


try "
export BASE_SERVICE_NAME=wiz
get_domains '
domain: 
' '
options:
  service-domain-map:
    (w)i(z): bar\$1\$2
'" "regex capture in service-domain-map"
noerror
is out 'barwz
'


try "
export BASE_SERVICE_NAME=wiz
get_domains '
domain: 
' '
options:
  service-domain-map:
    .*: \$0.shrubbery
'" "regex capture 2 in service-domain-map"
noerror
is out 'wiz.shrubbery
'


try "
export BASE_SERVICE_NAME=wiz
get_domains '
domain: 
' '
options:
  service-domain-map:
    .*: \$x
'" "refuse other variables in service-domain-map"
is errlvl 1
is err part 'Error: '
is err part 'Invalid mapping value'


try "
export BASE_SERVICE_NAME=wiz
get_domains '
domain: 
' '
options:
  service-domain-map:
    .*:
      - \$0.example.com
      - my-\$0.domain.org

'" "list is possible as value of service-domain-map"
noerror
is out 'wiz.example.com my-wiz.domain.org
'