#!/bin/bash

# Configuration
SCDR_BIN="/data/starfs1/bin/scdr-files"
DATA_TYPE="SPIRE_HYMS_L1B"
SSH_TARGET="jlocke@s4.ssec.wisc.edu"
DEST_DIR="/data/users/jlocke/SPIRE/DATA" # <-- Change this to your actual destination path on S4
rhwEST_DIR="/data/dmz/www/data/cdp_spire"

# 1. Calculate timestamps for the previous full day
STIME=$(date -d "12 hours ago" +"%Y-%m-%d %H:%M:%S")
ETIME=$(date +"%Y-%m-%d %H:%M:%S")

#STIME="2026-06-29"
#ETIME="2026-07-02"

# 2. Create a secured temporary file
TEMP_LIST=$(mktemp /tmp/spire_transfer.XXXXXX.txt)

echo "Starting retrieval for $DATA_TYPE ($STIME to $ETIME)..."

# 3. Run scdr-files and populate the temporary text file
# Note: This assumes scdr-files outputs the downloaded file paths to stdout. 
# If it doesn't, see the alternative mapping note below.
$SCDR_BIN -t "$DATA_TYPE" -stime "$STIME" -etime "$ETIME" > "$TEMP_LIST"

# 4. Loop through the text file and scp each file to S4
# Using a loop safely handles filenames that might contain spaces or special characters
while IFS= read -r filepath; do
    # Ensure the line isn't empty and the file actually exists locally
    if [ -n "$filepath" ] && [ -f "$filepath" ]; then
        echo "Transferring: $(basename "$filepath") to STAR Server"
        #scp "$filepath" "${SSH_TARGET}:${DEST_DIR}"
	rsync -v "$filepath" "${rhwEST_DIR}"
        chmod 444 /data/dmz/www/data/cdp_spire/$(basename "$filepath")

        if [ $? -ne 0 ]; then
            echo "Warning: Failed to transfer $filepath to STAR Server" >&2
        fi
    fi
done < "$TEMP_LIST"

while IFS= read -r filepath; do
    # Ensure the line isn't empty and the file actually exists locally
    if [ -n "$filepath" ] && [ -f "$filepath" ]; then
        echo "Transferring: $(basename "$filepath") to S4"
	scp "$filepath" "${SSH_TARGET}:${DEST_DIR}"
        #chmod 444 /data/users/jlocke/SPIRE/DATA/$(basename "$filepath")

        if [ $? -ne 0 ]; then
            echo "Warning: Failed to transfer $filepath to S4" >&2
        fi
    fi
done < "$TEMP_LIST"


echo "Transfer process complete."

# 6. Clean up the temporary file
rm -f "$TEMP_LIST"
echo "Temporary file cleaned up."
