#!/bin/bash
set -u

source "$(dirname "$0")"/custom/paths.sh

write_dynamic_config_to_backup() {
    local http_auth_enabled=${1}
    local https_auth_enabled=${2}

    dynamic_config_data=$(cat <<EOF
{
    "http": {
        "authenticationEnabled": ${http_auth_enabled}
    },
    "https": {
        "authenticationEnabled": ${https_auth_enabled}
    }
}
EOF
)

    echo -n "${dynamic_config_data}" | setman-backup "httpSettings" dynamic_config.json
}

#Backup http auth enabled
http_auth_enabled=false

if [ -L "${HTTP_AUTH_CONF_SYMLINK}" ] && [ "$(readlink -f "${HTTP_AUTH_CONF_SYMLINK}")" = "$(readlink -f "${HTTP_AUTH_CONF}")" ]; then
    http_auth_enabled=true
fi

https_auth_enabled=false

if [ -L "${HTTPS_AUTH_CONF_SYMLINK}" ] && [ "$(readlink -f "${HTTPS_AUTH_CONF_SYMLINK}")" = "$(readlink -f "${HTTPS_AUTH_CONF}")" ]; then
    https_auth_enabled=true
fi

write_dynamic_config_to_backup "${http_auth_enabled}" "${https_auth_enabled}"
