#!/bin/bash

exname=$(basename $0)

prefix_cmd="
. /etc/shlib

include common
include parse

. ../lib/common

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

will_need_http_access() {
    local i
    echo \"Calling will_need_http_access\" >&2
    ((i=0))
    for arg in \"\$@\"; do
        echo \"  arg\$((i++)):\"
        echo \"\$arg\" | prefix \"  | \"
    done >&2
    [ \"\$WILL_NEED_HTTP_ACCESS\" == 'yes' ]
}
export -f will_need_http_access



"

##
## Mocks
##

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

file_put() {
    echo "file_put $1"
    cat - | prefix "  | "
}
export -f file_put

docker() {
    local i
    echo "Calling: docker" >&2
    ((i=0))
    for arg in "$@"; do
        echo "  arg$((i++)):"
        echo "$arg" | prefix "  | "
    done >&2
    if [ "$1" == "ps" ]; then
        echo "$DOCKER_PS"
    fi
}
export -f docker

yaml_key_val_str() {
    printf "%s:\n%s" "$1" "$(echo "$2" | prefix "  ")"
}
export -f yaml_key_val_str

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



try "
SERVICE_NAME='\$SERVICE_NAME'
WILL_NEED_HTTP_ACCESS=
crt '' create www.example.com
"
is err reg 'Calling get_dc_env
  arg0:
  |
  arg1:
  | create
  arg2:
  | www.example.com
Calling will_need_http_access
  arg0:
  |
  arg1:
  | create
  arg2:
  | www.example.com
Calling: compose
.*
  | run
.*
  | letsencrypt
.*
  | crt
.*
  | create
.*
  | www.example.com' RTRIM
is errlvl 0
is out ''

try "
SERVICE_NAME='\$SERVICE_NAME'
GET_DC_ENV='
\$SERVICE_NAME:
  environment:
    LETSENCRYPT_USER_MAIL: foo@example.com
    LEXICON_OVH_FOO: 1
    LEXICON_PROVIDER: wiz
'
WILL_NEED_HTTP_ACCESS=
crt '' create www.example.com
"
is err reg 'Calling: compose
.*
  | --add-compose-content
.*
  | docker-compose:
  |   $SERVICE_NAME:
  |     environment:
  |       LETSENCRYPT_USER_MAIL: foo@example.com
  |       LEXICON_OVH_FOO: 1
  |       LEXICON_PROVIDER: wiz
.*
  | run
.*
  | letsencrypt
.*
  | crt
.*
  | create
.*
  | www.example.com' RTRIM
is errlvl 0


try "
SERVICE_NAME='\$SERVICE_NAME'
GET_DC_ENV='
\$SERVICE_NAME:
  environment:
    LETSENCRYPT_USER_MAIL: foo@example.com
    LEXICON_OVH_FOO: 1
    LEXICON_PROVIDER: wiz
'
WILL_NEED_HTTP_ACCESS=yes
DOCKER_PS=
crt '' create www.example.com
" "need http acces, no docker on port 80"
is err part 'Calling: docker
  arg0:
  | ps' RTRIM
is errlvl 0
is out ''

try "
GET_DC_ENV='
\$SERVICE_NAME:
  environment:
    LETSENCRYPT_USER_MAIL: foo@example.com
    LEXICON_OVH_FOO: 1
    LEXICON_PROVIDER: wiz
'
WILL_NEED_HTTP_ACCESS=yes
DOCKER_PS='
docker_1
docker_2
'
crt '' create www.example.com
" "need http acces, 2 dockers on port 80"
is err reg 'II Attempting to clear port 80 by stopping docker_1
Calling: docker
  arg0:
  . stop
.*
  . docker_1
II Attempting to clear port 80 by stopping docker_2
Calling: docker
  arg0:
  . stop
.*
  . docker_2
Calling: compose
.*
II Attempting restart docker_1
Calling: docker
  arg0:
  . start
.*
  . docker_1
II Attempting restart docker_2
Calling: docker
  arg0:
  . start
.*
  . docker_2' RTRIM
is errlvl 0
is out ''