diff --git a/fix-link.sh b/fix-link.sh index a1a2cbdcf..a742e3e92 100755 --- a/fix-link.sh +++ b/fix-link.sh @@ -1,89 +1,81 @@ #!/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 " +# Check if input file is provided +if [ $# -ne 1 ]; then + echo "Usage: $0 " exit 1 fi -if [[ ! -f "$INPUT_FILE" ]]; then - log_message "ERROR: Input file '$INPUT_FILE' does not exist." +input_file="$1" + +# Check if input file exists +if [ ! -f "$input_file" ]; then + echo "Error: File '$input_file' not found" exit 1 fi -log_message "INFO: Starting script with input file: $INPUT_FILE" - -# Counter for processed lines -processed_count=0 -error_count=0 +changes_made=0 +# Process each line 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" + # 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:]]*$//') - if [[ -z "$file_path" ]]; then - log_message "WARNING: Empty file path encountered at line $processed_count" - ((error_count++)) - continue - fi + 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]" - 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'" + # 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 - log_message "ERROR: Failed to modify $file_path" - ((error_count++)) + echo "WARNING: Failed to modify $file_path" 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 -log_message "INFO: Script completed" -log_message "INFO: Total lines processed: $processed_count" -log_message "INFO: Total errors encountered: $error_count" +done < "$input_file" -# 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 +echo "Processing completed!" +echo "Total changes made: $changes_made" \ No newline at end of file