#!/bin/bash
set -u

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

dyn_config=$(setman-restore "${HTTP_SETS_NAME}" "${HTTP_SETS_CFG_NAME}")

if [ $? -ne 0 ]; then
    setman-log debug "http/dynamic-config restore not found a file"
else
    cat >"${HTTP_CFG_FILE}" <<<"$dyn_config"

    setman-log debug "http/dynamic-config restored successfully"
fi

# restore httpOptionalServices

tmpzip=$(mktemp)
setman-restore "httpOptionalServices" optionalServices.zip > "${tmpzip}"
if  [[ $? -eq 0 ]]; then
    # -o: overwrite existing files without prompting
    # -d: destination dir
    unzip -o "${tmpzip}" -d "${OPTIONAL_SERVICE_DIR}"
    unzip_ec=$?
    rm "${tmpzip}"
    if [[ ${unzip_ec} -ne 0 ]]; then
        setman-log error "Restoring httpOptionalServices: unzip failed with error code: ${unzip_ec}"
        exit "${unzip_ec}"
    fi
fi