#!/bin/bash #Following the the spec set here https://semver.org/ (2.0.0) #we have three parts to a version number: major=0 minor=0 patch_level=0 #And there is a fourth additional label for the build metadata. #These labels are for pre-release and build metadata. meta=0 file_contents="" #Looping code from #https://stackoverflow.com/questions/10929453/read-a-file-line-by-line-assigning-the-value-to-a-variable #Adding numbers in bash: #https://stackoverflow.com/questions/6348902/how-can-i-add-numbers-in-a-bash-script while IFS=' ' read -ra line; do if [[ $line != \#define* ]] then continue fi #Lob off the quotes surrounding the version number. #After lobbing off the quotes, replace all instances of '-' with a period. version_number=($(echo ${line[2]:1:-1} | tr "-" ".")) #The symbol name that #define was setting (i.e. KERNEL_VERSION_NUMBER). symbol=${line[1]} #Read in the version number and split it on all periods. IFS='.' read -a chunks <<<$version_number version_part_counter=0 for i in "${chunks[@]}"; do case "$version_part_counter" in "0") major=$i ;; "1") minor=$i ;; "2") patch_level=$i ;; "3") #Check to see if a second parameter has been passed. #It doesn't matter what its value is only that one is present. #If there isn't a second parameter set, then we increment the meta build #number, otherwise we decrement. if [ "$2" != "reset" ] then echo "Incrementing meta build number." meta=$(($i + 1)) else echo "Resetting meta build number." meta=0 fi ;; esac version_part_counter=$(($version_part_counter + 1)) done ver="$major.$minor.$patch_level-$meta" file_contents="$file_contents#ifndef ${symbol}\n#define ${symbol} \"$ver\"\n#endif\n" done < "$1" printf "$file_contents" > "$1"