documentation/fix-link.sh
2025-02-28 10:22:52 +07:00

81 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# Check if input file is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <input_file>"
exit 1
fi
input_file="$1"
# Check if input file exists
if [ ! -f "$input_file" ]; then
echo "Error: File '$input_file' not found"
exit 1
fi
changes_made=0
# Process each line
while IFS=':' read -r file_path line_err status desc data; do
# Trim whitespace from all parts (both leading and trailing)
file_path=$(echo "$file_path" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
line_err=$(echo "$line_err" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
status=$(echo "$status" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
desc=$(echo "$desc" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
data=$(echo "$data" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "DEBUG: Processing line:"
echo "DEBUG: file_path=[$file_path]"
echo "DEBUG: line_err=[$line_err]"
echo "DEBUG: status=[$status]"
echo "DEBUG: desc=[$desc]"
echo "DEBUG: data=[$data]"
# Check if file exists and description matches conditions
if [ -f "$file_path" ]; then
echo "DEBUG: File exists: $file_path"
if [ "$desc" = "undefined label" ]; then
echo "DEBUG: Found 'undefined label' case"
# Replace \, /, or _ with - in data
new_data=$(echo "$data" | tr '\\/_' '-')
echo "DEBUG: Changing '$data' to '$new_data'"
# Replace the old data with new data in the file
if sed -i "s|$data|$new_data|g" "$file_path"; then
changes_made=$((changes_made + 1))
echo "Processed: $file_path - Changed '$data' to '$new_data'"
else
echo "WARNING: Failed to modify $file_path"
fi
elif [[ "$desc" =~ "toctree contains reference to nonexisting document" ]]; then
echo "DEBUG: Found 'toctree' case"
# Extract the document reference from data (assuming it's in single quotes)
doc_ref=$(echo "$data" | grep -o "'[^']*'" | tr -d "'")
if [ -n "$doc_ref" ]; then
# Replace \, /, or _ with - in doc_ref
new_data=$(echo "$doc_ref" | tr '\\/_' '-')
echo "DEBUG: Changing '$doc_ref' to '$new_data'"
# Replace the old data with new data in the file
if sed -i "s|$doc_ref|$new_data|g" "$file_path"; then
changes_made=$((changes_made + 1))
echo "Processed: $file_path - Changed '$doc_ref' to '$new_data'"
else
echo "WARNING: Failed to modify $file_path"
fi
else
echo "WARNING: Could not extract document reference from '$data' in $file_path"
fi
else
echo "DEBUG: Description '$desc' doesn't match any conditions"
fi
else
echo "WARNING: File not found: $file_path"
fi
echo "DEBUG: ---"
done < "$input_file"
echo "Processing completed!"
echo "Total changes made: $changes_made"