#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/../env/lib.sh"

offline_nginx_root() {
  printf '%s/nginx\n' "$(offline_envs_root)"
}

offline_nginx_state_dir() {
  printf '%s/state\n' "$(offline_nginx_root)"
}

offline_nginx_conf_dir() {
  printf '%s/conf.d\n' "$(offline_nginx_root)"
}

offline_nginx_rendered_conf() {
  local env_name="$1"
  printf '%s/%s.conf\n' "$(offline_nginx_conf_dir)" "${env_name}"
}

offline_nginx_live_conf() {
  local env_name="$1"
  printf '%s/nexuscrm-%s.conf\n' "${OFFLINE_NGINX_LIVE_CONF_DIR:-/etc/nginx/conf.d}" "${env_name}"
}

offline_nginx_active_slot_file() {
  local env_name="$1"
  printf '%s/%s.active-slot\n' "$(offline_nginx_state_dir)" "${env_name}"
}

offline_runtime_env_file() {
  local env_name="$1"
  local target="$2"
  printf '%s/%s/%s/env/runtime.env\n' "$(offline_envs_root)" "${env_name}" "${target}"
}

offline_write_active_slot() {
  local env_name="$1"
  local slot="$2"
  local file
  file="$(offline_nginx_active_slot_file "${env_name}")"
  mkdir -p "$(dirname "${file}")"
  printf '%s\n' "${slot}" > "${file}"
}

offline_runtime_frontend_dist() {
  local runtime_env_file="$1"
  local env_root
  env_root="$(awk -F= '$1=="ENV_ROOT"{print $2}' "${runtime_env_file}")"
  if [[ -z "${env_root}" ]]; then
    echo "ENV_ROOT missing in ${runtime_env_file}" >&2
    return 1
  fi
  printf '%s/frontend-dist\n' "${env_root}"
}

offline_render_nginx_conf() {
  local env_name="$1"
  local target="$2"
  local runtime_env_file="$3"
  local template_file="$4"
  local rendered_conf
  local listen_port forwarded_proto server_name backend_port hubspot_options_adapter_port frontend_dist

  rendered_conf="$(offline_nginx_rendered_conf "${env_name}")"
  listen_port="$(awk -F= '$1=="NGINX_LISTEN_PORT"{print $2}' "${runtime_env_file}")"
  forwarded_proto="$(awk -F= '$1=="NGINX_FORWARDED_PROTO"{print $2}' "${runtime_env_file}")"
  server_name="$(awk -F= '$1=="SERVER_NAME"{print $2}' "${runtime_env_file}")"
  backend_port="$(awk -F= '$1=="BACKEND_PORT"{print $2}' "${runtime_env_file}")"
  hubspot_options_adapter_port="$(awk -F= '$1=="EXP_HUBSPOT_OPTIONS_ADAPTER_PORT"{print $2}' "${runtime_env_file}")"
  frontend_dist="$(offline_runtime_frontend_dist "${runtime_env_file}")"

  : "${listen_port:=80}"
  : "${hubspot_options_adapter_port:=$((backend_port + 101))}"
  if [[ -z "${forwarded_proto}" ]]; then
    forwarded_proto="https"
  fi
  if [[ -z "${server_name}" || -z "${backend_port}" ]]; then
    echo "SERVER_NAME/BACKEND_PORT missing in ${runtime_env_file}" >&2
    return 1
  fi

  mkdir -p "$(offline_nginx_conf_dir)"
  sed \
    -e "s|__NGINX_LISTEN_PORT__|${listen_port}|g" \
    -e "s|__NGINX_FORWARDED_PROTO__|${forwarded_proto}|g" \
    -e "s|__SERVER_NAME__|${server_name}|g" \
    -e "s|__FRONTEND_DIST__|${frontend_dist}|g" \
    -e "s|__BACKEND_PORT__|${backend_port}|g" \
    -e "s|__EXP_HUBSPOT_OPTIONS_ADAPTER_PORT__|${hubspot_options_adapter_port}|g" \
    "${template_file}" > "${rendered_conf}"

  printf '%s\n' "${rendered_conf}"
}

offline_install_nginx_conf_link() {
  local env_name="$1"
  local rendered_conf="$2"
  local live_conf
  live_conf="$(offline_nginx_live_conf "${env_name}")"
  mkdir -p "$(dirname "${live_conf}")"
  ln -sfn "${rendered_conf}" "${live_conf}"
  printf '%s\n' "${live_conf}"
}
