#!/bin/bash

# --- Configuration ---
# Directory containing your docker-compose.yml
COMPOSE_DIR=$(pwd)
# The directory where your compressed backup file is located (MUST match backup.sh)
BACKUP_DIR="/home/Dejan/backups/filerun" # <--- ADJUST THIS PATH
# A temporary directory to extract the backup files
TEMP_DIR="/tmp/filerun_restore_stage"

# Define the local directories (volumes)
DB_DIR="db_data"
HTML_DIR="filerun_html"
USER_DATA_DIR="user_data"

# --- Script Start ---

echo "--- Starting FileRun Restore Process ---"

# 1. Ask the user for the backup file name
echo "Available backups in $BACKUP_DIR:"
ls -1 "$BACKUP_DIR"/*.tar.gz
echo "-----------------------------------"
read -p "Enter the FULL name of the backup file (e.g., filerun_backup_20251129_174239.tar.gz): " BACKUP_FILE

FULL_BACKUP_PATH="$BACKUP_DIR/$BACKUP_FILE"

# 2. Validate the backup file
if [ ! -f "$FULL_BACKUP_PATH" ]; then
    echo "ERROR: Backup file not found at $FULL_BACKUP_PATH. Exiting."
    exit 1
fi

# 3. Stop the FileRun and MariaDB services
echo "Stopping FileRun services..."
docker compose down
if [ $? -ne 0 ]; then
    echo "ERROR: Docker Compose down failed. Cannot continue restoration. Exiting."
    exit 1
fi

# 4. Clean up current volume data (IMPORTANT: irreversible step)
read -p "WARNING: This will DELETE all current data in your $DB_DIR, $HTML_DIR, and $USER_DATA_DIR directories. Continue? (y/N): " CONFIRM_DELETE
if [[ "$CONFIRM_DELETE" != "y" ]]; then
    echo "Restore cancelled by user. Restarting services..."
    docker compose up -d
    exit 0
fi

echo "Deleting current data..."
rm -rf "$COMPOSE_DIR/$DB_DIR"
rm -rf "$COMPOSE_DIR/$HTML_DIR"/*
rm -rf "$COMPOSE_DIR/$USER_DATA_DIR"/*

# Recreate essential directories (in case they were fully deleted)
mkdir -p "$COMPOSE_DIR/$DB_DIR"
mkdir -p "$COMPOSE_DIR/$HTML_DIR"
mkdir -p "$COMPOSE_DIR/$USER_DATA_DIR"

# 5. Extract the backup archive
echo "Extracting backup from $BACKUP_FILE..."
rm -rf "$TEMP_DIR"
mkdir -p "$TEMP_DIR"

# Extract the tar.gz file into the temporary directory
tar -xzf "$FULL_BACKUP_PATH" -C "$TEMP_DIR"
if [ $? -ne 0 ]; then
    echo "ERROR: Extraction failed. Exiting."
    exit 1
fi

# Determine the extracted folder name (it's usually the backup name without .tar.gz)
EXTRACTED_FOLDER=$(basename "$BACKUP_FILE" .tar.gz)

# 6. Copy extracted data back to the persistent volumes
echo "Copying extracted data back to live volumes..."
# Use rsync or cp -a to copy contents, excluding the root folder itself
cp -a "$TEMP_DIR/$EXTRACTED_FOLDER/$DB_DIR/." "$COMPOSE_DIR/$DB_DIR/"
cp -a "$TEMP_DIR/$EXTRACTED_FOLDER/$HTML_DIR/." "$COMPOSE_DIR/$HTML_DIR/"
cp -a "$TEMP_DIR/$EXTRACTED_FOLDER/$USER_DATA_DIR/." "$COMPOSE_DIR/$USER_DATA_DIR/"

# 7. Set correct permissions for the restored data
echo "Setting permissions for UID/GID 33 (www-data)..."
chown -R 33:33 "$COMPOSE_DIR/$HTML_DIR"
chown -R 33:33 "$COMPOSE_DIR/$DB_DIR"
chown -R 33:33 "$COMPOSE_DIR/$USER_DATA_DIR"

# 8. Cleanup and Restart
echo "Cleaning up temporary files..."
rm -rf "$TEMP_DIR"

echo "Starting FileRun services back up..."
docker compose up -d

echo "--- Restore Complete! ---"
echo "FileRun should now be accessible and restored to the state of $BACKUP_FILE."
echo "-------------------------"