#!/bin/bash

# --- Configuration ---
# Source directory (on the internal macOS SSD).
SOURCE_DIR=&#34;$HOME/fio-test-source&#34;
# Destination directory (on your SCSI Target mount point).
DEST_DIR=&#34;/Volumes/RAID5-0/copy-test&#34;
# Sequence of file sizes to test.
TEST_SIZES=(&#34;16M&#34; &#34;32M&#34; &#34;64M&#34; &#34;128M&#34; &#34;256M&#34; &#34;512M&#34; &#34;1G&#34; &#34;2G&#34; &#34;4G&#34; &#34;5G&#34; &#34;8G&#34; &#34;16G&#34; &#34;20G&#34;)

# --- Script Start ---
echo &#34;Starting system-level file copy integrity test...&#34;
echo &#34;=================================&#34;

# Create source and destination directories.
mkdir -p &#34;$SOURCE_DIR&#34;
mkdir -p &#34;$DEST_DIR&#34;

# Check if directories were created successfully.
if [ ! -d &#34;$SOURCE_DIR&#34; ] || [ ! -d &#34;$DEST_DIR&#34; ]; then
    echo &#34;Error: Could not create test directories.&#34;
    exit 1
fi

# Iterate through all defined sizes.
for size in &#34;${TEST_SIZES[@]}&#34;; do
    echo -e &#34;\n[*] Testing file size: $size&#34;
    SOURCE_FILE=&#34;$SOURCE_DIR/testfile_${size}.dat&#34;
    DEST_FILE=&#34;$DEST_DIR/testfile_${size}.dat&#34;

    # 1. Create the test file on the source SSD using random data.
    echo &#34;  -&gt; Step 1: Creating $size test file on source SSD...&#34;
    # Convert M/G units to the &#39;count&#39; parameter for dd.
    count=$(echo &#34;$size&#34; | sed &#39;s/G/ \* 1024/&#39; | sed &#39;s/M//&#39; | bc)
    dd if=/dev/urandom of=&#34;$SOURCE_FILE&#34; bs=1m count=$count status=none
    if [ $? -ne 0 ]; then
        echo &#34;  [!!!] Error: Failed to create source file.&#34;
        continue
    fi

    # 2. Calculate the checksum of the source file.
    echo &#34;  -&gt; Step 2: Calculating SHA256 checksum of source file...&#34;
    SOURCE_HASH=$(shasum -a 256 &#34;$SOURCE_FILE&#34; | awk &#39;{print $1}&#39;)
    echo &#34;     Source Hash: $SOURCE_HASH&#34;

    # 3. Copy the file to the SCSI Target.
    echo &#34;  -&gt; Step 3: Copying file to SCSI Target...&#34;
    cp &#34;$SOURCE_FILE&#34; &#34;$DEST_FILE&#34;
    if [ $? -ne 0 ]; then
        echo &#34;  [!!!] Error: File copy command failed!&#34;
        rm -f &#34;$SOURCE_FILE&#34; &#34;$DEST_FILE&#34;
        continue
    fi

    # 4. Force sync of file system buffers to disk (very important!).
    # This ensures we are verifying the data that was actually written to the physical disk.
    sync
    sleep 1 # Give sync a moment

    # 5. Calculate the checksum of the destination file.
    echo &#34;  -&gt; Step 4: Calculating SHA256 checksum of destination file...&#34;
    DEST_HASH=$(shasum -a 256 &#34;$DEST_FILE&#34; | awk &#39;{print $1}&#39;)
    echo &#34;     Destination Hash: $DEST_HASH&#34;

    # 6. Compare the checksums.
    echo &#34;  -&gt; Step 5: Verifying checksums...&#34;
    if [ &#34;$SOURCE_HASH&#34; == &#34;$DEST_HASH&#34; ]; then
        echo &#34;  [OK] SUCCESS: Data integrity verified for $size file.&#34;
    else
        echo &#34;  [!!!] FAILURE: Data corruption detected for $size file!&#34;
        echo &#34;--------------------------------------------------&#34;
        echo &#34;Source Hash:      $SOURCE_HASH&#34;
        echo &#34;Destination Hash: $DEST_HASH&#34;
        echo &#34;--------------------------------------------------&#34;
        # Clean up and terminate the script.
        rm -f &#34;$SOURCE_FILE&#34; &#34;$DEST_FILE&#34;
        exit 1
    fi

    # 7. Clean up the files from the current successful test run.
    rm -f &#34;$SOURCE_FILE&#34; &#34;$DEST_FILE&#34;
done

echo &#34;=================================&#34;
echo &#34;All system-level copy tests completed successfully!&#34;
