#!/bin/bash

echo "=== YourPart Rollback Script ==="
echo ""

# Bestätigung anfordern
read -p "Are you sure you want to rollback? This will stop the new service and restore the old configuration. (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "Rollback cancelled."
    exit 1
fi

echo "Starting rollback process..."

# Neuen Service stoppen und deaktivieren
echo "Stopping new YourPart service..."
sudo systemctl stop yourpart.service
sudo systemctl disable yourpart.service

# Alte Apache-Konfiguration wiederherstellen
echo "Restoring old Apache configuration..."

# Neue Konfigurationen deaktivieren
sudo a2dissite yourpart-http
sudo a2dissite yourpart-https

# Alte Konfiguration wieder aktivieren (falls vorhanden)
if [ -f "/etc/apache2/sites-available/yourpart" ]; then
    echo "Re-enabling old configuration..."
    sudo a2ensite yourpart
fi

# Apache neu laden
echo "Reloading Apache..."
sudo systemctl reload apache2

# Neue Dateien entfernen
echo "Removing new files..."
sudo rm -rf /opt/yourpart/backend
sudo rm -rf /opt/yourpart/frontend
sudo rm -f /etc/systemd/system/yourpart.service
sudo rm -f /etc/apache2/sites-available/yourpart-http.conf
sudo rm -f /etc/apache2/sites-available/yourpart-https.conf

# Systemd neu laden
sudo systemctl daemon-reload

echo ""
echo "=== Rollback Completed ==="
echo "The old configuration has been restored."
echo ""
echo "To verify:"
echo "  - Check if the old service is running on port 2030"
echo "  - Check Apache status: sudo systemctl status apache2"
echo "  - Check if the old site is accessible"
