#!/usr/bin/env bash
#
# Wrapper script for telora-daemon that restarts on exit code 75 (auto-update).
#
# Usage:
#   telora-daemon-wrapper.sh [daemon args...]
#
# The daemon exits with code 75 after installing a newer version of itself.
# This wrapper simply re-launches the daemon, which now runs the new code.
#
# For systemd users, you can instead configure:
#   RestartForceExitStatus=75
# in the [Service] section and run telora-daemon directly.

set -euo pipefail

# Default to 'run' subcommand if no args given
ARGS=("$@")
if [ ${#ARGS[@]} -eq 0 ]; then
  ARGS=("run")
fi

while true; do
  telora-daemon "${ARGS[@]}"
  rc=$?
  if [ "$rc" -eq 75 ]; then
    echo "[wrapper] Daemon exited with code 75 (auto-updated), restarting..."
    continue
  fi
  exit "$rc"
done
