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

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
SNAPSHOT="${ROOT}/server-config-snapshot"

require_root() {
  if [[ "${EUID}" -ne 0 ]]; then
    echo "Run as root: sudo $0" >&2
    exit 1
  fi
}

install_unit() {
  local name="$1"
  install -m 0644 "${SNAPSHOT}/${name}" "/etc/systemd/system/${name}"
}

require_root

for unit in \
  digest-orchestrator.service \
  digest-orchestrator.timer \
  digest-vpn-health.service \
  digest-vpn-health.timer
do
  install_unit "$unit"
done

systemctl daemon-reload
systemctl enable --now digest-vpn-health.timer
systemctl enable --now digest-orchestrator.timer

systemctl list-timers --all | grep digest || true

