#!/bin/bash

exname=$(basename $0)

prefix_cmd="
. /etc/shlib

include common
include parse

. ../lib/common

has_existing_cert() {
    echo \"Calling has_existing_cert $*\" >&2
    [ \"\$HAS_EXISTING_CERT\" == 'yes' ]
}
export -f has_existing_cert

letsencrypt_cert_info() {
    echo \"Calling letsencrypt_cert_info $*\" >&2
    echo \"\$LETSENCRYPT_CERT_INFO\"
}
export -f letsencrypt_cert_info

"


try "
HAS_EXISTING_CERT=  ## False
valid_existing_cert 30 'www.example.com'
"
is errlvl 1
is err 'Calling has_existing_cert' RTRIM
is out '' RTRIM


try "
HAS_EXISTING_CERT=yes  ## False
LETSENCRYPT_CERT_INFO='
domains: www.example.com
remaining: 74
'
valid_existing_cert 30 'www.example.com'
" "existing and valid cert"
is errlvl 0
is err part 'Calling has_existing_cert' RTRIM
is err part 'Querying www.example.com for previous info...' RTRIM
is err part 'Calling letsencrypt_cert_info' RTRIM
is out '' RTRIM


try "
HAS_EXISTING_CERT=yes  ## False
LETSENCRYPT_CERT_INFO='
domains: www.example.com
remaining: 74
'
valid_existing_cert 90 'www.example.com'
" "days validity beneath threshold"
is errlvl 1
is out '' RTRIM


try "
HAS_EXISTING_CERT=yes  ## False
LETSENCRYPT_CERT_INFO='
domains: www.example.com example.com
remaining: 74
'
valid_existing_cert 30 'www.example.com'
" "domains mismatch 1"
is errlvl 2
is out '' RTRIM

try "
HAS_EXISTING_CERT=yes  ## False
LETSENCRYPT_CERT_INFO='
domains: www.example.com
remaining: 74
'
valid_existing_cert 30 'www.example.com' example.com
" "domains mismatch 2"
is errlvl 2
is out '' RTRIM


try "
HAS_EXISTING_CERT=yes  ## False
LETSENCRYPT_CERT_INFO='
domains: www.example.com
remaining: EXPIRED
'
valid_existing_cert 30 www.example.com
" "expired"
is errlvl 1
is out '' RTRIM