This commit is contained in:
hoangvv 2025-02-28 10:22:52 +07:00
parent dd90f6bd9f
commit 52ff35cc22

View File

@ -1,89 +1,81 @@
#!/bin/bash #!/bin/bash
# Setup logging # Check if input file is provided
LOG_FILE="_build/script_log_$(date +%Y%m%d_%H%M%S).log" if [ $# -ne 1 ]; then
log_message() { echo "Usage: $0 <input_file>"
local timestamp="$(date '+%Y-%m-%d %H:%M:%S')"
echo "[$timestamp] $1" | tee -a "$LOG_FILE"
}
# Check input file
INPUT_FILE="$1"
if [[ -z "$INPUT_FILE" ]]; then
log_message "ERROR: No input file provided. Usage: $0 <input_file>"
exit 1 exit 1
fi fi
if [[ ! -f "$INPUT_FILE" ]]; then input_file="$1"
log_message "ERROR: Input file '$INPUT_FILE' does not exist."
# Check if input file exists
if [ ! -f "$input_file" ]; then
echo "Error: File '$input_file' not found"
exit 1 exit 1
fi fi
log_message "INFO: Starting script with input file: $INPUT_FILE" changes_made=0
# Counter for processed lines
processed_count=0
error_count=0
# Process each line
while IFS=':' read -r file_path line_err status desc data; do while IFS=':' read -r file_path line_err status desc data; do
((processed_count++)) # Trim whitespace from all parts (both leading and trailing)
file_path=$(echo "$file_path" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
# Trim whitespace from variables line_err=$(echo "$line_err" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
file_path="$(echo "$file_path" | xargs)" status=$(echo "$status" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
line_err="$(echo "$line_err" | xargs)" desc=$(echo "$desc" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
status="$(echo "$status" | xargs)" data=$(echo "$data" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
desc="$(echo "$desc" | xargs)"
data="$(echo "$data" | xargs | tr -d "'")" # Remove single quotes from data
# Log the current line being processed
log_message "DEBUG: Processing line $processed_count - File: $file_path, Desc: $desc, Data: $data"
if [[ -z "$file_path" ]]; then echo "DEBUG: Processing line:"
log_message "WARNING: Empty file path encountered at line $processed_count" echo "DEBUG: file_path=[$file_path]"
((error_count++)) echo "DEBUG: line_err=[$line_err]"
continue echo "DEBUG: status=[$status]"
fi echo "DEBUG: desc=[$desc]"
echo "DEBUG: data=[$data]"
if [[ ! -f "$file_path" ]]; then # Check if file exists and description matches conditions
log_message "ERROR: File not found: $file_path at line $processed_count" if [ -f "$file_path" ]; then
((error_count++)) echo "DEBUG: File exists: $file_path"
continue
fi if [ "$desc" = "undefined label" ]; then
echo "DEBUG: Found 'undefined label' case"
if [[ ! -w "$file_path" ]]; then # Replace \, /, or _ with - in data
log_message "ERROR: File not writable: $file_path at line $processed_count" new_data=$(echo "$data" | tr '\\/_' '-')
((error_count++)) echo "DEBUG: Changing '$data' to '$new_data'"
continue # Replace the old data with new data in the file
fi if sed -i "s|$data|$new_data|g" "$file_path"; then
changes_made=$((changes_made + 1))
case "$desc" in echo "Processed: $file_path - Changed '$data' to '$new_data'"
"undefined label"|*"toctree contains reference to nonexisting document"*)
fixed_data="$(echo "$data" | tr '\\/_' '-')"
if sed -i "s@$data@$fixed_data@g" "$file_path" 2>>"$LOG_FILE"; then
log_message "INFO: Successfully fixed issue in $file_path: '$data' -> '$fixed_data'"
else else
log_message "ERROR: Failed to modify $file_path" echo "WARNING: Failed to modify $file_path"
((error_count++))
fi fi
;;
*)
log_message "DEBUG: No matching condition for description: $desc"
;;
esac
done < "$INPUT_FILE" 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: ---"
# Final summary done < "$input_file"
log_message "INFO: Script completed"
log_message "INFO: Total lines processed: $processed_count"
log_message "INFO: Total errors encountered: $error_count"
# Exit with appropriate status echo "Processing completed!"
if [[ $error_count -gt 0 ]]; then echo "Total changes made: $changes_made"
log_message "WARNING: Script completed with $error_count errors"
exit 1
else
log_message "INFO: Script completed successfully"
exit 0
fi