#!/bin/bash

exname=$(basename $0)

prefix_cmd="
. /etc/shlib

include common
include parse

. ../lib/common

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

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

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


"

##
## Mocks
##

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



try "
exname=\"crt renew\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=
crt_renew xxx
"
is err 'Error: No argument required
usage: $
  crt renew [-h|--help]' RTRIM
is errlvl 1
is out ''

try "
exname=\"crt renew\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=
GET_DOMAIN_LIST=
crt_renew
"
is err part 'II No domain founds' RTRIM
is errlvl 0
is out ''


try "
exname=\"crt renew\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF='
options:
  wiz: foo
'
GET_DOMAIN_LIST='
www.example.com:
    remaining: 20
foo.bar:
    remaining: 32
'
crt_renew
" "2 certs, one need renew, one is ok"
is err part 'II Renewing domain www.example.com (20 days left)' RTRIM
is err part 'Calling crt
  arg0:
  | wiz: foo
  arg1:
  | renew
  arg2:
  | www.example.com
'
is err part 'II Domain foo.bar does not need renewing (32 days left).' RTRIM
is errlvl 0
is out ''



try "
exname=\"crt renew\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF='
options:
  wiz: foo
  renew-before-expiry: 15
'
GET_DOMAIN_LIST='
www.example.com:
    remaining: 45
'
crt_renew
" "setting renew-before-expiry"
is err part 'II Domain www.example.com does not need renewing (45 days left).' RTRIM
is errlvl 0
is out ''


try "
exname=\"crt renew\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=
GET_DOMAIN_LIST='
www.example.com:
    remaining: EXPIRED
'
crt_renew
" "expired cert"
is err part 'II Renewing domain www.example.com (expired).' RTRIM
is errlvl 0
is out ''


try "
crt() { ! [[ \"\$3\" =~ ^wiz|foo$ ]]; }
exname=\"crt renew\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=
GET_DOMAIN_LIST='
www.example.com:
    remaining: EXPIRED
foo:
    remaining: EXPIRED
bar:
    remaining: 98
wiz:
    remaining: 10
'
crt_renew
" "some failed renewal"
is err part 'II Renewing domain www.example.com (expired).' RTRIM
is err part 'Error: At least one domain failed to be renewed: foo wiz' RTRIM
is errlvl 1
is out ''