blob: e9cc595a436b3dc9515e22b56458ffebbd6c8177 [file] [log] [blame]
Alexandru N. Onea86525572019-04-10 00:30:22 +03001#!/usr/bin/env bash
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=""
18
19# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20#
21# Helper functions
22#
23# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24die()
25{
26 echo "Error: $@" 1>&2
27 exit 1
28}
29
30print_usage()
31{
Alexandru N. Onea86525572019-04-10 00:30:22 +030032 echo
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030033 echo "Usage: $0 [OPTIONS] ACTION"
34 echo
35 echo "Available options:"
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030036 echo " -s,--script Format output in \"script\" mode (no trailing newline)."
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030037 echo
38 echo "Available actions:"
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030039 echo " print [minor|major|patch] Print the current version."
40 echo " release [minor|major|patch] Bump the specified version part"
Alexandru N. Onea86525572019-04-10 00:30:22 +030041 echo " check Check the changelog latest released"
42 echo " version against the version file."
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030043 echo
44 echo "Please run this script from the project root folder."
Alexandru N. Onea86525572019-04-10 00:30:22 +030045 echo
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030046}
47
Alexandru N. Onea86525572019-04-10 00:30:22 +030048check_files()
49{
50 [ -f ${VERSION_FILE} ] || die "No ${VERSION_FILE} found!";
51 [ -f ${CHANGELOG_FILE} ] || die "No ${CHANGELOG_FILE} found!"
52}
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030053
54# Gets a part of the version from the project version file (version.h).
Alexandru N. Onea86525572019-04-10 00:30:22 +030055# Takes one argument: the matching version identifier in the version file, e.g.
56# TINYALSA_VERSION_MAJOR
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030057get_version_part()
58{
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030059 local V=$(grep -m 1 "^#define\([ \t]*\)$1" ${VERSION_FILE} | sed 's/[^0-9]*//g')
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030060
Alexandru N. Onea86525572019-04-10 00:30:22 +030061 if [ -z ${V} ]; then
62 die "Could not get $1 from ${VERSION_FILE}"
63 fi
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030064
65 echo ${V}
66}
67
68
69# Gets the complete version from the version file.
70get_version()
71{
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +030072 VERSION_MAJOR=$(get_version_part "TINYALSA_VERSION_MAJOR")
73 VERSION_MINOR=$(get_version_part "TINYALSA_VERSION_MINOR")
74 VERSION_PATCH=$(get_version_part "TINYALSA_VERSION_PATCH")
75}
76
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030077# Commits the new version part to the version file.
78# Takes two arguments: the version part identifier in the version file and the
Alexandru N. Onea86525572019-04-10 00:30:22 +030079# new version number. If no arguments, do nothing.
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030080commit_version_part()
81{
82 if [ -z $1 ] || [ -z $2 ]; then
83 return 0
84 fi
85
86 sed -i "s/\(^#define[ \t]*$1\)[ \t]*\([0-9]*\)/\1 $2/g" ${VERSION_FILE} \
Alexandru N. Onea86525572019-04-10 00:30:22 +030087 || die "Could not commit version for $1";
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030088
Alexandru N. Onea86525572019-04-10 00:30:22 +030089 [ $(get_version_part $1) = "$2" ] || die "Version check after commit failed for $1"
90
91 return 0;
Alexandru N. Onea891c04d2019-04-09 21:16:34 +030092}
93
94# Commits the new version to the version file.
95# Takes three arguments, the new version numbers for major, minor and patch
96commit_version()
97{
98 commit_version_part "TINYALSA_VERSION_PATCH" $1
99 commit_version_part "TINYALSA_VERSION_MINOR" $2
100 commit_version_part "TINYALSA_VERSION_MAJOR" $3
101
102 return 0
103}
104
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300105# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
106#
107# Actions implementations / functions
108#
109# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
110print_version()
111{
112 get_version
113
114 if [ -z $1 ]; then
115 printf "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${LF}"
116 else
117 case "$1" in
118 major)
119 printf "${VERSION_MAJOR}${LF}"
120 ;;
121 minor)
122 printf "${VERSION_MINOR}${LF}"
123 ;;
124 patch)
125 printf "${VERSION_PATCH}${LF}"
126 ;;
127 *)
128 die "Unknown part \"$1\" (must be one of minor, major and patch)."
129 ;;
130 esac
131 fi
132
133 return 0
134}
135
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300136bump_version()
137{
138 get_version
139
140 local PART="patch"
141
142 if [ ! -z $1 ]; then
143 PART="$1"
144 fi
145
146 case "$PART" in
147 major)
148 VERSION_MAJOR=$((VERSION_MAJOR+1))
149 VERSION_MINOR=0
150 VERSION_PATCH=0
151 ;;
152 minor)
153 VERSION_MINOR=$((VERSION_MINOR+1))
154 VERSION_PATCH=0
155 ;;
156 patch)
157 VERSION_PATCH=$((VERSION_PATCH+1))
158 ;;
159 *)
160 die "Unknown part \"$1\" (must be one of minor, major and patch)."
161 ;;
162 esac
163
164 commit_version ${VERSION_PATCH} ${VERSION_MINOR} ${VERSION_MAJOR}
165 print_version
166
167 return 0
168}
169
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300170check_version()
171{
172 get_version
173
Alexandru N. Onea86525572019-04-10 00:30:22 +0300174 local LOG_VERSION=$(grep -m 1 "^tinyalsa (" ${CHANGELOG_FILE}| sed "s/[^0-9.]*//g")
175 local REF_VERSION="${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}"
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300176
177 if [ "${LOG_VERSION}" != "${REF_VERSION}" ]; then
178 die "Changelog version (${LOG_VERSION}) does not match package version (${REF_VERSION})."
179 fi
180
181 printf "Changelog version (${LOG_VERSION}) OK!${LF}"
182 return 0
183}
184
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300185# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
186#
187# Command Line parsing
188#
189# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
190parse_command()
191{
192 if [ "$#" -eq "0" ]; then
193 print_usage
194 exit 1
195 fi
196
197 case "$1" in
198 print)
199 print_version "$2"
200 exit $?
201 ;;
Alexandru N. Onea891c04d2019-04-09 21:16:34 +0300202 release)
203 bump_version "$2"
204 exit $?
205 ;;
Alexandru N. Onea52a79572019-04-09 21:51:27 +0300206 check)
207 check_version
208 exit $?
209 ;;
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300210 *)
211 die "Unsupported action \"$1\"."
212 ;;
213 esac
214}
215
216# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
217#
218# Main
219#
220# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
221
222set -e
223trap "set +e" 0
224
225# Checking parameters
226if [ "$#" -eq "0" ]; then
227 print_usage
228 exit 0
229fi
230
231while [ "$#" -ne "0" ]; do
232 case "$1" in
233 -s|--script)
234 unset LF
235 shift
236 ;;
237 --)
238 shift
239 break
240 ;;
241 -*|--*=)
242 die "Unsupported flag \"$1\"."
243 ;;
244 *)
245 PARAMS="$PARAMS ${1}"
246 shift
247 ;;
248 esac
249done
250
251# set positional arguments in their proper place
252set -- "${PARAMS}"
253
Alexandru N. Onea86525572019-04-10 00:30:22 +0300254check_files
Alexandru N. Onea3a6abfa2019-04-09 01:23:10 +0300255parse_command ${PARAMS}
256
257# The script should never reach this place.
258die "Internal error. Please report this."