#!/bin/bash

# =================================================================
# --- CONFIGURATION (EDIT THESE VARIABLES) ---
# =================================================================

# 1. The root directory containing the satellite folders
SOURCE_ROOT_DIR="/data/www/data/cdp_pilot/TMS"

# 2. The directory where the date folders will be COPIED to
# *** REPLACE THIS WITH YOUR ACTUAL DESTINATION PATH ***
DESTINATION_DIR="/data/www/data/cdp_pilot/S4_Transfer/0505_0516"

# 3. The list of specific date folders you want to copy (separated by spaces)
# Dates must exactly match the folder name (e.g., 'YYYYMMDD')
# *** REPLACE THIS WITH YOUR ACTUAL LIST OF DATES ***
TARGET_DATES=("20260505" "20260506" "20260507" "20260508" "20260509" "20260510" "20260511" "20260512" "20260513" "20260514" "20260515" "20260516")

# The names of the satellite directories to search within (separated by spaces)
SATELLITE_DIRS=(
    "TS02_L1B-TB" "TS02_L1B-TBR" "TS02_L1C-TC" "TS02_L1C-TCR"
    "TS03_L1B-TB" "TS03_L1B-TBR" "TS03_L1C-TC" "TS03_L1C-TCR"
    "TS04_L1B-TB" "TS04_L1B-TBR" "TS04_L1C-TC" "TS04_L1C-TCR"
    "TS05_L1B-TB" "TS05_L1B-TBR" "TS05_L1C-TC" "TS05_L1C-TCR"
    "TS06_L1B-TB" "TS06_L1B-TBR" "TS06_L1C-TC" "TS06_L1C-TCR"
    "TS07_L1B-TB" "TS07_L1B-TBR" "TS07_L1C-TC" "TS07_L1C-TCR"
    "TS08_L1B-TB" "TS08_L1B-TBR" "TS08_L1C-TC" "TS08_L1C-TCR"
    "TS09_L1B-TB" "TS09_L1B-TBR" "TS09_L1C-TC" "TS09_L1C-TCR"
    "TS10_L1B-TB" "TS10_L1B-TBR" "TS10_L1C-TC" "TS10_L1C-TCR"
    "TS11_L1B-TB" "TS11_L1B-TBR" "TS11_L1C-TC" "TS11_L1C-TCR"
)

# =================================================================
# --- MAIN SCRIPT LOGIC ---
# =================================================================

echo "Starting file copy process..."
echo "Source Root: ${SOURCE_ROOT_DIR}"
echo "Destination: ${DESTINATION_DIR}"
echo "Target Dates: ${TARGET_DATES[*]}"
echo "------------------------------"

# 1. Ensure the destination root directory exists
mkdir -p "${DESTINATION_DIR}" || { echo "Error: Could not create destination directory. Exiting."; exit 1; }
echo "Ensured destination root directory exists: ${DESTINATION_DIR}"

# 2. Iterate through each satellite directory
for SATELLITE_NAME in "${SATELLITE_DIRS[@]}"; do
    SOURCE_SATELLITE_PATH="${SOURCE_ROOT_DIR}/${SATELLITE_NAME}"
    DEST_SATELLITE_PATH="${DESTINATION_DIR}/${SATELLITE_NAME}"

    if [ ! -d "${SOURCE_SATELLITE_PATH}" ]; then
        echo "Warning: Satellite directory not found: ${SOURCE_SATELLITE_PATH}"
        continue
    fi

    echo -e "\nSearching in: ${SATELLITE_NAME}..."

    # 3. Ensure the satellite folder exists in the destination
    mkdir -p "${DEST_SATELLITE_PATH}"

    # 4. Iterate through the list of target dates
    for DATE_NAME in "${TARGET_DATES[@]}"; do
        SOURCE_DATE_PATH="${SOURCE_SATELLITE_PATH}/${DATE_NAME}"

        # 5. Check if the date directory exists in the current satellite folder
        if [ -d "${SOURCE_DATE_PATH}" ]; then
            
            echo "  -> Found and copying date folder: ${DATE_NAME}"
            echo "     From: ${SOURCE_DATE_PATH}"
            echo "     To:   ${DEST_SATELLITE_PATH}/${DATE_NAME}"

            # 6. COPY the entire date directory recursively
            # The 'cp -r' command copies the folder and all its contents
            if cp -r "${SOURCE_DATE_PATH}" "${DEST_SATELLITE_PATH}/"; then
                echo "  -> Successfully COPIED ${DATE_NAME} from ${SATELLITE_NAME}."
            else
                echo "  -> ERROR copying ${SOURCE_DATE_PATH}"
            fi
        fi
    done
done

echo -e "\nFile copy complete! Original files remain in the source directory."
