85 lines
1.9 KiB
Bash
85 lines
1.9 KiB
Bash
#!/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
|
|
|
|
folder=$(dirname "$1")
|
|
parent_folder=$(basename "$folder")
|
|
#Capitalize the folder name so it matches the format in the C #define files.
|
|
parent_folder=${parent_folder^^}
|
|
|
|
define_count=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.
|
|
#version_number="${line[2]:1:-1}"
|
|
version_number=($(echo ${line[2]:1:-1} | tr "-" "."))
|
|
#The symbol name that #define was setting (i.e. KERNEL_VERSION_NUMBER).
|
|
symbol=${line[1]}
|
|
|
|
for i in "${my_array[@]}"
|
|
do
|
|
echo $i
|
|
done
|
|
|
|
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" == "" ]
|
|
then
|
|
meta=$(($i + 1))
|
|
else
|
|
if [ $meta > 0 ]
|
|
then
|
|
meta=$(($i - 1))
|
|
fi
|
|
fi
|
|
;;
|
|
esac
|
|
|
|
version_part_counter=$(($version_part_counter + 1))
|
|
done
|
|
|
|
ver="$major.$minor.$patch_level-$meta"
|
|
tmp="#ifndef ${symbol}\n#define ${symbol} \"$ver\"\n#endif\n"
|
|
|
|
file_contents="$file_contents$tmp"
|
|
|
|
define_count=$(($define_count + 1))
|
|
done < "$1"
|
|
|
|
printf "$file_contents"
|
|
echo "$1"
|
|
printf "$file_contents" > "$1"
|