|
| 1 | +#!/bin/bash |
| 2 | +# Run this script from the root SIPNET directory with the command: |
| 3 | +# > tests/update_model_structures.sh |
| 4 | + |
| 5 | +# Name of the file containing #define compiler switches |
| 6 | +STRUCTURES_FILE="modelStructures.h" |
| 7 | + |
| 8 | +# Get the path to the current directory's STRUCTURES_FILE - that is, |
| 9 | +# the production version |
| 10 | +PROD_MODEL_STRUCTURES="./${STRUCTURES_FILE}" |
| 11 | + |
| 12 | +# Check if the current directory has the base model_structures.h |
| 13 | +if [[ ! -f $PROD_MODEL_STRUCTURES ]]; then |
| 14 | + echo "Error: ${STRUCTURES_FILE} not found in the current directory." |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +# Define the awk script to update #define values; this will be called on each test |
| 19 | +# version of STRUCTURES_FILE |
| 20 | +read -r -d '' AWK_SCRIPT << 'EOF' |
| 21 | +BEGIN { |
| 22 | + FS = "[ \t]+"; # Set field separator to handle spaces or tabs |
| 23 | + define_regex = "^#define"; |
| 24 | + skip_regex = "MODEL_STRUCTURES_H"; |
| 25 | +} |
| 26 | +
|
| 27 | +# Process the test structures file and store the #define values in an associative array |
| 28 | +FNR == NR { |
| 29 | + if ($1 ~ define_regex && $2 !~ skip_regex) { |
| 30 | + name = $2; # The second field is the name of the #define |
| 31 | + value = $3; # The third field is the value of the #define |
| 32 | + defines[name] = value; |
| 33 | + } |
| 34 | + next; |
| 35 | +} |
| 36 | +
|
| 37 | +# Update the new file with the old #define values |
| 38 | +{ |
| 39 | + if ($1 ~ define_regex && $2 !~ skip_regex) { |
| 40 | + name = $2; |
| 41 | + if (name in defines) { |
| 42 | + printf "#define %s %s\n", name, defines[name]; |
| 43 | + next; |
| 44 | + } |
| 45 | + } |
| 46 | + # Print the line as-is if no overwrite is needed |
| 47 | + print $0; |
| 48 | +} |
| 49 | +EOF |
| 50 | + |
| 51 | +# Find and process all target files in subdirectories |
| 52 | +find . -type f -name "$STRUCTURES_FILE" -mindepth 2 | while read -r file; do |
| 53 | + echo "Processing: $file" |
| 54 | + |
| 55 | + # Create a backup of the original file |
| 56 | + cp "$file" "${file}.bak" |
| 57 | + |
| 58 | + # Add a warning at the top, then update defines |
| 59 | + { |
| 60 | + echo "//" |
| 61 | + echo "// DO NOT ADD CUSTOM CODE TO THIS FILE (other than #define overrides for unit tests)" |
| 62 | + echo "// IT MAY BE OVERWRITTEN BY update_model_structures.sh" |
| 63 | + cat "$PROD_MODEL_STRUCTURES" |
| 64 | + } | awk "$AWK_SCRIPT" "$file" - > "${file}.tmp" |
| 65 | + |
| 66 | + # Overwrite the target file with the updated content |
| 67 | + mv "${file}.tmp" "$file" |
| 68 | + |
| 69 | + echo "Updated: $file" |
| 70 | +done |
0 commit comments