blob: 6378801d4fa6e283c4c129e749f463483b919a04 [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"
9
10# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
11#
12# Scripts internal variables
13#
14# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
15LF="\n"
16PARAMS=""
17
18# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
19#
20# Helper functions
21#
22# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23die()
24{
25 echo "Error: $@" 1>&2
26 exit 1
27}
28
29print_usage()
30{
31 echo "Usage: $0 [OPTIONS] ACTION"
32 echo
33 echo "Available options:"
34 echo " -s,--script Format output in \"script\" mode (no trailing newline)."
35 echo
36 echo "Available actions:"
37 echo " print [minor|major|patch] Print the current version."
38 echo
39 echo "Please run this script from the project root folder."
40}
41
42
43# Gets a part of the version from the project version file (version.h).
44# Takes one argument: the matching version identifier in the version file.
45get_version_part()
46{
47 local V=$(grep -m 1 "^#define\([ \t]*\)${1}" ${VERSION_FILE} | sed 's/[^0-9]*//g')
48
49 [ ! -z ${V} ] || die "Could not get ${1} from ${VERSION_FILE}"
50
51 echo ${V}
52}
53
54
55# Gets the complete version from the version file.
56get_version()
57{
58 [ -f ${VERSION_FILE} ] || die "No ${VERSION_FILE} found! Is this the project root?";
59
60 VERSION_MAJOR=$(get_version_part "TINYALSA_VERSION_MAJOR")
61 VERSION_MINOR=$(get_version_part "TINYALSA_VERSION_MINOR")
62 VERSION_PATCH=$(get_version_part "TINYALSA_VERSION_PATCH")
63}
64
65# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
66#
67# Actions implementations / functions
68#
69# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
70print_version()
71{
72 get_version
73
74 if [ -z $1 ]; then
75 printf "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${LF}"
76 else
77 case "$1" in
78 major)
79 printf "${VERSION_MAJOR}${LF}"
80 ;;
81 minor)
82 printf "${VERSION_MINOR}${LF}"
83 ;;
84 patch)
85 printf "${VERSION_PATCH}${LF}"
86 ;;
87 *)
88 die "Unknown part \"$1\" (must be one of minor, major and patch)."
89 ;;
90 esac
91 fi
92
93 return 0
94}
95
96# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
97#
98# Command Line parsing
99#
100# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
101parse_command()
102{
103 if [ "$#" -eq "0" ]; then
104 print_usage
105 exit 1
106 fi
107
108 case "$1" in
109 print)
110 print_version "$2"
111 exit $?
112 ;;
113 *)
114 die "Unsupported action \"$1\"."
115 ;;
116 esac
117}
118
119# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
120#
121# Main
122#
123# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
124
125set -e
126trap "set +e" 0
127
128# Checking parameters
129if [ "$#" -eq "0" ]; then
130 print_usage
131 exit 0
132fi
133
134while [ "$#" -ne "0" ]; do
135 case "$1" in
136 -s|--script)
137 unset LF
138 shift
139 ;;
140 --)
141 shift
142 break
143 ;;
144 -*|--*=)
145 die "Unsupported flag \"$1\"."
146 ;;
147 *)
148 PARAMS="$PARAMS ${1}"
149 shift
150 ;;
151 esac
152done
153
154# set positional arguments in their proper place
155set -- "${PARAMS}"
156
157parse_command ${PARAMS}
158
159# The script should never reach this place.
160die "Internal error. Please report this."