blob: 05fbf37afc31f4a77f78472060530d445cd6b60a [file] [log] [blame]
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +03001#!/bin/bash
2
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=""
18
19# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20#
21# Helper functions
22#
23# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24die()
25{
26 echo "Error: $@" 1>&2
27 exit 1
28}
29
30print_usage()
31{
32 echo "Usage: $0 [OPTIONS] ACTION"
33 echo
34 echo "Available options:"
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030035 echo " -s,--script Format output in \"script\" mode (no trailing newline)."
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030036 echo
37 echo "Available actions:"
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030038 echo " print [minor|major|patch] Print the current version."
39 echo " release [minor|major|patch] Bump the specified version part"
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030040 echo
41 echo "Please run this script from the project root folder."
42}
43
44
45# Gets a part of the version from the project version file (version.h).
46# Takes one argument: the matching version identifier in the version file.
47get_version_part()
48{
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030049 local V=$(grep -m 1 "^#define\([ \t]*\)$1" ${VERSION_FILE} | sed 's/[^0-9]*//g')
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030050
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030051 [ ! -z ${V} ] || die "Could not get $1 from ${VERSION_FILE}"
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030052
53 echo ${V}
54}
55
56
57# Gets the complete version from the version file.
58get_version()
59{
60 [ -f ${VERSION_FILE} ] || die "No ${VERSION_FILE} found! Is this the project root?";
61
62 VERSION_MAJOR=$(get_version_part "TINYALSA_VERSION_MAJOR")
63 VERSION_MINOR=$(get_version_part "TINYALSA_VERSION_MINOR")
64 VERSION_PATCH=$(get_version_part "TINYALSA_VERSION_PATCH")
65}
66
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030067# Commits the new version part to the version file.
68# Takes two arguments: the version part identifier in the version file and the
69# new version number.
70commit_version_part()
71{
72 if [ -z $1 ] || [ -z $2 ]; then
73 return 0
74 fi
75
76 sed -i "s/\(^#define[ \t]*$1\)[ \t]*\([0-9]*\)/\1 $2/g" ${VERSION_FILE} \
77 || die "Could not commit version";
78
79 return 0
80}
81
82# Commits the new version to the version file.
83# Takes three arguments, the new version numbers for major, minor and patch
84commit_version()
85{
86 commit_version_part "TINYALSA_VERSION_PATCH" $1
87 commit_version_part "TINYALSA_VERSION_MINOR" $2
88 commit_version_part "TINYALSA_VERSION_MAJOR" $3
89
90 return 0
91}
92
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030093# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
94#
95# Actions implementations / functions
96#
97# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
98print_version()
99{
100 get_version
101
102 if [ -z $1 ]; then
103 printf "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${LF}"
104 else
105 case "$1" in
106 major)
107 printf "${VERSION_MAJOR}${LF}"
108 ;;
109 minor)
110 printf "${VERSION_MINOR}${LF}"
111 ;;
112 patch)
113 printf "${VERSION_PATCH}${LF}"
114 ;;
115 *)
116 die "Unknown part \"$1\" (must be one of minor, major and patch)."
117 ;;
118 esac
119 fi
120
121 return 0
122}
123
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300124bump_version()
125{
126 get_version
127
128 local PART="patch"
129
130 if [ ! -z $1 ]; then
131 PART="$1"
132 fi
133
134 case "$PART" in
135 major)
136 VERSION_MAJOR=$((VERSION_MAJOR+1))
137 VERSION_MINOR=0
138 VERSION_PATCH=0
139 ;;
140 minor)
141 VERSION_MINOR=$((VERSION_MINOR+1))
142 VERSION_PATCH=0
143 ;;
144 patch)
145 VERSION_PATCH=$((VERSION_PATCH+1))
146 ;;
147 *)
148 die "Unknown part \"$1\" (must be one of minor, major and patch)."
149 ;;
150 esac
151
152 commit_version ${VERSION_PATCH} ${VERSION_MINOR} ${VERSION_MAJOR}
153 print_version
154
155 return 0
156}
157
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300158check_version()
159{
160 get_version
161
162 LOG_VERSION=$(grep -m 1 "^tinyalsa (" ${CHANGELOG_FILE}| sed "s/[^0-9.]*//g")
163 REF_VERSION="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
164
165 if [ "${LOG_VERSION}" != "${REF_VERSION}" ]; then
166 die "Changelog version (${LOG_VERSION}) does not match package version (${REF_VERSION})."
167 fi
168
169 printf "Changelog version (${LOG_VERSION}) OK!${LF}"
170 return 0
171}
172
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300173# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
174#
175# Command Line parsing
176#
177# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
178parse_command()
179{
180 if [ "$#" -eq "0" ]; then
181 print_usage
182 exit 1
183 fi
184
185 case "$1" in
186 print)
187 print_version "$2"
188 exit $?
189 ;;
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300190 release)
191 bump_version "$2"
192 exit $?
193 ;;
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300194 check)
195 check_version
196 exit $?
197 ;;
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300198 *)
199 die "Unsupported action \"$1\"."
200 ;;
201 esac
202}
203
204# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
205#
206# Main
207#
208# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
209
210set -e
211trap "set +e" 0
212
213# Checking parameters
214if [ "$#" -eq "0" ]; then
215 print_usage
216 exit 0
217fi
218
219while [ "$#" -ne "0" ]; do
220 case "$1" in
221 -s|--script)
222 unset LF
223 shift
224 ;;
225 --)
226 shift
227 break
228 ;;
229 -*|--*=)
230 die "Unsupported flag \"$1\"."
231 ;;
232 *)
233 PARAMS="$PARAMS ${1}"
234 shift
235 ;;
236 esac
237done
238
239# set positional arguments in their proper place
240set -- "${PARAMS}"
241
242parse_command ${PARAMS}
243
244# The script should never reach this place.
245die "Internal error. Please report this."