#!/bin/bash
set -euo pipefail
cd /opt/realistic-vision-v4

# Expand swap to 8G if needed
if [ "$(swapon --show --bytes | awk 'NR==2{print $3}')" -lt 7000000000 ] 2>/dev/null; then
  swapoff /swapfile || true
  fallocate -l 8G /swapfile
  chmod 600 /swapfile
  mkswap /swapfile
  swapon /swapfile
fi
free -h

# Free disk
docker builder prune -af || true
docker image prune -f || true

# Rebuild app layer and restart
docker compose down || true
docker volume rm realistic-vision-v4_hf_cache 2>/dev/null || true
docker compose build
docker compose up -d

echo "Container started. Streaming logs..."
docker logs -f realistic-vision-v4
