dbschema-version-postprocess.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env sh
  2. # This script is executed by CMake after db_berkeley and db_text schema files are generated.
  3. # It appends the last line of each generated file (except 'version')
  4. # to the 'version' file and truncates each generated file to the n line.
  5. # Get the number of lines to move to version file from the first argument
  6. # Get the nubmer of lines to keep in the file from the second argument
  7. TAIL_NUMBER="$1"
  8. HEAD_NUMBER="$2"
  9. # echo " Tail number: $TAIL_NUMBER"
  10. # echo " Head number: $HEAD_NUMBER"
  11. # Loop through files, sorted alphabetically
  12. for FILE in $(ls * | sort); do
  13. # Check if it's a regular file and not the version file
  14. if [ -f "$FILE" ] && [ "$FILE" != "version" ]; then
  15. # echo " Processing: $FILE"
  16. # Check if file has at least 1 line before tail
  17. if [ -s "$FILE" ]; then # -s checks if file is not empty
  18. # Append the last line to the version file
  19. # Using "printf" to ensure a newline is added after the tail output
  20. tail -n "$TAIL_NUMBER" "$FILE" >> version
  21. if [ $? -ne 0 ]; then # Check tail command result
  22. echo "Warning: tail command failed for $FILE"
  23. fi
  24. # Get the first line and overwrite the original file
  25. head -n "$HEAD_NUMBER" "$FILE" > "$$FILE".tmp
  26. if [ $? -ne 0 ]; then
  27. echo "Warning: head command failed for $FILE"
  28. else
  29. mv "$$FILE".tmp "$FILE"
  30. if [ $? -ne 0 ]; then
  31. echo "Warning: mv command failed for $FILE"
  32. fi
  33. fi
  34. else
  35. echo "Warning: File $FILE is empty, skipping processing."
  36. fi
  37. fi
  38. done
  39. echo "Finished processing schema files in $PWD"
  40. exit 0 # Indicate success