1#!/bin/bash -e
2
3prompt_for_number() {
4	local prompt_text=$1
5	local default_value=$2
6	local tmp=""
7	while true; do
8		read -p "$prompt_text [$default_value]: " tmp
9		if [ "$tmp" = "" ]; then
10			echo "$default_value"; return
11		elif echo "$tmp" | grep -q -E '^[0-9]+$'; then
12			echo "$tmp"; return
13		fi
14	done
15}
16
17# On a release the following actions are performed
18# * DEVELOPMENT_BUILD is set to false
19# * android versionCode is bumped
20# * appdata release version and date are updated
21# * Commit the changes
22# * Tag with current version
23perform_release() {
24	RELEASE_DATE=$(date +%Y-%m-%d)
25
26	sed -i -re "s/^set\(DEVELOPMENT_BUILD TRUE\)$/set(DEVELOPMENT_BUILD FALSE)/" CMakeLists.txt
27
28	sed -i 's/project.ext.set("versionExtra", "-dev")/project.ext.set("versionExtra", "")/' build/android/build.gradle
29	sed -i -re "s/\"versionCode\", [0-9]+/\"versionCode\", $NEW_ANDROID_VERSION_CODE/" build/android/build.gradle
30
31	sed -i '/\<release/s/\(version\)="[^"]*"/\1="'"$RELEASE_VERSION"'"/' misc/net.minetest.minetest.appdata.xml
32	sed -i 's/\(<release date\)="[^"]*"/\1="'"$RELEASE_DATE"'"/' misc/net.minetest.minetest.appdata.xml
33
34	git add -f CMakeLists.txt build/android/build.gradle misc/net.minetest.minetest.appdata.xml
35
36	git commit -m "Bump version to $RELEASE_VERSION"
37
38	echo "Tagging $RELEASE_VERSION"
39
40	git tag -a "$RELEASE_VERSION" -m "$RELEASE_VERSION"
41}
42
43# After release
44# * Set DEVELOPMENT_BUILD to true
45# * Bump version in CMakeLists and docs
46# * Commit the changes
47back_to_devel() {
48	echo 'Creating "return back to development" commit'
49
50	# Update CMakeList.txt versions
51	sed -i -re 's/^set\(DEVELOPMENT_BUILD FALSE\)$/set(DEVELOPMENT_BUILD TRUE)/' CMakeLists.txt
52	sed -i -re "s/^set\(VERSION_MAJOR [0-9]+\)$/set(VERSION_MAJOR $NEXT_VERSION_MAJOR)/" CMakeLists.txt
53	sed -i -re "s/^set\(VERSION_MINOR [0-9]+\)$/set(VERSION_MINOR $NEXT_VERSION_MINOR)/" CMakeLists.txt
54	sed -i -re "s/^set\(VERSION_PATCH [0-9]+\)$/set(VERSION_PATCH $NEXT_VERSION_PATCH)/" CMakeLists.txt
55
56	# Update Android versions
57	sed -i 's/set("versionExtra", "")/set("versionExtra", "-dev")/' build/android/build.gradle
58	sed -i -re "s/set\(\"versionMajor\", [0-9]+\)/set(\"versionMajor\", $NEXT_VERSION_MAJOR)/" build/android/build.gradle
59	sed -i -re "s/set\(\"versionMinor\", [0-9]+\)/set(\"versionMinor\", $NEXT_VERSION_MINOR)/" build/android/build.gradle
60	sed -i -re "s/set\(\"versionPatch\", [0-9]+\)/set(\"versionPatch\", $NEXT_VERSION_PATCH)/" build/android/build.gradle
61
62	# Update doc versions
63	sed -i -re "1s/[0-9]+\.[0-9]+\.[0-9]+/$NEXT_VERSION/g" doc/menu_lua_api.txt
64	sed -i -re "1s/[0-9]+\.[0-9]+\.[0-9]+/$NEXT_VERSION/g" doc/client_lua_api.txt
65
66	# Commit
67	git add -f CMakeLists.txt build/android/build.gradle doc/menu_lua_api.txt doc/client_lua_api.txt
68	git commit -m "Continue with $NEXT_VERSION-dev"
69}
70##################################
71# Switch to top minetest directory
72##################################
73
74cd ${0%/*}/..
75
76
77#######################
78# Determine old version
79#######################
80
81# Make sure all the files we need exist
82grep -q -E '^set\(VERSION_MAJOR [0-9]+\)$' CMakeLists.txt
83grep -q -E '^set\(VERSION_MINOR [0-9]+\)$' CMakeLists.txt
84grep -q -E '^set\(VERSION_PATCH [0-9]+\)$' CMakeLists.txt
85grep -q -E '\("versionCode", [0-9]+\)' build/android/build.gradle
86
87VERSION_MAJOR=$(grep -E '^set\(VERSION_MAJOR [0-9]+\)$' CMakeLists.txt | tr -dC 0-9)
88VERSION_MINOR=$(grep -E '^set\(VERSION_MINOR [0-9]+\)$' CMakeLists.txt | tr -dC 0-9)
89VERSION_PATCH=$(grep -E '^set\(VERSION_PATCH [0-9]+\)$' CMakeLists.txt | tr -dC 0-9)
90ANDROID_VERSION_CODE=$(grep -E '"versionCode", [0-9]+' build/android/build.gradle | tr -dC 0-9)
91
92RELEASE_VERSION="$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH"
93
94echo "Current Minetest version: $RELEASE_VERSION"
95echo "Current Android version code: $ANDROID_VERSION_CODE"
96
97# +1 for ARM and +1 for ARM64 APKs
98NEW_ANDROID_VERSION_CODE=$(expr $ANDROID_VERSION_CODE + 2)
99NEW_ANDROID_VERSION_CODE=$(prompt_for_number "Set android version code" $NEW_ANDROID_VERSION_CODE)
100
101echo
102echo "New android version code: $NEW_ANDROID_VERSION_CODE"
103
104########################
105# Perform release
106########################
107
108perform_release
109
110########################
111# Prompt for next version
112########################
113
114NEXT_VERSION_MAJOR=$VERSION_MAJOR
115NEXT_VERSION_MINOR=$VERSION_MINOR
116NEXT_VERSION_PATCH=$(expr $VERSION_PATCH + 1)
117
118NEXT_VERSION_MAJOR=$(prompt_for_number "Set next major" $NEXT_VERSION_MAJOR)
119
120if [ "$NEXT_VERSION_MAJOR" != "$VERSION_MAJOR" ]; then
121	NEXT_VERSION_MINOR=0
122	NEXT_VERSION_PATCH=0
123fi
124
125NEXT_VERSION_MINOR=$(prompt_for_number "Set next minor" $NEXT_VERSION_MINOR)
126
127if [ "$NEXT_VERSION_MINOR" != "$VERSION_MINOR" ]; then
128	NEXT_VERSION_PATCH=0
129fi
130
131NEXT_VERSION_PATCH=$(prompt_for_number "Set next patch" $NEXT_VERSION_PATCH)
132
133NEXT_VERSION="$NEXT_VERSION_MAJOR.$NEXT_VERSION_MINOR.$NEXT_VERSION_PATCH"
134
135echo
136echo "New version: $NEXT_VERSION"
137
138########################
139# Return back to devel
140########################
141
142back_to_devel
143