#!/bin/bash
# Recreate taler-hacktivism-exchange-ansible with systemd (regional-currency style).
# Image: Containerfile (debian + systemd + ssh). Port: only 9011.
# Ansible: ansible_connection=podman  →  systemctl inside container.
set -euo pipefail

NAME=taler-hacktivism-exchange-ansible
IMAGE=localhost/taler-hacktivism-exchange-ansible:base
ROOT="$(cd "$(dirname "$0")" && pwd)"

cd "$ROOT"
podman build -f Containerfile -t "$IMAGE"

echo "Stopping/removing existing $NAME (if any)..."
podman rm -f "$NAME" 2>/dev/null || true

podman run -d \
  --name "$NAME" \
  --hostname "$NAME" \
  --network pasta \
  --systemd=always \
  -p 9011:9011 \
  --label org.hacktivism.service=taler-exchange \
  --label org.hacktivism.host_port=9011 \
  --label org.hacktivism.site=exchange.hacktivism.ch \
  --label org.hacktivism.currency=GOA \
  --label org.hacktivism.managed_by=ansible-taler-exchange \
  --label org.hacktivism.init=systemd \
  "$IMAGE" \
  /usr/sbin/init

echo "waiting for systemd..."
for i in $(seq 1 30); do
  if podman exec "$NAME" systemctl is-system-running 2>/dev/null | grep -Eq 'running|degraded'; then
    break
  fi
  sleep 1
done

podman ps --filter "name=$NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}\t{{.Image}}"
echo -n "PID1: "
podman exec "$NAME" cat /proc/1/comm
echo -n "systemd: "
podman exec "$NAME" systemctl is-system-running || true
echo "Next: ./deploy-hacktivism-goa.sh"
