#!/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, as root: openchronology-reset-admin-password
set -eu
APP=/usr/local/lib/openchronology
CONFIG=/usr/local/etc/openchronology/openchronology.env
if [ "$(id -u)" -ne 0 ]; then
  echo "Run as root: 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 /
su -m openchronology -c "env OCH_CONFIG=$CONFIG /usr/local/bin/node $APP/server/reset-admin-password.mjs"
