blob: 9ed27a9f16328493e03fb1ab1039918e566ab851 [file] [log] [blame]
Ethan Sommer81995762020-05-22 20:47:34 -04001#!/bin/sh
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +03002
3# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4#
5# Project configuration variables
6#
7# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8VERSION_FILE="include/tinyalsa/version.h"
Alexandru N. Onea52a79572019-04-09 21:51:27 +03009CHANGELOG_FILE="debian/changelog"
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030010
11# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
12#
13# Scripts internal variables
14#
15# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
16LF="\n"
17PARAMS=""
Alexandru N. Oneacbc69372019-04-10 00:45:38 +030018DRYRUN=0
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030019
20# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21#
22# Helper functions
23#
24# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25die()
26{
27 echo "Error: $@" 1>&2
28 exit 1
29}
30
31print_usage()
32{
Alexandru N. Onea86525572019-04-10 00:30:22 +030033 echo
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030034 echo "Usage: $0 [OPTIONS] ACTION"
35 echo
36 echo "Available options:"
Alexandru N. Oneacbc69372019-04-10 00:45:38 +030037 echo " -s,--script Format output in \"script\" mode (no trailing newline)."
38 echo " -d,--dry-run Does not commit anything to any file, just prints."
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030039 echo
40 echo "Available actions:"
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030041 echo " print [minor|major|patch] Print the current version."
42 echo " release [minor|major|patch] Bump the specified version part"
Alexandru N. Onea86525572019-04-10 00:30:22 +030043 echo " check Check the changelog latest released"
44 echo " version against the version file."
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030045 echo
46 echo "Please run this script from the project root folder."
Alexandru N. Onea86525572019-04-10 00:30:22 +030047 echo
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030048}
49
Alexandru N. Onea86525572019-04-10 00:30:22 +030050check_files()
51{
52 [ -f ${VERSION_FILE} ] || die "No ${VERSION_FILE} found!";
53 [ -f ${CHANGELOG_FILE} ] || die "No ${CHANGELOG_FILE} found!"
54}
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030055
56# Gets a part of the version from the project version file (version.h).
Alexandru N. Onea86525572019-04-10 00:30:22 +030057# Takes one argument: the matching version identifier in the version file, e.g.
58# TINYALSA_VERSION_MAJOR
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030059get_version_part()
60{
Ethan Sommer81995762020-05-22 20:47:34 -040061 set -- "$1" "$(grep -m 1 "^#define\([ \t]*\)$1" ${VERSION_FILE} | sed 's/[^0-9]*//g')"
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030062
Ethan Sommer81995762020-05-22 20:47:34 -040063 if [ -z "$2" ]; then
Alexandru N. Onea86525572019-04-10 00:30:22 +030064 die "Could not get $1 from ${VERSION_FILE}"
65 fi
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030066
Ethan Sommer81995762020-05-22 20:47:34 -040067 echo "$2"
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030068}
69
70
71# Gets the complete version from the version file.
Alexandru N. Oneacbc69372019-04-10 00:45:38 +030072# Sets VERSION_MAJOR, VERSION_MINOR and VERSION_PATCH globals
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030073get_version()
74{
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030075 VERSION_MAJOR=$(get_version_part "TINYALSA_VERSION_MAJOR")
76 VERSION_MINOR=$(get_version_part "TINYALSA_VERSION_MINOR")
77 VERSION_PATCH=$(get_version_part "TINYALSA_VERSION_PATCH")
78}
79
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030080# Commits the new version part to the version file.
81# Takes two arguments: the version part identifier in the version file and the
Alexandru N. Onea86525572019-04-10 00:30:22 +030082# new version number. If no arguments, do nothing.
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030083commit_version_part()
84{
85 if [ -z $1 ] || [ -z $2 ]; then
86 return 0
87 fi
88
89 sed -i "s/\(^#define[ \t]*$1\)[ \t]*\([0-9]*\)/\1 $2/g" ${VERSION_FILE} \
Alexandru N. Onea86525572019-04-10 00:30:22 +030090 || die "Could not commit version for $1";
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030091
Alexandru N. Onea86525572019-04-10 00:30:22 +030092 [ $(get_version_part $1) = "$2" ] || die "Version check after commit failed for $1"
93
94 return 0;
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030095}
96
97# Commits the new version to the version file.
98# Takes three arguments, the new version numbers for major, minor and patch
99commit_version()
100{
101 commit_version_part "TINYALSA_VERSION_PATCH" $1
102 commit_version_part "TINYALSA_VERSION_MINOR" $2
103 commit_version_part "TINYALSA_VERSION_MAJOR" $3
104
105 return 0
106}
107
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300108# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
109#
110# Actions implementations / functions
111#
112# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
113print_version()
114{
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300115 if [ -z $1 ]; then
116 printf "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${LF}"
117 else
118 case "$1" in
119 major)
120 printf "${VERSION_MAJOR}${LF}"
121 ;;
122 minor)
123 printf "${VERSION_MINOR}${LF}"
124 ;;
125 patch)
126 printf "${VERSION_PATCH}${LF}"
127 ;;
128 *)
129 die "Unknown part \"$1\" (must be one of minor, major and patch)."
130 ;;
131 esac
132 fi
133
134 return 0
135}
136
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300137bump_version()
138{
Ethan Sommer81995762020-05-22 20:47:34 -0400139 case "${1:-patch}" in
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300140 major)
141 VERSION_MAJOR=$((VERSION_MAJOR+1))
142 VERSION_MINOR=0
143 VERSION_PATCH=0
144 ;;
145 minor)
146 VERSION_MINOR=$((VERSION_MINOR+1))
147 VERSION_PATCH=0
148 ;;
149 patch)
150 VERSION_PATCH=$((VERSION_PATCH+1))
151 ;;
152 *)
153 die "Unknown part \"$1\" (must be one of minor, major and patch)."
154 ;;
155 esac
156
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300157 if [ ${DRYRUN} -ne 1 ]; then
158 commit_version ${VERSION_PATCH} ${VERSION_MINOR} ${VERSION_MAJOR}
159 fi
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300160
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300161 print_version
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300162 return 0
163}
164
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300165check_version()
166{
Ethan Sommer81995762020-05-22 20:47:34 -0400167 # set $1 to log version, and $2 to ref version
168 set -- \
169 "$(grep -m 1 "^tinyalsa (" ${CHANGELOG_FILE}| sed "s/[^0-9.]*//g")" \
170 "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300171
Ethan Sommer81995762020-05-22 20:47:34 -0400172 if [ "$1" != "$2" ]; then
173 die "Changelog version ($1) does not match package version ($2)."
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300174 fi
175
Ethan Sommer81995762020-05-22 20:47:34 -0400176 printf "Changelog version ($1) OK!${LF}"
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300177 return 0
178}
179
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300180# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
181#
182# Command Line parsing
183#
184# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
185parse_command()
186{
187 if [ "$#" -eq "0" ]; then
188 print_usage
189 exit 1
190 fi
191
192 case "$1" in
193 print)
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300194 get_version
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300195 print_version "$2"
196 exit $?
197 ;;
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300198 release)
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300199 get_version
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300200 bump_version "$2"
201 exit $?
202 ;;
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300203 check)
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300204 get_version
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300205 check_version
206 exit $?
207 ;;
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300208 *)
209 die "Unsupported action \"$1\"."
210 ;;
211 esac
212}
213
214# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
215#
216# Main
217#
218# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
219
220set -e
221trap "set +e" 0
222
223# Checking parameters
224if [ "$#" -eq "0" ]; then
225 print_usage
226 exit 0
227fi
228
229while [ "$#" -ne "0" ]; do
230 case "$1" in
231 -s|--script)
232 unset LF
233 shift
234 ;;
Alexandru N. Oneacbc69372019-04-10 00:45:38 +0300235 -d|--dry-run)
236 DRYRUN=1
237 shift
238 ;;
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300239 --)
240 shift
241 break
242 ;;
243 -*|--*=)
244 die "Unsupported flag \"$1\"."
245 ;;
246 *)
247 PARAMS="$PARAMS ${1}"
248 shift
249 ;;
250 esac
251done
252
253# set positional arguments in their proper place
254set -- "${PARAMS}"
255
Alexandru N. Onea86525572019-04-10 00:30:22 +0300256check_files
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300257parse_command ${PARAMS}
258
259# The script should never reach this place.
260die "Internal error. Please report this."