#!/bin/bash
set -u

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

dynamic_config_data=$(setman-restore "httpSettings" dynamic_config.json)

#Restore http auth enabled
http_auth_enabled=$(echo "${dynamic_config_data}" | jq -r '.http.authenticationEnabled')

if [ "${http_auth_enabled}" = "true" ]; then
    ln -sf "${HTTP_AUTH_CONF}" "${HTTP_AUTH_CONF_SYMLINK}"
else
    if [ -L "${HTTP_AUTH_CONF_SYMLINK}" ]; then
        rm -f "${HTTP_AUTH_CONF_SYMLINK}"
    fi
fi

#Restore https auth enabled
https_auth_enabled=$(echo "${dynamic_config_data}" | jq -r '.https.authenticationEnabled')

if [ "${https_auth_enabled}" = "true" ]; then
    ln -sf "${HTTPS_AUTH_CONF}" "${HTTPS_AUTH_CONF_SYMLINK}"
else
    if [ -L "${HTTPS_AUTH_CONF_SYMLINK}" ]; then
        rm -f "${HTTPS_AUTH_CONF_SYMLINK}"
    fi
fi
