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

HOST=""
REMOTE_ROOT="/opt/lingniu-go-native"

usage() {
  cat <<'USAGE'
Usage:
  scripts/install-identity-writer-service.sh --host root@115.29.187.205 [--remote-root DIR]

Prepares the native identity-writer service and switches Gateway registration
persistence to Kafka delegation. The existing Gateway binary ignores the new
environment switch, so this is safe to run before staging the first release
that contains identity-writer.
USAGE
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --host)
      HOST="${2:-}"
      shift 2
      ;;
    --remote-root)
      REMOTE_ROOT="${2:-}"
      shift 2
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "unknown argument: $1" >&2
      usage >&2
      exit 2
      ;;
  esac
done

if [[ -z "$HOST" ]]; then
  echo "--host is required" >&2
  exit 2
fi

ssh "$HOST" "REMOTE_ROOT='$REMOTE_ROOT' bash -s" <<'REMOTE'
set -euo pipefail

env_dir="${REMOTE_ROOT}/env"
gateway_env="${env_dir}/gateway.env"
identity_env="${env_dir}/identity-writer.env"
unit="/etc/systemd/system/lingniu-go-identity-writer.service"
mkdir -p "${env_dir}"

cat >"${identity_env}" <<'ENV'
KAFKA_GROUP=go-identity-writer
KAFKA_TOPIC=vehicle.raw.go.jt808.v1
KAFKA_START_OFFSET=last
HEALTH_ADDR=127.0.0.1:20217
MYSQL_ENSURE_SCHEMA=true
MYSQL_MAX_OPEN_CONNS=8
MYSQL_MAX_IDLE_CONNS=4
MYSQL_CONN_MAX_LIFETIME_SECONDS=300
IDENTITY_WRITER_BATCH_SIZE=500
IDENTITY_WRITER_BATCH_WAIT_MS=20
IDENTITY_WRITER_RETRY_DELAY_MS=1000
JT808_REGISTRATION_LOCATION_TOUCH_INTERVAL_SECONDS=600
LOCAL_TZ=Asia/Shanghai
ENV
chmod 0640 "${identity_env}"

touch "${gateway_env}"
if grep -q '^JT808_REGISTRATION_GATEWAY_WRITES_ENABLED=' "${gateway_env}"; then
  sed -i 's/^JT808_REGISTRATION_GATEWAY_WRITES_ENABLED=.*/JT808_REGISTRATION_GATEWAY_WRITES_ENABLED=false/' "${gateway_env}"
else
  printf '\nJT808_REGISTRATION_GATEWAY_WRITES_ENABLED=false\n' >>"${gateway_env}"
fi

cat >"${unit}" <<UNIT
[Unit]
Description=Lingniu Go JT808 Identity Writer
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
WorkingDirectory=${REMOTE_ROOT}/current
EnvironmentFile=${env_dir}/base.env
EnvironmentFile=${identity_env}
Environment=GOMEMLIMIT=192MiB
ExecStart=${REMOTE_ROOT}/current/identity-writer
Restart=always
RestartSec=3
LimitNOFILE=1048576
MemoryLimit=256M
KillSignal=SIGTERM
TimeoutStopSec=30

[Install]
WantedBy=multi-user.target
UNIT

systemctl daemon-reload
systemctl enable lingniu-go-identity-writer.service
if [[ -x "${REMOTE_ROOT}/current/identity-writer" ]]; then
  systemd-analyze verify "${unit}"
  systemctl restart lingniu-go-identity-writer.service
  systemctl is-active lingniu-go-identity-writer.service
else
  echo "identity-writer binary is not in current release yet; service prepared for the next deploy"
fi
echo "identity writer service prepared; gateway MySQL registration writes delegated"
REMOTE
