File: update-version-file.sh

package info (click to toggle)
libflame 5.2.0-5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 162,092 kB
  • sloc: ansic: 750,080; fortran: 404,344; makefile: 8,136; sh: 5,458; python: 937; pascal: 144; perl: 66
file content (142 lines) | stat: -rwxr-xr-x 3,262 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

#
# update-version-file.sh
#
# Field G. Van Zee
#


print_usage()
{
	#local script_name
	
	# Get the script name
	#script_name=${0##*/}
	
	# Echo usage info
	echo " "
	echo " "$script_name
	echo " "
	echo " Field G. Van Zee"
	echo " "
	echo " Checks whether the current libflame distribution is a git clone: if so,"
	echo " it queries git to update the 'version' file with the latest version"
	echo " string; otherwise, leaves the contents of 'version' unchanged."
	echo " "
	echo " Usage:"
	echo "   ${script_name} [options] versfile"
	echo " "
	echo " Arguments:"
	echo " "
	echo "   versfile    The file where the version string is stored. If versfile is"
	echo "               is not specified, then it defaults to 'version'."
	echo " "
	echo " Options:"
	echo " "
	echo "   -o SCRIPT   output script name"
	echo "                 Use SCRIPT when outputting messages instead of the script's"
	echo "                 actual name."
	echo " "
	
	# Exit with non-zero exit status
	exit 1
}


main()
{
	# -- BEGIN GLOBAL VARIABLE DECLARATIONS --

	# The name of the script, stripped of any preceeding path.
	script_name=${0##*/}

	# The name of the default version file.
	version_file_def='version'

	# The name of the specified version file.
	version_file=''

	# Strings used during version query.
	git_describe_str=''
	new_version_str=''

	# The script name to use instead of the $0 when outputting messages.
	output_name=''

	# The git directory.
	gitdir='.git'
	
	# -- END GLOBAL VARIABLE DECLARATIONS --


	# Process our command line options.
	while getopts ":ho:" opt; do
		case $opt in
			o  ) output_name=$OPTARG ;;
			h  ) print_usage ;;
			\? ) print_usage
		esac
	done
	shift $(($OPTIND - 1))


	# If an output script name was given, overwrite script_name with it.
	if [ -n "${output_name}" ]; then

		script_name="${output_name}"
	fi


	echo "${script_name}: checking whether we need to update the version file."

	
	
	# Check the number of arguments after command line option processing.
	if [ $# = "0" ]; then

		version_file=${version_file_def}
		echo "${script_name}: not sure which version file to update; defaulting to '${version_file}'."

	elif [ $# = "1" ]; then

		version_file=$1
		echo "${script_name}: checking version file '${version_file}'."

	else
		print_usage
	fi


	# Check if the .git dir exists; if it does not, we do nothing.
	if [ -d "${gitdir}" ]; then

		echo "${script_name}: found '${gitdir}' directory; assuming git clone."

		echo "${script_name}: executing git describe --tags."

		# Query git for the version string, which is simply the current tag,
		# followed by a number signifying how many commits have transpired
		# since the tag, followed by a 'g' and a shortened hash tab.
		git_describe_str=$(git describe --tags)

		echo "${script_name}: got back ${git_describe_str}."

		# Strip off the commit hash label.
		new_version_str=$(echo ${git_describe_str} | cut -d- -f-2)

		echo "${script_name}: truncating to ${new_version_str}."
		echo "${script_name}: updating version file '${version_file}'."

		# Write the new version string to the version file.
		echo "${new_version_str}" > ${version_file}

	fi


	# Exit peacefully.
	return 0
}


# The script's main entry point, passing all parameters given.
main "$@"