#!/bin/bash
# Script to migrate from Git clone deployment to standard Docker deployment

set -e

# Print usage
print_usage() {
  echo "Usage: $0 [OPTIONS]"
  echo "Migrate from Git clone deployment to standard Docker deployment"
  echo ""
  echo "Options:"
  echo "  -s, --source CONTAINER    Source container name (default: jwpw-app-staging)"
  echo "  -c, --compose FILE        Docker Compose file for new deployment (default: _deploy/standard-docker-compose.yml)"
  echo "  -b, --backup DIR          Backup directory (default: ./deployment_backup)"
  echo "  -h, --help                Show this help message"
  exit 1
}

# Default values
SOURCE_CONTAINER="jwpw-app-staging"
COMPOSE_FILE="_deploy/standard-docker-compose.yml"
BACKUP_DIR="./deployment_backup"

# Parse command line arguments
while [[ $# -gt 0 ]]; do
  case $1 in
    -s|--source)
      SOURCE_CONTAINER="$2"
      shift 2
      ;;
    -c|--compose)
      COMPOSE_FILE="$2"
      shift 2
      ;;
    -b|--backup)
      BACKUP_DIR="$2"
      shift 2
      ;;
    -h|--help)
      print_usage
      ;;
    *)
      echo "Unknown option: $1"
      print_usage
      ;;
  esac
done

echo "=========================================================="
echo "Migration from Git clone deployment to standard deployment"
echo "=========================================================="
echo "Source container: $SOURCE_CONTAINER"
echo "Compose file: $COMPOSE_FILE"
echo "Backup directory: $BACKUP_DIR"
echo ""

# Confirm before proceeding
read -p "Do you want to proceed? (y/n) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  echo "Migration aborted."
  exit 1
fi

# Check if source container exists
if ! docker ps -a | grep -q "$SOURCE_CONTAINER"; then
  echo "Error: Source container '$SOURCE_CONTAINER' not found."
  exit 1
fi

# Create backup directory
mkdir -p "$BACKUP_DIR/permits"
mkdir -p "$BACKUP_DIR/uploads"
mkdir -p "$BACKUP_DIR/logs"

echo "Step 1: Backing up data from source container..."
docker cp "$SOURCE_CONTAINER:/app/public/content/permits/." "$BACKUP_DIR/permits/"
docker cp "$SOURCE_CONTAINER:/app/public/content/uploads/." "$BACKUP_DIR/uploads/"
docker cp "$SOURCE_CONTAINER:/app/logs/." "$BACKUP_DIR/logs/"
echo "Backup completed successfully."

echo "Step 2: Checking if the Compose file exists..."
if [ ! -f "$COMPOSE_FILE" ]; then
  echo "Error: Compose file '$COMPOSE_FILE' not found."
  exit 1
fi

echo "Step 3: Deploying new Docker containers..."
docker-compose -f "$COMPOSE_FILE" up -d
echo "New containers deployed."

# Get the new container name
NEW_CONTAINER=$(docker-compose -f "$COMPOSE_FILE" ps -q nextjs-app)
if [ -z "$NEW_CONTAINER" ]; then
  echo "Error: Could not find new container. Please check the deployment."
  exit 1
fi

echo "Step 4: Restoring data to new container..."
docker cp "$BACKUP_DIR/permits/." "$NEW_CONTAINER:/app/public/content/permits/"
docker cp "$BACKUP_DIR/uploads/." "$NEW_CONTAINER:/app/public/content/uploads/"
echo "Data restored successfully."

echo "Step 5: Setting correct permissions..."
docker exec -it "$NEW_CONTAINER" chown -R nextjs:nodejs /app/public/content

echo "=========================================================="
echo "Migration completed successfully!"
echo "New container: $NEW_CONTAINER"
echo "Backup saved to: $BACKUP_DIR"
echo ""
echo "To verify the deployment, visit your application URL."
echo "To roll back, stop the new containers and restart the old ones."
echo "==========================================================" 