#!/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
    return \"\$VALID_EXISTING_CERT\"
}
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
    return \$CRT
}
export -f crt


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


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


"

##
## 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 create\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=
VALID_EXISTING_CERT=1
crt_create
"
is err 'Error: At least one domain should be provided as argument.
usage:
  crt create [-h|--help]
  crt create MAIN_DOMAIN [ALT_DOMAINS...]' RTRIM
is errlvl 1


try "
exname=\"crt create\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=
VALID_EXISTING_CERT=1
crt_create --help
"
is err ''
is out 'usage:
  crt create [-h|--help]
  crt create MAIN_DOMAIN [ALT_DOMAINS...]' RTRIM
is errlvl 0


try "
CRT=0
exname=\"crt create\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=
VALID_EXISTING_CERT=1
LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
crt_create www.example.com
" "invalid cert"
is err 'Calling: get_compose_service_def
  arg0:
  | $SERVICE_NAME
Calling valid_existing_cert
  arg0:
  | 30
  arg1:
  | www.example.com
Calling crt
  arg0:
  |
  arg1:
  | create
  arg2:
  | www.example.com
Calling letsencrypt_set_renew_before_expiry
  arg0:
  | www.example.com
  arg1:
  | 30' RTRIM
is out '' RTRIM
is errlvl 0


try "
exname=\"crt create\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=
VALID_EXISTING_CERT=0
LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
crt_create www.example.com
" "valid cert"
is err 'Calling: get_compose_service_def
  arg0:
  | $SERVICE_NAME
Calling valid_existing_cert
  arg0:
  | 30
  arg1:
  | www.example.com
II A valid cert already exists for domain www.example.com.' RTRIM
is out '' RTRIM
is errlvl 0


try "
exname=\"crt create\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=
VALID_EXISTING_CERT=0
LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
crt_create www.example.com -f
" "valid cert but force"
is err 'Calling: get_compose_service_def
  arg0:
  | $SERVICE_NAME
Calling valid_existing_cert
  arg0:
  | 30
  arg1:
  | www.example.com
Calling crt
  arg0:
  |
  arg1:
  | create
  arg2:
  | www.example.com
Calling letsencrypt_set_renew_before_expiry
  arg0:
  | www.example.com
  arg1:
  | 30' RTRIM
is out '' RTRIM
is errlvl 0


try "
exname=\"crt create\"
SERVICE_NAME='\$SERVICE_NAME'
LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
GET_COMPOSE_SERVICE_DEF='
a: 1
options:
  foo: bar'
VALID_EXISTING_CERT=1
crt_create www.example.com
" "not valid, cfg is passed correctly"
is err reg 'Calling crt
  arg0:
  . foo: bar
  arg1:
  . create
  arg2:
  . www.example.com' RTRIM
is out '' RTRIM
is errlvl 0


try "
exname=\"crt create\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF='
a: 1
options:
  foo: bar'
VALID_EXISTING_CERT=2
LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
LETSENCRYPT_CERT_DELETE=yes
crt_create www.example.com
" "not valid, already existing diff domain"
is err 'Calling: get_compose_service_def
  arg0:
  | $SERVICE_NAME
Calling valid_existing_cert
  arg0:
  | 30
  arg1:
  | www.example.com
Error: Domain mismatch detected, lets delete previous cert.
Calling letsencrypt_cert_delete
  arg0:
  | www.example.com
Error: Previous cert for www.example.com deleted.
Calling crt
  arg0:
  | foo: bar
  arg1:
  | create
  arg2:
  | www.example.com
Calling letsencrypt_set_renew_before_expiry
  arg0:
  | www.example.com
  arg1:
  | 30' RTRIM
is out '' RTRIM
is errlvl 0



try "
exname=\"crt create\"
SERVICE_NAME='\$SERVICE_NAME'
LETSENCRYPT_SET_RENEW_BEFORE_EXPIRY=yes
GET_COMPOSE_SERVICE_DEF='
a: 1
options:
  foo: bar
  renew-before-expiry: 15
'
VALID_EXISTING_CERT=1
crt_create www.example.com
" "not valid, renew-before-expiry is used"
is err reg 'Calling valid_existing_cert
  arg0:
  . 15
  arg1:
  . www.example.com
' RTRIM
is out '' RTRIM
is errlvl 0



try "
crt() { return 1; }
exname=\"crt create\"
SERVICE_NAME='\$SERVICE_NAME'
GET_COMPOSE_SERVICE_DEF=''
VALID_EXISTING_CERT=1
crt_create www.example.com
" "valid cert but force"
is err part "Error: Certificate creation/renew failed for domain 'www.example.com'." RTRIM
is out '' RTRIM
is errlvl 1