#!/bin/sh
# Copyright (c) 2026 Athan Clark. SPDX-License-Identifier: GPL-3.0-only
# Sets the administrator's password back to ADMIN_INITIAL_PASSWORD from the configuration
# (#118), for an administrator who cannot sign in: the ADMIN_USERNAME account. Its sessions are
# signed out. Edit ADMIN_INITIAL_PASSWORD first to choose a new password.
# Usage: sudo openchronology-reset-admin-password
set -eu
APP=/usr/lib/openchronology
CONFIG=/etc/openchronology/openchronology.env
if [ "$(id -u)" -ne 0 ]; then
  echo "Run as root: sudo openchronology-reset-admin-password" >&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
cd /
runuser -u openchronology -- env OCH_CONFIG="$CONFIG" "$APP/node/bin/node" \
  "$APP/server/reset-admin-password.mjs"
