#!/bin/bash

# Define the directory to traverse
dist_dir="dist"

# Define the output file name (default is "sums.txt")
output_file="${1:-sums}.txt"

# Function to calculate checksums
calculate_checksum() {
    dir="$1"
    # Calculate checksum for the directory
    checksum=$(find "$dir" -type f -exec shasum -a 256 {} + | shasum -a 256 | awk '{print $1}')
    # Extract path relative to dist_dir and remove the first two directories
    relative_path="${dir#$dist_dir/}"
    relative_path="${relative_path#*/}"
    # Print checksum and relative path
    echo "$relative_path=$checksum"
}

# Traverse the directory tree to find directories at the third level
find "$dist_dir" -mindepth 3 -maxdepth 3 -type d | while IFS= read -r dir; do
    # Check if the directory path contains "common"
    if [[ "$dir" == *"common"* ]]; then
        echo "Skipping checksum for directory $dir since it contains 'common'."
        continue
    fi
    # Calculate checksum for the current directory and append to output file
    calculate_checksum "$dir" >> "$output_file"
done

echo "Checksums saved in $output_file."