1# This script tries to figure out proper RELEASE number
2# and saves its best guess to CFRELEASE file passed as 1st and the only argument.
3
4# It checks $EXPLICIT_RELEASE, $RELEASE, and git tags.
5# If nothing helps, it defaults to 1.
6
7if [ "$#" -ne 1 ]
8then
9    echo "Usage: determine-release.sh path/to/CFRELEASE"
10    exit 1
11fi
12
13if [ "$EXPLICIT_RELEASE" ]; then
14	echo "EXPLICIT_RELEASE is set, using it"
15	echo "$EXPLICIT_RELEASE" > "$1"
16	exit 0
17fi
18
19if [ "$RELEASE" ]; then
20	echo "RELEASE is set, using it"
21	echo "$RELEASE" > "$1"
22	exit 0
23fi
24
25all_tags="$(git tag --points-at HEAD)"
26
27if [ -z "$all_tags" ]; then
28	echo "No tags found, using default release number 1"
29	echo 1 > "$1"
30	exit 0
31fi
32
33echo "All tags pointing to current commit:"
34echo "$all_tags"
35
36full_version="$(echo "$all_tags" | sed 's/-build[0-9]*$//' | sort -u | tail -n1)"
37
38echo "Latest version: $full_version"
39
40if ! expr "$full_version" : "3.*-\([0-9]*\)$" >/dev/null; then
41	echo "Could not parse it, using default release number 1"
42	echo 1 > "$1"
43	exit 0
44fi
45
46release="$(expr "$full_version" : "3.*-\([0-9]*\)$")"
47echo "Using release number from version: $release"
48echo "$release" > "$1"
49exit 0
50