#!/bin/sh
# Copyright (c) 2026 Athan Clark. SPDX-License-Identifier: GPL-3.0-only
# Brings the OpenChronology database up to this package's schema, then (re)starts the server.
# Safe to run more than once. Usage: sudo openchronology-migrate [--no-restart]
set -eu
APP=/usr/lib/openchronology
CONFIG=/etc/openchronology/openchronology.env
if [ "$(id -u)" -ne 0 ]; then
  echo "Run as root: sudo openchronology-migrate" >&2
  exit 1
fi
if [ ! -r "$CONFIG" ]; then
  echo "$CONFIG is missing; reinstall openchronology-server or restore it from a backup." >&2
  exit 1
fi
# Node reads the same KEY=value file the services use, as systemd does (OCH_CONFIG: a # inside
# a value is part of it, where node --env-file would cut the value there).
runuser -u openchronology -- env OCH_CONFIG="$CONFIG" "$APP/node/bin/node" "$APP/server/migrate.mjs"
if [ "${1:-}" != "--no-restart" ] && [ -d /run/systemd/system ]; then
  systemctl enable --quiet openchronology.service
  systemctl restart openchronology.service
  echo "OpenChronology restarted."
fi
