diff --git a/fix-link.sh b/fix-link.sh index cb9d9bcf5..a59866b2a 100755 --- a/fix-link.sh +++ b/fix-link.sh @@ -1,51 +1,28 @@ #!/bin/bash -# Check if input file is provided -if [ $# -ne 1 ]; then - echo "Usage: $0 " +INPUT_FILE="$1" + +if [[ ! -f "$INPUT_FILE" ]]; then + echo "Error: Input file does not exist." exit 1 fi -input_file="$1" +while IFS=':' read -r file_path line_err status desc data; do + # 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 quotes from data -# Check if file exists -if [ ! -f "$input_file" ]; then - echo "Error: File '$input_file' not found" - exit 1 -fi - -# Process each line of the input file -while IFS= read -r line || [ -n "$line" ]; do - # Skip empty lines - [ -z "$line" ] && continue - - # Extract filepath (everything before first colon) - filepath=$(echo "$line" | cut -d: -f1) - - # Extract status (WARNING) - status="WARNING" - - # Split based on different patterns - if [[ "$line" =~ "WARNING: toctree contains reference to nonexisting document" ]]; then - description="toctree contains reference to nonexisting document" - data=$(echo "$line" | sed "s|^.*$description ||") - elif [[ "$line" =~ "WARNING: undefined label:" ]]; then - description="undefined label:" - data=$(echo "$line" | sed "s|^.*$description ||") - else - # For the first line with deprecated substitutions - description=$(echo "$line" | sed "s|^.*WARNING: ||" | sed "s| \[.*$||") - data=$(echo "$line" | grep -o "\[.*$" | tr -d '[]') + if [[ -f "$file_path" ]]; then + if [[ "$desc" == "undefined label" ]]; then + fixed_data="$(echo "$data" | tr '\\/_' '-')" + sed -i "s@$data@$fixed_data@g" "$file_path" + elif [[ "$desc" == *"toctree contains reference to nonexisting document"* ]]; then + fixed_data="$(echo "$data" | tr '\\/_' '-')" + sed -i "s@$data@$fixed_data@g" "$file_path" + fi fi - - # Replace /, \, and _ with - in data - modified_data=$(echo "$data" | tr '/\\_' '-') - - # Print the results - echo "filepath = $filepath" - echo "status = $status" - echo "description = $description" - echo "data = $modified_data" - echo "-------------------" - -done < "$input_file" \ No newline at end of file + +done < "$INPUT_FILE"