#!/bin/bash
set -euo pipefail

# --- Environment Setup ---
ENV=""

# Parse command-line arguments
if [[ "$#" -gt 0 && "$1" == "--env" ]]; then
    if [[ -n "$2" && ("$2" == "prod" || "$2" == "test") ]]; then
        ENV="$2"
    else
        echo "Error: Invalid environment specified for deploy-master.sh. Use 'prod' or 'test'." >&2
        exit 1
    fi
else
    echo "Usage: $0 --env [prod|test]" >&2
    exit 1
fi

# --- Configuration ---
SSH_USER="alex_p"
if [[ "$ENV" == "prod" ]]; then
    MASTER_SERVER="af-green"
elif [[ "$ENV" == "test" ]]; then
    MASTER_SERVER="af-test"
fi

REMOTE_DEST_PATH="/srv/airflow_master/"
MASTER_HOST="${SSH_USER}@${MASTER_SERVER}"

# List of files and directories to sync from the project root.
# This script assumes it is run from the project root via deploy_all.sh
ROOT_FILES_TO_SYNC=(
    "Dockerfile"
    "get_info_json_client.py"
    "proxy_manager_client.py"
    "setup.py"
    "VERSION"
)
AIRFLOW_FILES_TO_SYNC=(
    "docker-compose-master.yaml"
    "init-airflow.sh"
    "nginx.conf"
)

DIRS_TO_SYNC=(
    "airflow/inputfiles/"
    "server_fix/"
    "yt_ops_services/"
)

RSYNC_OPTS="-avz --progress --delete --exclude='__pycache__/' --exclude='*.pyc' --exclude='*.pyo' --exclude='node_modules/'"

echo ">>> Deploying to MASTER for environment: $ENV"

# --- Deployment ---
echo ">>> Creating remote directory on MASTER: $MASTER_HOST"
ssh "$MASTER_HOST" "mkdir -p $REMOTE_DEST_PATH"

echo ">>> Syncing individual files to MASTER..."
for f in "${ROOT_FILES_TO_SYNC[@]}"; do
    rsync $RSYNC_OPTS "$f" "$MASTER_HOST:$REMOTE_DEST_PATH"
done
for f in "${AIRFLOW_FILES_TO_SYNC[@]}"; do
    rsync $RSYNC_OPTS "airflow/$f" "$MASTER_HOST:$REMOTE_DEST_PATH"
done

echo ">>> Syncing directories to MASTER..."
for d in "${DIRS_TO_SYNC[@]}"; do
    rsync $RSYNC_OPTS "$d" "$MASTER_HOST:$REMOTE_DEST_PATH"
done

echo ">>> Renaming master compose file on remote..."
ssh "$MASTER_HOST" "cd $REMOTE_DEST_PATH && ln -sf docker-compose-master.yaml docker-compose.yaml"

echo ">>> MASTER deployment sync complete."
exit 0
