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

readonly HLCC_ROOT="/var/lib/guanghu/code-channel/candidates/hlcc-v16.0.1"
readonly HLCC_PID_FILE="${HLCC_ROOT}/hlcc.pid"

if [[ "$(id -un)" != "guanghu" ]]; then
  echo "Refusing to stop a domestic isolated candidate as any user other than guanghu." >&2
  exit 2
fi

if [[ ! -f "${HLCC_PID_FILE}" ]]; then
  echo "HLCC candidate PID file does not exist."
  exit 0
fi

readonly candidate_pid="$(cat "${HLCC_PID_FILE}")"
if kill -0 "${candidate_pid}" 2>/dev/null; then
  kill "${candidate_pid}"
fi

rm "${HLCC_PID_FILE}"
echo "HLCC isolated candidate stopped."
