#!/bin/bash

set -u

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

backup_dynamic_config() {

    if [ ! -f "${HTTP_CFG_FILE}" ]; then
        setman-log error "lwr-http-app backup failed: ${HTTP_CFG_FILE} not found"
        exit 1
    fi

    setman-backup "${HTTP_SETS_NAME}" "${HTTP_SETS_CFG_NAME}" < "${HTTP_CFG_FILE}"

    if [ $? -ne 0 ]; then
        setman-log error "lwr-http-app/dynamic-config backup failed"
        exit 1
    fi
}

# httpOptionalServices
backup_optional_service_configs() {
    cd "${OPTIONAL_SERVICE_DIR}" || return
    # only if directory is not empty
    optional_config_files=$(find "." -name '*.json')
    if [[ -n "${optional_config_files}" ]]; then
        zip -r - . | setman-backup "httpOptionalServices" optionalServices.zip
    fi
}

backup_optional_service_configs
backup_dynamic_config