#!/bin/bash
# Ensure merchant helper processes are running (no systemd).
# Prefer run as taler-merchant-httpd; root may use runuser.
#
# Usage:
#   ensure_merchant_helpers.sh
#   (as root) ensure_merchant_helpers.sh   # re-exec as taler-merchant-httpd
set -euo pipefail

CONF="${TALER_MERCHANT_CONFIG:-/etc/taler-merchant/taler-merchant.conf}"
LOG_DIR="${TALER_MERCHANT_LOG_DIR:-/var/log/taler-merchant}"

# As root: start wirewatch supervisor (needs root for runuser), then re-exec as httpd.
if [ "$(id -un)" = "root" ]; then
  mkdir -p "$LOG_DIR"
  if [ -x /usr/local/bin/taler-merchant-wirewatch-supervise.sh ]; then
    if ! ps -eo args= 2>/dev/null | grep -q 'taler-merchant-wirewatch-supervise\.sh'; then
      echo "start: taler-merchant-wirewatch-supervise"
      nohup /usr/local/bin/taler-merchant-wirewatch-supervise.sh \
        >>"$LOG_DIR/wirewatch-supervise.nohup" 2>&1 </dev/null &
      disown 2>/dev/null || true
    else
      echo "already: taler-merchant-wirewatch-supervise"
    fi
  fi
  exec runuser -u taler-merchant-httpd -- "$0" "$@"
fi

if [ "$(id -un)" != "taler-merchant-httpd" ]; then
  echo "run as taler-merchant-httpd or root" >&2
  exit 1
fi

mkdir -p "$LOG_DIR"
chmod 755 "$LOG_DIR" 2>/dev/null || true

# Linux COMM is 15 chars — do not use pgrep -x for taler-merchant-wirewatch etc.
is_running() {
  local bin="$1"
  local base
  base=$(basename "$bin")
  ps -u "$(id -un)" -o args= 2>/dev/null | grep -qE "(^|/)[${base:0:1}]${base:1}( |$)" \
    || pgrep -u "$(id -un)" -f "(^|/)(${base})( |$)" >/dev/null 2>&1
}

start_one() {
  local name="$1"
  local bin="$2"
  shift 2
  if is_running "$bin"; then
    echo "already: $name"
    return 0
  fi
  echo "start: $name"
  nohup "$bin" "$@" >>"$LOG_DIR/${name}.log" 2>&1 </dev/null &
  disown 2>/dev/null || true
  local i
  for i in 1 2 3 4 5 6; do
    sleep 0.4
    if is_running "$bin"; then
      echo "  ok: $name"
      return 0
    fi
  done
  echo "  FAIL: $name did not stay up (see $LOG_DIR/${name}.log)" >&2
  tail -20 "$LOG_DIR/${name}.log" 2>/dev/null || true
  return 1
}

ec=0
# Helpers needed for settlement / ops (not only httpd).
start_one taler-merchant-webhook /usr/bin/taler-merchant-webhook || ec=1
start_one taler-merchant-kyccheck /usr/bin/taler-merchant-kyccheck || ec=1
# Prefer already-running supervisor (started as root above); else bare wirewatch.
if ps -eo args= 2>/dev/null | grep -q 'taler-merchant-wirewatch-supervise\.sh'; then
  echo "already: taler-merchant-wirewatch (via supervise)"
elif is_running /usr/bin/taler-merchant-wirewatch; then
  echo "already: taler-merchant-wirewatch"
else
  start_one taler-merchant-wirewatch /usr/bin/taler-merchant-wirewatch \
    -c "$CONF" -L INFO || ec=1
fi
start_one taler-merchant-depositcheck /usr/bin/taler-merchant-depositcheck || ec=1
start_one taler-merchant-exchangekeyupdate /usr/bin/taler-merchant-exchangekeyupdate || ec=1
start_one taler-merchant-reconciliation /usr/bin/taler-merchant-reconciliation || ec=1

if ! is_running taler-merchant-httpd; then
  echo "start: taler-merchant-httpd"
  nohup /usr/bin/taler-merchant-httpd --log=info \
    >>"$LOG_DIR/taler-merchant-httpd-$(date +%Y-%m-%d).log" 2>&1 </dev/null &
  disown 2>/dev/null || true
  sleep 1
  is_running taler-merchant-httpd || ec=1
fi

echo "--- live merchant ---"
ps -eo pid,user,stat,etime,args 2>/dev/null \
  | grep taler-merchant | grep -vE 'grep| Z |ensure_merchant' || true

exit "$ec"
