Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions scripts/checksum-sort.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ get_timestamp() {
# Update date and version headers, and add checksum after Title
update_headers() {
local file="$1"
local temp_file="${file}.tmp.$$"
local temp_file="$file.tmp.$$"
local datetime version checksum

datetime=$(get_timestamp "datetime")
Expand All @@ -95,12 +95,12 @@ update_headers() {
else
echo "$line"
fi
done < "$file" > "$temp_file"
done <"$file" >"$temp_file"

# If Last modified or Version headers weren't found, add them after Title/Checksum
if [[ "$found_last_modified" == "false" ]] || [[ "$found_version" == "false" ]]; then
local temp_file2="${file}.tmp2.$$"
local temp_file2="$file.tmp2.$$"

while IFS= read -r line || [[ -n "$line" ]]; do
echo "$line"
# After Title line, add Checksum (placeholder), then missing headers
Expand All @@ -113,8 +113,8 @@ update_headers() {
echo "! Version: $version"
fi
fi
done < "$temp_file" > "$temp_file2"
done <"$temp_file" >"$temp_file2"

mv "$temp_file2" "$temp_file"
fi

Expand All @@ -128,7 +128,7 @@ add_checksum() {

# Calculate checksum after updating headers
checksum=$(calculate_checksum "$file")
temp_file="${file}.tmp.$$"
temp_file="$file.tmp.$$"

# Find Title line and insert Checksum right after it
local title_found=false
Expand All @@ -139,23 +139,23 @@ add_checksum() {
echo "! Checksum: $checksum"
title_found=true
fi
done < "$file" > "$temp_file"
done <"$file" >"$temp_file"

# If no Title line was found, prepend checksum
if [[ "$title_found" == "false" ]]; then
echo "! Checksum: $checksum" > "$temp_file"
cat "$file" >> "$temp_file"
echo "! Checksum: $checksum" >"$temp_file"
cat "$file" >>"$temp_file"
fi

mv "$temp_file" "$file"

log_info "📝 Checksum: $checksum"
}

# Sort the filter file (sort filter rules while preserving header)
sort_filter() {
local file="$1"
local temp_file="${file}.tmp.$$"
local temp_file="$file.tmp.$$"
local header_end=0
local line_num=0

Expand All @@ -166,18 +166,18 @@ sort_filter() {
header_end=$line_num
break
fi
done < "$file"
done <"$file"

if [[ $header_end -eq 0 ]]; then
# No filter rules, just return
return
fi

# Extract header (lines before first filter rule)
head -n $((header_end - 1)) "$file" > "$temp_file"
head -n $((header_end - 1)) "$file" >"$temp_file"

# Sort and append filter rules (skip empty lines at start of content)
tail -n +"$header_end" "$file" | sort -u >> "$temp_file"
tail -n +"$header_end" "$file" | sort -u >>"$temp_file"

mv "$temp_file" "$file"
}
Expand Down Expand Up @@ -256,4 +256,4 @@ main() {
}

# Run main function with all arguments
main "$@"
main "$@"