#!/bin/bash
FILE="checksums.sha256"

echo "--- Checking Integrity ---"

# Run the checksum check
# We use --quiet so it only prints errors.
if sha256sum -c --quiet "$FILE"; then
    echo "SUCCESS: All files listed in $FILE are intact."
    integrity_status="pass"
else
    echo "ERROR: Some files are corrupted or missing!"
    integrity_status="fail"
fi

echo -e "\n--- Checking for Extra (Unlisted) Files ---"

# 1. Get list from checksum file AND strip leading "./"
expected=$(awk '{print substr($0, index($0, " ") + 2)}' "$FILE" | sed 's|^\./||' | sort)

# 2. Get list from disk AND strip leading "./"
actual=$(find . -type f ! -name "$FILE" | sed 's|^\./||' | sort)

# 3. Compare the two cleaned lists
extras=$(comm -13 <(echo "$expected") <(echo "$actual"))

if [ -z "$extras" ]; then
    echo "SUCCESS: No extra files found. Directory matches the list exactly."
else
    echo "WARNING: The following files are present but not in $FILE:"
    echo "$extras"
fi

# Final Summary Message
echo -e "\n--------------------------"
if [[ "$integrity_status" == "pass" && -z "$extras" ]]; then
    echo "OVERALL STATUS: PERFECT (Integrity OK, No extra files)"
else
    echo "OVERALL STATUS: ISSUES FOUND (Check details above)"
fi
