#!/bin/bash
#
# Creates a clean deployment bundle of the project in a specified local directory.
#
# This script is designed to be run from the root of the project directory.
# It uses rsync with a "whitelist" of files and directories to ensure only
# artifacts required for deployment are included in the bundle.
#
# Usage:
#   ./tools/create-deployment-bundle.sh [DESTINATION_PATH]
#
# If DESTINATION_PATH is not provided, it defaults to /opt/yt-ops-deploys/yt-ops-services.

set -e # Exit immediately if a command exits with a non-zero status.
set -u # Treat unset variables as an error.

# --- Configuration ---
# The root directory of the project on the local machine.
SOURCE_DIR="."
# Default destination for the deployment bundle. Can be overridden by the first argument.
DEFAULT_DEST_DIR="/opt/yt-ops-deploys/yt-ops-services"
DEST_DIR="${1:-$DEFAULT_DEST_DIR}"

# --- rsync command ---
echo ">>> Creating deployment bundle from '$SOURCE_DIR' to '$DEST_DIR'..."

# Ensure the parent directory of the destination exists.
# This requires sudo if the user doesn't have permissions for the parent path.
if [ ! -d "$(dirname "$DEST_DIR")" ]; then
    echo "Parent directory of destination does not exist. Attempting to create with sudo..."
    sudo mkdir -p "$(dirname "$DEST_DIR")"
    sudo chown "$USER" "$(dirname "$DEST_DIR")"
fi

# Create a temporary file to list the files to be included.
# This is a "whitelist" approach, ensuring only necessary files are bundled.
# This list is generated by analyzing the Ansible playbooks to determine
# exactly which files and directories are required for deployment.
INCLUDE_FILE=$(mktemp)
EXCLUDE_FILE=$(mktemp)
trap 'rm -f -- "$INCLUDE_FILE" "$EXCLUDE_FILE"' EXIT

# Define files and directories to exclude from the bundle.
cat > "$EXCLUDE_FILE" <<EOF
airflow/dags/README.ru.md
airflow/dags/get_ip.py
ansible/README-yt.md
EOF

cat > "$INCLUDE_FILE" <<EOF
# Ansible deployment scripts, roles, and templates
ansible/

# Cluster definition file (input for inventory generator)
cluster.yml

# Scripts needed on the tower host
tools/

# Core application and service code
VERSION
get_info_json_client.py
proxy_manager_client.py
setup.py
yt_ops_services/
thrift_model/
thrift_exceptions_patch.py

# Airflow components
airflow/.dockerignore
airflow/Dockerfile
airflow/Dockerfile.caddy
airflow/airflow.cfg
airflow/camoufox/
airflow/config/
airflow/configs/
airflow/dags/
airflow/generate_envoy_config.py
airflow/inputfiles/
airflow/plugins/
airflow/update-yt-dlp.sh
EOF

# The rsync command:
# -a: archive mode (recursive, preserves permissions, etc.)
# -v: verbose
# -z: compress file data during the transfer
# --delete: delete extraneous files from the destination directory
# --files-from: read the list of files/dirs to transfer from our temp file.
# --filter: use .gitignore to exclude unnecessary files from within the whitelisted directories.
# --exclude-from: read a list of patterns to exclude from the transfer.
rsync -avz --delete \
    --filter=':- .gitignore' \
    --exclude-from="$EXCLUDE_FILE" \
    --files-from="$INCLUDE_FILE" \
    "$SOURCE_DIR/" \
    "$DEST_DIR/"

echo ">>> Deployment bundle created successfully at '$DEST_DIR'."
