#!/usr/bin/env bash

set -Eeuo pipefail
umask 077

readonly EXPECTED_HOSTNAME='sqjcmqtt01'
readonly EXPECTED_IP='10.2.203.108'
readonly NODE_ENV='/etc/zhct/node.env'
readonly OBSERVED_PRE_REPAIR_SHA256='45b1f19e7275638260aa9f97dd4ad79c01449a5f9df3b04b9d91cfbc7391c0aa'
readonly EXPECTED_POST_REPAIR_SHA256='6185d5b1780e5f9673c3eb8d6a40526afb2732f41f2532a7664eed1e8c8d5a6b'
readonly AUDIT_LOG='/root/q141-node-env-repair.log'

q141_backup=''
q141_temp=''
q141_mutation_started=0
q141_success=0

exec > >(tee "${AUDIT_LOG}") 2>&1

q141_repair_finish() {
  q141_rc=$?
  trap - EXIT
  set +e
  if [[ "${q141_success}" -ne 1 &&
        "${q141_mutation_started}" -eq 1 &&
        -n "${q141_backup}" &&
        -f "${q141_backup}" ]]; then
    install -m 0640 -o root -g root "${q141_backup}" "${NODE_ENV}"
    printf '%s\n' 'ROLLBACK_STATUS=PRE_REPAIR_NODE_ENV_RESTORED'
  elif [[ "${q141_success}" -eq 1 ]]; then
    printf '%s\n' 'ROLLBACK_STATUS=NOT_REQUIRED_BACKUP_RETAINED'
  else
    printf '%s\n' 'ROLLBACK_STATUS=NOT_REQUIRED_NO_MUTATION'
  fi
  if [[ -n "${q141_temp}" ]]; then
    rm -f "${q141_temp}"
  fi
  printf '%s\n' 'Q141_NODE_ENV_REPAIR_END'
  printf 'Q141_NODE_ENV_REPAIR_RC=%s\n' "${q141_rc}"
  exit "${q141_rc}"
}
trap q141_repair_finish EXIT

printf '%s\n' 'Q141_NODE_ENV_REPAIR_BEGIN'

hostname -s | grep -Fx "${EXPECTED_HOSTNAME}"
ip -br -4 addr show ens192 | grep -F "${EXPECTED_IP}/24"

printf '%s\n' '=== PRE-REPAIR GATE ==='
test -f "${NODE_ENV}"
test "$(stat -c '%U:%G' "${NODE_ENV}")" = 'root:root'
test "$(stat -c '%a' "${NODE_ENV}")" = '640'
printf '%s  %s\n' "${OBSERVED_PRE_REPAIR_SHA256}" "${NODE_ENV}" | sha256sum -c -
test ! -e /etc/zhct/secrets/emqx.cluster-cookie
printf '%s\n' 'COOKIE_ABSENT'

for q141_unit in emqx.service mosquitto.service; do
  q141_enabled=$(systemctl is-enabled "${q141_unit}" 2>&1 || true)
  q141_active=$(systemctl is-active "${q141_unit}" 2>&1 || true)
  printf '%s enabled=%s active=%s\n' \
    "${q141_unit}" "${q141_enabled}" "${q141_active}"
  test "${q141_enabled}" = 'disabled'
  test "${q141_active}" = 'inactive'
done

q141_backup="/root/q141-node-env-pre-repair-$(date +%Y%m%dT%H%M%S%z).backup"
install -m 0600 -o root -g root "${NODE_ENV}" "${q141_backup}"
printf 'BACKUP_PATH=%s\n' "${q141_backup}"
printf 'BACKUP_OWNER_MODE=%s:%s\n' \
  "$(stat -c '%U:%G' "${q141_backup}")" \
  "$(stat -c '%a' "${q141_backup}")"
printf 'BACKUP_SHA256=%s\n' "$(sha256sum "${q141_backup}" | awk '{print $1}')"

q141_temp=$(mktemp /etc/zhct/.node.env.q141.XXXXXX)
printf '%s\n' \
  'ROLE=emqx' \
  'NODE_KEY=emqx01' \
  'NODE_IP=10.2.203.108' \
  'NODE_NAME=sqjcmqtt01' >"${q141_temp}"
chown root:root "${q141_temp}"
chmod 0640 "${q141_temp}"
printf '%s  %s\n' "${EXPECTED_POST_REPAIR_SHA256}" "${q141_temp}" | sha256sum -c -

q141_mutation_started=1
mv -f "${q141_temp}" "${NODE_ENV}"
q141_temp=''

printf '%s\n' '=== POST-REPAIR VERIFY ==='
test "$(stat -c '%U:%G' "${NODE_ENV}")" = 'root:root'
test "$(stat -c '%a' "${NODE_ENV}")" = '640'
printf '%s  %s\n' "${EXPECTED_POST_REPAIR_SHA256}" "${NODE_ENV}" | sha256sum -c -
cmp -s \
  <(printf '%s\n' \
    'ROLE=emqx' \
    'NODE_KEY=emqx01' \
    'NODE_IP=10.2.203.108' \
    'NODE_NAME=sqjcmqtt01') \
  "${NODE_ENV}"
printf '%s\n' 'NODE_ENV_EXACT_CONTENT=PASS'

test ! -e /etc/zhct/secrets/emqx.cluster-cookie
printf '%s\n' 'COOKIE_ABSENT'
for q141_unit in emqx.service mosquitto.service; do
  q141_enabled=$(systemctl is-enabled "${q141_unit}" 2>&1 || true)
  q141_active=$(systemctl is-active "${q141_unit}" 2>&1 || true)
  test "${q141_enabled}" = 'disabled'
  test "${q141_active}" = 'inactive'
done

printf '%s\n' 'COOKIE_NOT_GENERATED'
printf '%s\n' 'ACTIVATE_ROLE_NOT_RUN'
printf '%s\n' 'SERVICE_START_STOP_NOT_PERFORMED'
printf '%s\n' 'Q141_NODE_ENV_REPAIR_PASS'
q141_success=1
