documentation/fix-link.sh
2025-02-28 10:14:34 +07:00

90 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Setup logging
LOG_FILE="_build/script_log_$(date +%Y%m%d_%H%M%S).log"
log_message() {
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
fi
if [[ ! -f "$INPUT_FILE" ]]; then
log_message "ERROR: Input file '$INPUT_FILE' does not exist."
exit 1
fi
log_message "INFO: Starting script with input file: $INPUT_FILE"
# Counter for processed lines
processed_count=0
error_count=0
while IFS=':' read -r file_path line_err status desc data; do
((processed_count++))
# Trim whitespace from variables
file_path="$(echo "$file_path" | xargs)"
line_err="$(echo "$line_err" | xargs)"
status="$(echo "$status" | xargs)"
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
log_message "WARNING: Empty file path encountered at line $processed_count"
((error_count++))
continue
fi
if [[ ! -f "$file_path" ]]; then
log_message "ERROR: File not found: $file_path at line $processed_count"
((error_count++))
continue
fi
if [[ ! -w "$file_path" ]]; then
log_message "ERROR: File not writable: $file_path at line $processed_count"
((error_count++))
continue
fi
case "$desc" in
"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
log_message "ERROR: Failed to modify $file_path"
((error_count++))
fi
;;
*)
log_message "DEBUG: No matching condition for description: $desc"
;;
esac
done < "$INPUT_FILE"
# Final summary
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
if [[ $error_count -gt 0 ]]; then
log_message "WARNING: Script completed with $error_count errors"
exit 1
else
log_message "INFO: Script completed successfully"
exit 0
fi