#!/bin/bash
set -e

REPO_DIR="/opt/aufmassweb"
COMPOSE_FILE="docker-compose.deploy.yml"
LOG_FILE="/var/log/aufmassweb-deploy.log"

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}

log "=== Deployment gestartet ==="

cd "$REPO_DIR"

log "Pull von Gitea..."
git pull origin main 2>&1 | tee -a "$LOG_FILE"

log "Docker Compose build..."
docker compose -f "$COMPOSE_FILE" build 2>&1 | tee -a "$LOG_FILE"

log "Docker Compose up (Rolling-Restart)..."
docker compose -f "$COMPOSE_FILE" up -d --remove-orphans 2>&1 | tee -a "$LOG_FILE"

log "Alte Images bereinigen..."
docker image prune -f 2>&1 | tee -a "$LOG_FILE"

log "=== Deployment erfolgreich abgeschlossen ==="
