#!/bin/sh
set -u
umask 077

CONF="/etc/router-egress-emergency-refresh.conf"
RUNNER="/usr/local/sbin/router-egress-emergency-refresh.sh"
HOOK="/usr/local/sbin/router-egress-emergency-decision-hook.sh"
PLANNER="/usr/local/sbin/router-egress-hmn-plan-top5.sh"
HELPER="/usr/local/lib/router-egress-recovery-state.sh"

HOOK_INIT="/etc/init.d/router-egress-emergency-decision"
WATCHER_INIT="/etc/init.d/router-egress-health-repair"
SLOTS_INIT="/etc/init.d/router-egress-slots"
MAPPER_INIT="/etc/init.d/router-egress-mapper"

CACHE_DIR="/root/hmn/cache"
POOL="${CACHE_DIR}/ok-awg1-strict-foreign-latest.tsv"
STATE_DIR="/var/lib/router-egress-recovery"

RUN_ID="$(date -u +%Y%m%d-%H%M%S)-$$"
BACKUP_ROOT="/root/step050m07c2-backup-${RUN_ID}"
ROLLBACK="/root/rollback-step050m07c2-${RUN_ID}.sh"

RUNNER_OUT="/tmp/step050m07c2-runner-${RUN_ID}.out"
RUNNER_ERR="/tmp/step050m07c2-runner-${RUN_ID}.err"
LOG_DELTA="/tmp/step050m07c2-log-${RUN_ID}.delta"

ROLLBACK_READY=false
ROLLBACK_DONE=false

HOOK_WAS_RUNNING=false
WATCHER_WAS_RUNNING=false

fact() {
  printf '__FACT__ %s=%s\n' "$1" "$2"
}

json_block() {
  echo "__JSON_BEGIN__ $1"
  printf '%s\n' "$2"
  echo "__JSON_END__ $1"
}

block() {
  echo "__BLOCK_BEGIN__ $1"
  printf '%s\n' "$2"
  echo "__BLOCK_END__ $1"
}

bool_cmd() {
  if "$@" >/dev/null 2>&1; then
    printf true
  else
    printf false
  fi
}

state_value() {
  key="$1"
  fallback="$2"

  (
    unset REG_STATE_DIR
    . "$HELPER"
    reg_get_state "$key" "$fallback"
  )
}

repair_counter() {
  (
    unset REG_STATE_DIR
    . "$HELPER"
    reg_daily_repair_get
  )
}

strict_all() {
  for interface in vpn1 vpn2 vpn3 vpn4 vpn5; do
    ping -I "$interface" -c 1 -W 3 1.1.1.1 \
      >/dev/null 2>&1 ||
      return 1
  done

  return 0
}

routes_all() {
  for table in 201 202 203 204 205; do
    ip route show table "$table" |
      grep -q '^default ' ||
      return 1
  done

  return 0
}

restore_services() {
  [ "$HOOK_WAS_RUNNING" != true ] ||
    "$HOOK_INIT" start >/dev/null 2>&1 ||
    true

  [ "$WATCHER_WAS_RUNNING" != true ] ||
    "$WATCHER_INIT" start >/dev/null 2>&1 ||
    true
}

stream_delta() {
  file="$1"
  old_offset="$2"
  destination="$3"
  label="$4"

  if [ -f "$file" ]; then
    current_size="$(
      wc -c < "$file" 2>/dev/null |
        tr -d ' ' ||
      echo 0
    )"
  else
    current_size=0
  fi

  case "$current_size" in
    ''|*[!0-9]*)
      current_size=0
      ;;
  esac

  if [ "$current_size" -gt "$old_offset" ]; then
    count=$((current_size - old_offset))

    if [ "$destination" = stderr ]; then
      echo "__LIVE_CHUNK__ source=${label} bytes=${count}" >&2

      dd \
        if="$file" \
        bs=1 \
        skip="$old_offset" \
        count="$count" \
        2>/dev/null >&2
    else
      echo "__LIVE_CHUNK__ source=${label} bytes=${count}"

      dd \
        if="$file" \
        bs=1 \
        skip="$old_offset" \
        count="$count" \
        2>/dev/null
    fi
  fi

  printf '%s' "$current_size"
}

detect_stage() {
  log="$1"

  if [ ! -f "$log" ]; then
    echo "runner_starting"
    return
  fi

  tail_text="$(
    tail -n 120 "$log" 2>/dev/null ||
    true
  )"

  if printf '%s\n' "$tail_text" |
    grep -q '=== hmn-refresh-pool-safe done ==='
  then
    echo "refresh_finished_rebalance_running"

  elif printf '%s\n' "$tail_text" |
    grep -q '=== run manager once after validation ==='
  then
    echo "manager_after_validation"

  elif printf '%s\n' "$tail_text" |
    grep -q '=== done validate-current-pool ==='
  then
    echo "pool_validation_completed"

  elif printf '%s\n' "$tail_text" |
    grep -q 'strict foreign head'
  then
    echo "strict_foreign_pool_validation"

  elif printf '%s\n' "$tail_text" |
    grep -Eq 'download|serverlist|fresh_download'
  then
    echo "provider_download"

  elif printf '%s\n' "$tail_text" |
    grep -Eq 'validate|testing|strict'
  then
    echo "tunnel_validation"

  else
    echo "hmn_refresh_running"
  fi
}

auto_rollback() {
  rc="$?"
  trap - EXIT

  if [ "$rc" -ne 0 ] &&
     [ "$ROLLBACK_READY" = true ] &&
     [ "$ROLLBACK_DONE" != true ]
  then
    if sh "$ROLLBACK"; then
      ROLLBACK_DONE=true
      fact auto_rollback true
    else
      fact auto_rollback false
    fi
  elif [ "$rc" -ne 0 ]; then
    restore_services
  fi

  rm -f \
    "$RUNNER_OUT" \
    "$RUNNER_ERR" \
    "$LOG_DELTA"

  exit "$rc"
}

trap auto_rollback EXIT

for path in \
  "$CONF" \
  "$RUNNER" \
  "$HOOK" \
  "$PLANNER" \
  "$HELPER" \
  "$CACHE_DIR" \
  "$POOL" \
  "$STATE_DIR"
do
  [ -e "$path" ] || {
    echo "__ERROR__ missing=$path"
    exit 21
  }
done

for command_name in \
  sh \
  awk \
  sed \
  grep \
  find \
  sort \
  cp \
  mv \
  rm \
  mkdir \
  date \
  wc \
  sha256sum \
  dd \
  tail \
  kill \
  uci \
  wg \
  ip \
  ping
do
  command -v "$command_name" >/dev/null 2>&1 || {
    echo "__ERROR__ command_missing=$command_name"
    exit 22
  }
done

RAW_PRE="$(
  (
    . "$CONF"
    printf '%s' "${EMERGENCY_COMMIT_ENABLED:-UNSET}"
  )
)"

CONFIRM_TOKEN="$(
  (
    . "$CONF"
    printf '%s' "${EMERGENCY_CONFIRM_TOKEN:-}"
  )
)"

EMERGENCY_LOG="$(
  (
    . "$CONF"
    printf '%s' "${EMERGENCY_LOG:-/var/log/router-egress-emergency-refresh.log}"
  )
)"

fact commit_raw_pre "$RAW_PRE"
fact emergency_log "$EMERGENCY_LOG"

[ "$RAW_PRE" = "1" ] || {
  echo "__ERROR__ expected_commit_raw_1_actual=$RAW_PRE"
  exit 23
}

[ -n "$CONFIRM_TOKEN" ] || {
  echo "__ERROR__ confirm_token_empty"
  exit 24
}

RUNNER_PRE="$("$RUNNER" --dry-run)"
PLANNER_PRE="$("$PLANNER")"

COUNTER_PRE="$(repair_counter)"

POOL_HASH_PRE="$(
  sha256sum "$POOL" |
    sed 's/[[:space:]].*$//'
)"

NETWORK_FILE_HASH_PRE="$(
  sha256sum /etc/config/network |
    sed 's/[[:space:]].*$//'
)"

json_block runner_pre "$RUNNER_PRE"
json_block planner_pre "$PLANNER_PRE"

fact repair_counter_pre "$COUNTER_PRE"
fact pool_hash_pre "$POOL_HASH_PRE"
fact network_file_hash_pre "$NETWORK_FILE_HASH_PRE"

mkdir -p "$BACKUP_ROOT"

cp -a "$CONF" "$BACKUP_ROOT/config.before"
cp -a "$CACHE_DIR" "$BACKUP_ROOT/cache.before"
cp -a "$STATE_DIR" "$BACKUP_ROOT/state.before"
cp -a /etc/config/network "$BACKUP_ROOT/network.before"

find "$STATE_DIR" \
  -maxdepth 4 \
  -type f \
  -name 'rollback-egress*.sh' \
  2>/dev/null |
  sort \
  > "$BACKUP_ROOT/slot-rollbacks.before"

HOOK_WAS_RUNNING="$(bool_cmd "$HOOK_INIT" running)"
WATCHER_WAS_RUNNING="$(bool_cmd "$WATCHER_INIT" running)"

fact hook_was_running "$HOOK_WAS_RUNNING"
fact watcher_was_running "$WATCHER_WAS_RUNNING"

cat > "$ROLLBACK" <<EOF
#!/bin/sh
set -u
umask 077

CONF='$CONF'
CACHE_DIR='$CACHE_DIR'
STATE_DIR='$STATE_DIR'
BACKUP_ROOT='$BACKUP_ROOT'

HOOK_INIT='$HOOK_INIT'
WATCHER_INIT='$WATCHER_INIT'
SLOTS_INIT='$SLOTS_INIT'
MAPPER_INIT='$MAPPER_INIT'

HOOK_WAS_RUNNING='$HOOK_WAS_RUNNING'
WATCHER_WAS_RUNNING='$WATCHER_WAS_RUNNING'

command_errors=0

"\$HOOK_INIT" stop >/dev/null 2>&1 || true
"\$WATCHER_INIT" stop >/dev/null 2>&1 || true

find "\$STATE_DIR" \
  -maxdepth 4 \
  -type f \
  -name 'rollback-egress*.sh' \
  2>/dev/null |
  sort -r \
  > "\$BACKUP_ROOT/slot-rollbacks.after"

while IFS= read -r rollback_path; do
  [ -n "\$rollback_path" ] || continue

  if ! grep -Fxq \
    "\$rollback_path" \
    "\$BACKUP_ROOT/slot-rollbacks.before"
  then
    sh "\$rollback_path" ||
      command_errors=\$((command_errors + 1))
  fi
done < "\$BACKUP_ROOT/slot-rollbacks.after"

cp -a "\$BACKUP_ROOT/config.before" "\$CONF" ||
  command_errors=\$((command_errors + 1))

rm -rf "\$CACHE_DIR"
cp -a "\$BACKUP_ROOT/cache.before" "\$CACHE_DIR" ||
  command_errors=\$((command_errors + 1))

rm -rf "\$STATE_DIR"
cp -a "\$BACKUP_ROOT/state.before" "\$STATE_DIR" ||
  command_errors=\$((command_errors + 1))

cp -a \
  "\$BACKUP_ROOT/network.before" \
  /etc/config/network ||
  command_errors=\$((command_errors + 1))

rm -rf \
  /var/lock/router-egress-emergency-refresh.lock \
  /tmp/hmn-refresh-pool-safe.lock \
  2>/dev/null ||
  true

/etc/init.d/network reload >/dev/null 2>&1 ||
  command_errors=\$((command_errors + 1))

sleep 8

[ ! -x "\$SLOTS_INIT" ] ||
  "\$SLOTS_INIT" restart >/dev/null 2>&1 ||
  command_errors=\$((command_errors + 1))

[ ! -x "\$MAPPER_INIT" ] ||
  "\$MAPPER_INIT" restart >/dev/null 2>&1 ||
  command_errors=\$((command_errors + 1))

if [ "\$HOOK_WAS_RUNNING" = true ]; then
  "\$HOOK_INIT" start >/dev/null 2>&1 ||
    command_errors=\$((command_errors + 1))
fi

if [ "\$WATCHER_WAS_RUNNING" = true ]; then
  "\$WATCHER_INIT" start >/dev/null 2>&1 ||
    command_errors=\$((command_errors + 1))
fi

config_expected="\$(
  sha256sum "\$BACKUP_ROOT/config.before" |
    sed 's/[[:space:]].*\$//'
)"

config_live="\$(
  sha256sum "\$CONF" |
    sed 's/[[:space:]].*\$//'
)"

pool_expected="\$(
  sha256sum \
    "\$BACKUP_ROOT/cache.before/ok-awg1-strict-foreign-latest.tsv" |
    sed 's/[[:space:]].*\$//'
)"

pool_live="\$(
  sha256sum \
    "\$CACHE_DIR/ok-awg1-strict-foreign-latest.tsv" |
    sed 's/[[:space:]].*\$//'
)"

network_expected="\$(
  sha256sum "\$BACKUP_ROOT/network.before" |
    sed 's/[[:space:]].*\$//'
)"

network_live="\$(
  sha256sum /etc/config/network |
    sed 's/[[:space:]].*\$//'
)"

strict=true

for interface in vpn1 vpn2 vpn3 vpn4 vpn5; do
  ping -I "\$interface" -c 1 -W 3 1.1.1.1 \
    >/dev/null 2>&1 ||
    strict=false
done

routes=true

for table in 201 202 203 204 205; do
  ip route show table "\$table" |
    grep -q '^default ' ||
    routes=false
done

hook_restored=true
watcher_restored=true

if [ "\$HOOK_WAS_RUNNING" = true ]; then
  "\$HOOK_INIT" running >/dev/null 2>&1 ||
    hook_restored=false
fi

if [ "\$WATCHER_WAS_RUNNING" = true ]; then
  "\$WATCHER_INIT" running >/dev/null 2>&1 ||
    watcher_restored=false
fi

config_match=false
pool_match=false
network_match=false

[ "\$config_expected" = "\$config_live" ] &&
  config_match=true

[ "\$pool_expected" = "\$pool_live" ] &&
  pool_match=true

[ "\$network_expected" = "\$network_live" ] &&
  network_match=true

echo "rollback_command_errors=\$command_errors"
echo "rollback_config_match=\$config_match"
echo "rollback_pool_match=\$pool_match"
echo "rollback_network_match=\$network_match"
echo "rollback_strict_all=\$strict"
echo "rollback_routes_all=\$routes"
echo "rollback_hook_restored=\$hook_restored"
echo "rollback_watcher_restored=\$watcher_restored"

if [ "\$config_match" = true ] &&
   [ "\$pool_match" = true ] &&
   [ "\$network_match" = true ] &&
   [ "\$strict" = true ] &&
   [ "\$routes" = true ] &&
   [ "\$hook_restored" = true ] &&
   [ "\$watcher_restored" = true ]
then
  echo "rollback_validated=true"
  exit 0
fi

echo "rollback_validated=false"
exit 1
EOF

chmod 700 "$ROLLBACK"
ROLLBACK_READY=true

fact rollback "$ROLLBACK"
fact rollback_exists "$(bool_cmd test -x "$ROLLBACK")"
fact backup_root "$BACKUP_ROOT"

"$WATCHER_INIT" stop >/dev/null 2>&1 || true
"$HOOK_INIT" stop >/dev/null 2>&1 || true

sleep 2

fact watcher_stopped "$(
  if "$WATCHER_INIT" running >/dev/null 2>&1; then
    echo false
  else
    echo true
  fi
)"

fact hook_stopped "$(
  if "$HOOK_INIT" running >/dev/null 2>&1; then
    echo false
  else
    echo true
  fi
)"

TEMP="${CONF}.step050m07c2.$$.tmp"

awk '
  BEGIN {
    count=0
  }

  /^[[:space:]]*EMERGENCY_COMMIT_ENABLED[[:space:]]*=/ {
    print "EMERGENCY_COMMIT_ENABLED=true"
    count++
    next
  }

  {
    print
  }

  END {
    if (count != 1) {
      exit 42
    }
  }
' "$CONF" > "$TEMP" || {
  rm -f "$TEMP"
  echo "__ERROR__ commit_boolean_patch_failed"
  exit 25
}

chmod 600 "$TEMP"
chown 0:0 "$TEMP"
mv "$TEMP" "$CONF"

fact mutation_started true
fact commit_raw_post "$(
  (
    . "$CONF"
    printf '%s' "${EMERGENCY_COMMIT_ENABLED:-UNSET}"
  )
)"

LOG_SIZE_PRE="$(
  if [ -f "$EMERGENCY_LOG" ]; then
    wc -c < "$EMERGENCY_LOG" |
      tr -d ' '
  else
    echo 0
  fi
)"

fact emergency_log_size_pre "$LOG_SIZE_PRE"

: > "$RUNNER_OUT"
: > "$RUNNER_ERR"
: > "$LOG_DELTA"

"$RUNNER" \
  --commit \
  --confirm "$CONFIRM_TOKEN" \
  > "$RUNNER_OUT" \
  2> "$RUNNER_ERR" &

RUNNER_PID=$!

START_EPOCH="$(date +%s)"
LAST_HEARTBEAT=0

OUT_OFFSET=0
ERR_OFFSET=0
LOG_OFFSET="$LOG_SIZE_PRE"

echo
echo "=== LIVE HMN REFRESH START ==="
echo "__HEARTBEAT__ stage=runner_starting elapsed=0s runner_pid=$RUNNER_PID"
echo

while kill -0 "$RUNNER_PID" >/dev/null 2>&1; do
  new_offset="$(
    stream_delta \
      "$RUNNER_OUT" \
      "$OUT_OFFSET" \
      stdout \
      runner_stdout
  )"
  OUT_OFFSET="$new_offset"

  new_offset="$(
    stream_delta \
      "$RUNNER_ERR" \
      "$ERR_OFFSET" \
      stderr \
      runner_stderr
  )"
  ERR_OFFSET="$new_offset"

  new_offset="$(
    stream_delta \
      "$EMERGENCY_LOG" \
      "$LOG_OFFSET" \
      stdout \
      emergency_log
  )"

  if [ "$new_offset" -gt "$LOG_OFFSET" ]; then
    count=$((new_offset - LOG_OFFSET))

    dd \
      if="$EMERGENCY_LOG" \
      bs=1 \
      skip="$LOG_OFFSET" \
      count="$count" \
      2>/dev/null \
      >> "$LOG_DELTA"
  fi

  LOG_OFFSET="$new_offset"

  NOW_EPOCH="$(date +%s)"
  ELAPSED=$((NOW_EPOCH - START_EPOCH))

  if [ $((ELAPSED - LAST_HEARTBEAT)) -ge 15 ]; then
    CURRENT_REFRESH_STAGE="$(detect_stage "$EMERGENCY_LOG")"

    echo
    echo "__HEARTBEAT__ stage=${CURRENT_REFRESH_STAGE} elapsed=${ELAPSED}s runner_pid=${RUNNER_PID}"
    echo

    LAST_HEARTBEAT="$ELAPSED"
  fi

  if [ "$ELAPSED" -ge 1800 ]; then
    echo "__ERROR__ runner_timeout_after=${ELAPSED}s"

    kill "$RUNNER_PID" >/dev/null 2>&1 || true
    sleep 2
    kill -9 "$RUNNER_PID" >/dev/null 2>&1 || true
    wait "$RUNNER_PID" >/dev/null 2>&1 || true

    exit 30
  fi

  sleep 2
done

set +e
wait "$RUNNER_PID"
RUNNER_RC=$?
set -e

OUT_OFFSET="$(
  stream_delta \
    "$RUNNER_OUT" \
    "$OUT_OFFSET" \
    stdout \
    runner_stdout_final
)"

ERR_OFFSET="$(
  stream_delta \
    "$RUNNER_ERR" \
    "$ERR_OFFSET" \
    stderr \
    runner_stderr_final
)"

FINAL_LOG_OFFSET="$(
  stream_delta \
    "$EMERGENCY_LOG" \
    "$LOG_OFFSET" \
    stdout \
    emergency_log_final
)"

if [ "$FINAL_LOG_OFFSET" -gt "$LOG_OFFSET" ]; then
  count=$((FINAL_LOG_OFFSET - LOG_OFFSET))

  dd \
    if="$EMERGENCY_LOG" \
    bs=1 \
    skip="$LOG_OFFSET" \
    count="$count" \
    2>/dev/null \
    >> "$LOG_DELTA"
fi

LOG_OFFSET="$FINAL_LOG_OFFSET"

FINISH_EPOCH="$(date +%s)"
TOTAL_ELAPSED=$((FINISH_EPOCH - START_EPOCH))

echo
echo "=== LIVE HMN REFRESH END ==="
echo "__HEARTBEAT__ stage=runner_finished elapsed=${TOTAL_ELAPSED}s runner_pid=${RUNNER_PID}"
echo

RUNNER_OUTPUT="$(
  cat "$RUNNER_OUT" 2>/dev/null ||
  true
)"

RUNNER_STDERR="$(
  cat "$RUNNER_ERR" 2>/dev/null ||
  true
)"

fact runner_commit_rc "$RUNNER_RC"
fact runner_elapsed_seconds "$TOTAL_ELAPSED"

json_block runner_commit "$RUNNER_OUTPUT"
block runner_commit_stderr_final "$RUNNER_STDERR"

OLD_POOL_FALLBACK=false
FRESH_DOWNLOAD_PROVEN=false

if grep -q \
  'pool_source=old-after-download-fail' \
  "$LOG_DELTA"
then
  OLD_POOL_FALLBACK=true
fi

if grep -Eq \
  'pool_source=fresh|last_fresh_download_rc=0' \
  "$LOG_DELTA"
then
  FRESH_DOWNLOAD_PROVEN=true
fi

fact old_pool_fallback "$OLD_POOL_FALLBACK"
fact fresh_download_proven "$FRESH_DOWNLOAD_PROVEN"

restore_services
sleep 2

RUNNER_POST="$("$RUNNER" --dry-run)"
HOOK_POST="$("$HOOK")"
PLANNER_POST="$("$PLANNER")"

json_block runner_post "$RUNNER_POST"
json_block hook_post "$HOOK_POST"
json_block planner_post "$PLANNER_POST"

fact state_mode_post "$(state_value mode UNKNOWN)"
fact state_status_post "$(
  state_value last_emergency_refresh_status UNKNOWN
)"
fact state_epoch_post "$(
  state_value last_emergency_refresh_epoch 0
)"
fact repair_counter_post "$(repair_counter)"

fact hook_running_post "$(bool_cmd "$HOOK_INIT" running)"
fact watcher_running_post "$(bool_cmd "$WATCHER_INIT" running)"
fact strict_all_post "$(bool_cmd strict_all)"
fact routes_all_post "$(bool_cmd routes_all)"

fact emergency_lock_present "$(
  bool_cmd test -e /var/lock/router-egress-emergency-refresh.lock
)"

fact refresh_lock_present "$(
  bool_cmd test -e /tmp/hmn-refresh-pool-safe.lock
)"

fact direct_failopen_changed false

[ "$RUNNER_RC" -eq 0 ] || {
  echo "__ERROR__ runner_rc=$RUNNER_RC"
  exit 31
}

printf '%s\n' "$RUNNER_OUTPUT" |
  grep -q \
    '"decision"[[:space:]]*:[[:space:]]*"refresh_ok_rebalance_ok"' || {
  echo "__ERROR__ runner_decision_not_success"
  exit 32
}

[ "$OLD_POOL_FALLBACK" = false ] || {
  echo "__ERROR__ fresh_download_failed_old_pool_fallback"
  exit 33
}

[ "$FRESH_DOWNLOAD_PROVEN" = true ] || {
  echo "__ERROR__ fresh_download_not_proven"
  exit 34
}

printf '%s\n' "$RUNNER_POST" |
  grep -q \
    '"decision"[[:space:]]*:[[:space:]]*"cooldown_active"' || {
  echo "__ERROR__ cooldown_not_active"
  exit 35
}

printf '%s\n' "$PLANNER_POST" |
  grep -q \
    '"changes_count"[[:space:]]*:[[:space:]]*0' || {
  echo "__ERROR__ planner_not_converged"
  exit 36
}

[ "$(state_value mode UNKNOWN)" = NORMAL ] || {
  echo "__ERROR__ state_not_normal"
  exit 37
}

[ "$(state_value last_emergency_refresh_status UNKNOWN)" = refresh_ok_rebalance_ok ] || {
  echo "__ERROR__ state_status_not_success"
  exit 38
}

[ "$(bool_cmd strict_all)" = true ] || {
  echo "__ERROR__ strict_not_all"
  exit 39
}

[ "$(bool_cmd routes_all)" = true ] || {
  echo "__ERROR__ routes_not_all"
  exit 40
}

[ "$(bool_cmd "$HOOK_INIT" running)" = true ] || {
  echo "__ERROR__ hook_not_restored"
  exit 41
}

[ "$(bool_cmd "$WATCHER_INIT" running)" = true ] || {
  echo "__ERROR__ watcher_not_restored"
  exit 42
}

[ "$(bool_cmd test -e /var/lock/router-egress-emergency-refresh.lock)" = false ] || {
  echo "__ERROR__ emergency_lock_remains"
  exit 43
}

[ "$(bool_cmd test -e /tmp/hmn-refresh-pool-safe.lock)" = false ] || {
  echo "__ERROR__ refresh_lock_remains"
  exit 44
}

echo "__TRACE__ stage=complete"

trap - EXIT

rm -f \
  "$RUNNER_OUT" \
  "$RUNNER_ERR" \
  "$LOG_DELTA"

exit 0
