Revision ec50c96e
setup.sh | ||
---|---|---|
23 | 23 |
|
24 | 24 |
#!/bin/bash |
25 | 25 |
|
26 |
################################################################################ |
|
27 |
# GENERIC FUNCTIONS # |
|
28 |
################################################################################ |
|
29 |
|
|
30 |
### print an error message ##################################################### |
|
31 |
# Prints a error <message> to standard output. |
|
32 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
33 |
# |
|
34 |
# usage: printError <message> |
|
35 |
# arguments: <message> |
|
36 |
# Message string to print. |
|
37 |
# return: n/a |
|
38 |
# |
|
39 |
function printError { |
|
40 |
local string="ERROR: $1" |
|
41 |
# if a log file is specified |
|
42 |
if [ -n "$LOG_FILE" ]; then |
|
43 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
44 |
fi |
|
45 |
printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2 |
|
46 |
} |
|
47 |
|
|
48 |
### print a warning message #################################################### |
|
49 |
# Prints a warning <message> to standard output. |
|
50 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
51 |
# |
|
52 |
# usage: printMessage <message> |
|
53 |
# arguments: <message> |
|
54 |
# Message string to print. |
|
55 |
# return: n/a |
|
56 |
# |
|
57 |
function printWarning { |
|
58 |
local string="WARNING: $1" |
|
59 |
# if a log file is specified |
|
60 |
if [ -n "$LOG_FILE" ]; then |
|
61 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
62 |
fi |
|
63 |
printf "$(tput setaf 3)>>> $string$(tput sgr 0)" |
|
64 |
} |
|
65 |
|
|
66 |
### print an information message ############################################### |
|
67 |
# Prints an information <message> to standard output. |
|
68 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
69 |
# |
|
70 |
# usage: printInfo <message> |
|
71 |
# arguments: <message> |
|
72 |
# Message string to print. |
|
73 |
# return: n/a |
|
74 |
# |
|
75 |
function printInfo { |
|
76 |
local string="INFO: $1" |
|
77 |
# if a log file is specified |
|
78 |
if [ -n "$LOG_FILE" ]; then |
|
79 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
80 |
fi |
|
81 |
printf "$(tput setaf 2)>>> $string$(tput sgr 0)" |
|
82 |
} |
|
83 |
|
|
84 |
### print a message to file #################################################### |
|
85 |
# Appends a <message> to a log file, specified by the variable 'LOG_FILE'. |
|
86 |
# |
|
87 |
# usage printLog <message> |
|
88 |
# arguments: <message> |
|
89 |
# Message string to print. |
|
90 |
# return: n/a |
|
91 |
# |
|
92 |
function printLog { |
|
93 |
local string="LOG: $1" |
|
94 |
# if a log file is specified |
|
95 |
if [ -n "$LOG_FILE" ]; then |
|
96 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
97 |
fi |
|
98 |
} |
|
99 |
|
|
100 |
### exit the script normally ################################################### |
|
101 |
# Prints a delimiter and exits the script normally (returns 0). |
|
102 |
# |
|
103 |
# usage: quitScript |
|
104 |
# arguments: n/a |
|
105 |
# return: 0 |
|
106 |
# No error or warning occurred. |
|
107 |
# |
|
108 |
function quitScript { |
|
109 |
printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n" |
|
110 |
printf "\n" |
|
111 |
printf "######################################################################\n" |
|
112 |
exit 0 |
|
113 |
} |
|
114 |
|
|
115 |
### read a user input ########################################################## |
|
116 |
# Reads a single character user input from a set up <options> and stores it in |
|
117 |
# a given <return> variable. |
|
118 |
# |
|
119 |
# usage: readUserInput <options> <return> |
|
120 |
# arguments: <options> |
|
121 |
# String definiing the set of valid characters. |
|
122 |
# If the string is empty, the user can input any character. |
|
123 |
# <return> |
|
124 |
# Variable to store the selected character to. |
|
125 |
# return: n/a |
|
126 |
# |
|
127 |
function readUserInput { |
|
128 |
local input="" |
|
129 |
# read user input |
|
130 |
while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do |
|
131 |
read -p "your selection: " -n 1 -e input |
|
132 |
if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then |
|
133 |
printWarning "[$input] is no valid action\n" |
|
134 |
fi |
|
135 |
done |
|
136 |
printLog "[$input] has been selected\n" |
|
137 |
eval $2="$input" |
|
138 |
} |
|
139 |
|
|
140 |
### check whether argument is an option ######################################## |
|
141 |
# Checks a <string> whether it is an option. |
|
142 |
# Options are defined to either start with '--' followed by any string, or |
|
143 |
# to start with a single '-' followed by a single character, or |
|
144 |
# to start with a single '-' followed by a single character, a '=' and any string. |
|
145 |
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--' |
|
146 |
# |
|
147 |
# usage: parseIsOption <string> |
|
148 |
# arguments: <string> |
|
149 |
# A string to check whether it is an option. |
|
150 |
# return: 0 |
|
151 |
# <string> is an option. |
|
152 |
# -1 |
|
153 |
# <string> is not an option. |
|
154 |
# |
|
155 |
function parseIsOption { |
|
156 |
if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then |
|
157 |
return 0 |
|
158 |
else |
|
159 |
return -1 |
|
160 |
fi |
|
161 |
} |
|
162 |
|
|
163 |
### set the log file ########################################################### |
|
164 |
# Sets a specified <infile> as log file and checks whether it already exists. |
|
165 |
# If so, the log may either be appended to the file, its content can be cleared, |
|
166 |
# or no log is generated at all. |
|
167 |
# The resulting path is stored in <outvar>. |
|
168 |
# |
|
169 |
# usage: setLogFile [--option=<option>] [--quiet] <infile> <outvar> |
|
170 |
# arguments: --option=<option> |
|
171 |
# Select what to do if <file> already exists. |
|
172 |
# Possible values are 'a', 'c', 'r' and 'n'. |
|
173 |
# - a: append (starts with a separator) |
|
174 |
# - c: continue (does not insert a seperator) |
|
175 |
# - r: delete and restart |
|
176 |
# - n: no log |
|
177 |
# If no option is secified but <file> exists, an interactive selection is provided. |
|
178 |
# --quiet |
|
179 |
# Suppress all messages. |
|
180 |
# <infile> |
|
181 |
# Path of the wanted log file. |
|
182 |
# <outvar> |
|
183 |
# Variable to store the path of the log file to. |
|
184 |
# return: 0 |
|
185 |
# No error or warning occurred. |
|
186 |
# -1 |
|
187 |
# Error: invalid input |
|
188 |
# |
|
189 |
function setLogFile { |
|
190 |
local filepath="" |
|
191 |
local option="" |
|
192 |
local quiet=false |
|
193 |
|
|
194 |
# parse arguments |
|
195 |
local otherargs=() |
|
196 |
while [ $# -gt 0 ]; do |
|
197 |
if ( parseIsOption $1 ); then |
|
198 |
case "$1" in |
|
199 |
-o=*|--option=*) |
|
200 |
option=${1#*=}; shift 1;; |
|
201 |
-o*|--option*) |
|
202 |
option="$2"; shift 2;; |
|
203 |
-q|--quiet) |
|
204 |
quiet=true; shift 1;; |
|
205 |
*) |
|
206 |
printError "invalid option: $1\n"; shift 1;; |
|
207 |
esac |
|
208 |
else |
|
209 |
otherargs+=("$1") |
|
210 |
shift 1 |
|
211 |
fi |
|
212 |
done |
|
213 |
filepath=$(realpath ${otherargs[0]}) |
|
214 |
|
|
215 |
# if file already exists |
|
216 |
if [ -e $filepath ]; then |
|
217 |
# if no option was specified, ask what to do |
|
218 |
if [ -z "$option" ]; then |
|
219 |
printWarning "log file $filepath already esists\n" |
|
220 |
local userinput="" |
|
221 |
printf "Select what to do:\n" |
|
222 |
printf " [A] - append log\n" |
|
223 |
printf " [R] - restart log (delete existing file)\n" |
|
224 |
printf " [N] - no log\n" |
|
225 |
readUserInput "AaRrNn" userinput |
|
226 |
option=${userinput,,} |
|
227 |
fi |
|
228 |
# evaluate option |
|
229 |
case "$option" in |
|
230 |
a|c) |
|
231 |
if [ $quiet = false ]; then |
|
232 |
printInfo "appending log to $filepath\n" |
|
233 |
fi |
|
234 |
if [ $option != c ]; then |
|
235 |
printf "\n" >> $filepath |
|
236 |
printf "######################################################################\n" >> $filepath |
|
237 |
printf "\n" >> $filepath |
|
238 |
fi |
|
239 |
;; |
|
240 |
r) |
|
241 |
echo -n "" > $filepath |
|
242 |
if [ $quiet = false ]; then |
|
243 |
printInfo "content of $filepath wiped\n" |
|
244 |
fi |
|
245 |
;; |
|
246 |
n) |
|
247 |
if [ $quiet = false ]; then |
|
248 |
printInfo "no log file will be generated\n" |
|
249 |
fi |
|
250 |
filepath="" |
|
251 |
;; |
|
252 |
*) # sanity check (return error) |
|
253 |
printError "unexpected argument: $option\n"; return -1;; |
|
254 |
esac |
|
255 |
else |
|
256 |
if [ $quiet = false ]; then |
|
257 |
printInfo "log file set to $filepath\n" |
|
258 |
fi |
|
259 |
fi |
|
260 |
|
|
261 |
eval ${otherargs[1]}="$filepath" |
|
262 |
|
|
263 |
return 0 |
|
264 |
} |
|
265 |
|
|
266 |
### check whether commands are available ####################################### |
|
267 |
# Checks whether the specified commands are available and can be executed. |
|
268 |
# |
|
269 |
# usage: checkCommands [<command> <command> ...] |
|
270 |
# arguments: <command> |
|
271 |
# Name of the command to check. |
|
272 |
# return: 0 |
|
273 |
# All requested commands are available. |
|
274 |
# >0 |
|
275 |
# Number of requested commands that were not found. |
|
276 |
# -1 |
|
277 |
# No argument given. |
|
278 |
# |
|
279 |
function checkCommands { |
|
280 |
local status=0 |
|
281 |
|
|
282 |
# return if no argument was specified |
|
283 |
if [ $# -eq 0 ]; then |
|
284 |
return -1 |
|
285 |
fi |
|
286 |
|
|
287 |
# check all specified commands |
|
288 |
while [ $# -gt 0 ]; do |
|
289 |
command -v $1 &>/dev/null |
|
290 |
if [ $? -ne 0 ]; then |
|
291 |
printWarning "Command '$1' not available.\n" |
|
292 |
status=$((status + 1)) |
|
293 |
fi |
|
294 |
shift 1 |
|
295 |
done |
|
296 |
|
|
297 |
return $status |
|
298 |
} |
|
299 |
|
|
300 |
################################################################################ |
|
301 |
# SPECIFIC FUNCTIONS # |
|
302 |
################################################################################ |
|
26 |
# load library |
|
27 |
source "$(dirname ${BASH_SOURCE[0]})/tools/bash/setuplib.sh" |
|
303 | 28 |
|
304 | 29 |
### print welcome text ######################################################### |
305 | 30 |
# Prints a welcome message to standard out. |
tools/bash/setuplib.sh | ||
---|---|---|
1 |
################################################################################ |
|
2 |
# AMiRo-BLT is an bootloader and toolchain designed for the Autonomous Mini # |
|
3 |
# Robot (AMiRo) platform. # |
|
4 |
# Copyright (C) 2016..2020 Thomas Schöpping et al. # |
|
5 |
# # |
|
6 |
# This program is free software: you can redistribute it and/or modify # |
|
7 |
# it under the terms of the GNU General Public License as published by # |
|
8 |
# the Free Software Foundation, either version 3 of the License, or # |
|
9 |
# (at your option) any later version. # |
|
10 |
# # |
|
11 |
# This program is distributed in the hope that it will be useful, # |
|
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
|
14 |
# GNU General Public License for more details. # |
|
15 |
# # |
|
16 |
# You should have received a copy of the GNU General Public License # |
|
17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. # |
|
18 |
# # |
|
19 |
# This research/work was supported by the Cluster of Excellence Cognitive # |
|
20 |
# Interaction Technology 'CITEC' (EXC 277) at Bielefeld University, which is # |
|
21 |
# funded by the German Research Foundation (DFG). # |
|
22 |
################################################################################ |
|
23 |
|
|
24 |
### print an error message ##################################################### |
|
25 |
# Prints a error <message> to standard output. |
|
26 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
27 |
# |
|
28 |
# usage: printError <message> |
|
29 |
# arguments: <message> |
|
30 |
# Message string to print. |
|
31 |
# return: n/a |
|
32 |
# |
|
33 |
function printError { |
|
34 |
local string="ERROR: $1" |
|
35 |
# if a log file is specified |
|
36 |
if [ -n "$LOG_FILE" ]; then |
|
37 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
38 |
fi |
|
39 |
printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2 |
|
40 |
} |
|
41 |
|
|
42 |
### print a warning message #################################################### |
|
43 |
# Prints a warning <message> to standard output. |
|
44 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
45 |
# |
|
46 |
# usage: printMessage <message> |
|
47 |
# arguments: <message> |
|
48 |
# Message string to print. |
|
49 |
# return: n/a |
|
50 |
# |
|
51 |
function printWarning { |
|
52 |
local string="WARNING: $1" |
|
53 |
# if a log file is specified |
|
54 |
if [ -n "$LOG_FILE" ]; then |
|
55 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
56 |
fi |
|
57 |
printf "$(tput setaf 3)>>> $string$(tput sgr 0)" |
|
58 |
} |
|
59 |
|
|
60 |
### print an information message ############################################### |
|
61 |
# Prints an information <message> to standard output. |
|
62 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
63 |
# |
|
64 |
# usage: printInfo <message> |
|
65 |
# arguments: <message> |
|
66 |
# Message string to print. |
|
67 |
# return: n/a |
|
68 |
# |
|
69 |
function printInfo { |
|
70 |
local string="INFO: $1" |
|
71 |
# if a log file is specified |
|
72 |
if [ -n "$LOG_FILE" ]; then |
|
73 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
74 |
fi |
|
75 |
printf "$(tput setaf 2)>>> $string$(tput sgr 0)" |
|
76 |
} |
|
77 |
|
|
78 |
### print a message to file #################################################### |
|
79 |
# Appends a <message> to a log file, specified by the variable 'LOG_FILE'. |
|
80 |
# |
|
81 |
# usage printLog <message> |
|
82 |
# arguments: <message> |
|
83 |
# Message string to print. |
|
84 |
# return: n/a |
|
85 |
# |
|
86 |
function printLog { |
|
87 |
local string="LOG: $1" |
|
88 |
# if a log file is specified |
|
89 |
if [ -n "$LOG_FILE" ]; then |
|
90 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
91 |
fi |
|
92 |
} |
|
93 |
|
|
94 |
### exit the script normally ################################################### |
|
95 |
# Prints a delimiter and exits the script normally (returns 0). |
|
96 |
# |
|
97 |
# usage: quitScript |
|
98 |
# arguments: n/a |
|
99 |
# return: 0 |
|
100 |
# No error or warning occurred. |
|
101 |
# |
|
102 |
function quitScript { |
|
103 |
printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n" |
|
104 |
printf "\n" |
|
105 |
printf "######################################################################\n" |
|
106 |
exit 0 |
|
107 |
} |
|
108 |
|
|
109 |
### read a user input ########################################################## |
|
110 |
# Reads a single character user input from a set up <options> and stores it in |
|
111 |
# a given <return> variable. |
|
112 |
# |
|
113 |
# usage: readUserInput <options> <return> |
|
114 |
# arguments: <options> |
|
115 |
# String definiing the set of valid characters. |
|
116 |
# If the string is empty, the user can input any character. |
|
117 |
# <return> |
|
118 |
# Variable to store the selected character to. |
|
119 |
# return: n/a |
|
120 |
# |
|
121 |
function readUserInput { |
|
122 |
local input="" |
|
123 |
# read user input |
|
124 |
while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do |
|
125 |
read -p "your selection: " -n 1 -e input |
|
126 |
if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then |
|
127 |
printWarning "[$input] is no valid action\n" |
|
128 |
fi |
|
129 |
done |
|
130 |
printLog "[$input] has been selected\n" |
|
131 |
eval $2="$input" |
|
132 |
} |
|
133 |
|
|
134 |
### check whether argument is an option ######################################## |
|
135 |
# Checks a <string> whether it is an option. |
|
136 |
# Options are defined to either start with '--' followed by any string, or |
|
137 |
# to start with a single '-' followed by a single character, or |
|
138 |
# to start with a single '-' followed by a single character, a '=' and any string. |
|
139 |
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--' |
|
140 |
# |
|
141 |
# usage: parseIsOption <string> |
|
142 |
# arguments: <string> |
|
143 |
# A string to check whether it is an option. |
|
144 |
# return: 0 |
|
145 |
# <string> is an option. |
|
146 |
# -1 |
|
147 |
# <string> is not an option. |
|
148 |
# |
|
149 |
function parseIsOption { |
|
150 |
if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then |
|
151 |
return 0 |
|
152 |
else |
|
153 |
return -1 |
|
154 |
fi |
|
155 |
} |
|
156 |
|
|
157 |
### set the log file ########################################################### |
|
158 |
# Sets a specified <infile> as log file and checks whether it already exists. |
|
159 |
# If so, the log may either be appended to the file, its content can be cleared, |
|
160 |
# or no log is generated at all. |
|
161 |
# The resulting path is stored in <outvar>. |
|
162 |
# |
|
163 |
# usage: setLogFile [--option=<option>] [--quiet] <infile> <outvar> |
|
164 |
# arguments: --option=<option> |
|
165 |
# Select what to do if <file> already exists. |
|
166 |
# Possible values are 'a', 'c', 'r' and 'n'. |
|
167 |
# - a: append (starts with a separator) |
|
168 |
# - c: continue (does not insert a seperator) |
|
169 |
# - r: delete and restart |
|
170 |
# - n: no log |
|
171 |
# If no option is secified but <file> exists, an interactive selection is provided. |
|
172 |
# --quiet |
|
173 |
# Suppress all messages. |
|
174 |
# <infile> |
|
175 |
# Path of the wanted log file. |
|
176 |
# <outvar> |
|
177 |
# Variable to store the path of the log file to. |
|
178 |
# return: 0 |
|
179 |
# No error or warning occurred. |
|
180 |
# -1 |
|
181 |
# Error: invalid input |
|
182 |
# |
|
183 |
function setLogFile { |
|
184 |
local filepath="" |
|
185 |
local option="" |
|
186 |
local quiet=false |
|
187 |
|
|
188 |
# parse arguments |
|
189 |
local otherargs=() |
|
190 |
while [ $# -gt 0 ]; do |
|
191 |
if ( parseIsOption $1 ); then |
|
192 |
case "$1" in |
|
193 |
-o=*|--option=*) |
|
194 |
option=${1#*=}; shift 1;; |
|
195 |
-o*|--option*) |
|
196 |
option="$2"; shift 2;; |
|
197 |
-q|--quiet) |
|
198 |
quiet=true; shift 1;; |
|
199 |
*) |
|
200 |
printError "invalid option: $1\n"; shift 1;; |
|
201 |
esac |
|
202 |
else |
|
203 |
otherargs+=("$1") |
|
204 |
shift 1 |
|
205 |
fi |
|
206 |
done |
|
207 |
filepath=$(realpath ${otherargs[0]}) |
|
208 |
|
|
209 |
# if file already exists |
|
210 |
if [ -e $filepath ]; then |
|
211 |
# if no option was specified, ask what to do |
|
212 |
if [ -z "$option" ]; then |
|
213 |
printWarning "log file $filepath already esists\n" |
|
214 |
local userinput="" |
|
215 |
printf "Select what to do:\n" |
|
216 |
printf " [A] - append log\n" |
|
217 |
printf " [R] - restart log (delete existing file)\n" |
|
218 |
printf " [N] - no log\n" |
|
219 |
readUserInput "AaRrNn" userinput |
|
220 |
option=${userinput,,} |
|
221 |
fi |
|
222 |
# evaluate option |
|
223 |
case "$option" in |
|
224 |
a|c) |
|
225 |
if [ $quiet = false ]; then |
|
226 |
printInfo "appending log to $filepath\n" |
|
227 |
fi |
|
228 |
if [ $option != c ]; then |
|
229 |
printf "\n" >> $filepath |
|
230 |
printf "######################################################################\n" >> $filepath |
|
231 |
printf "\n" >> $filepath |
|
232 |
fi |
|
233 |
;; |
|
234 |
r) |
|
235 |
echo -n "" > $filepath |
|
236 |
if [ $quiet = false ]; then |
|
237 |
printInfo "content of $filepath wiped\n" |
|
238 |
fi |
|
239 |
;; |
|
240 |
n) |
|
241 |
if [ $quiet = false ]; then |
|
242 |
printInfo "no log file will be generated\n" |
|
243 |
fi |
|
244 |
filepath="" |
|
245 |
;; |
|
246 |
*) # sanity check (return error) |
|
247 |
printError "unexpected argument: $option\n"; return -1;; |
|
248 |
esac |
|
249 |
else |
|
250 |
if [ $quiet = false ]; then |
|
251 |
printInfo "log file set to $filepath\n" |
|
252 |
fi |
|
253 |
fi |
|
254 |
|
|
255 |
eval ${otherargs[1]}="$filepath" |
|
256 |
|
|
257 |
return 0 |
|
258 |
} |
|
259 |
|
|
260 |
### check whether commands are available ####################################### |
|
261 |
# Checks whether the specified commands are available and can be executed. |
|
262 |
# |
|
263 |
# usage: checkCommands [<command> <command> ...] |
|
264 |
# arguments: <command> |
|
265 |
# Name of the command to check. |
|
266 |
# return: 0 |
|
267 |
# All requested commands are available. |
|
268 |
# >0 |
|
269 |
# Number of requested commands that were not found. |
|
270 |
# -1 |
|
271 |
# No argument given. |
|
272 |
# |
|
273 |
function checkCommands { |
|
274 |
local status=0 |
|
275 |
|
|
276 |
# return if no argument was specified |
|
277 |
if [ $# -eq 0 ]; then |
|
278 |
return -1 |
|
279 |
fi |
|
280 |
|
|
281 |
# check all specified commands |
|
282 |
while [ $# -gt 0 ]; do |
|
283 |
command -v $1 &>/dev/null |
|
284 |
if [ $? -ne 0 ]; then |
|
285 |
printWarning "Command '$1' not available.\n" |
|
286 |
status=$((status + 1)) |
|
287 |
fi |
|
288 |
shift 1 |
|
289 |
done |
|
290 |
|
|
291 |
return $status |
|
292 |
} |
tools/compiler/GCC/gccsetup.sh | ||
---|---|---|
23 | 23 |
|
24 | 24 |
#!/bin/bash |
25 | 25 |
|
26 |
################################################################################ |
|
27 |
# GENERIC FUNCTIONS # |
|
28 |
################################################################################ |
|
29 |
|
|
30 |
### print an error message ##################################################### |
|
31 |
# Prints a error <message> to standard output. |
|
32 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
33 |
# |
|
34 |
# usage: printError <message> |
|
35 |
# arguments: <message> |
|
36 |
# Message string to print. |
|
37 |
# return: n/a |
|
38 |
# |
|
39 |
function printError { |
|
40 |
local string="ERROR: $1" |
|
41 |
# if a log file is specified |
|
42 |
if [ -n "$LOG_FILE" ]; then |
|
43 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
44 |
fi |
|
45 |
printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2 |
|
46 |
} |
|
47 |
|
|
48 |
### print a warning message #################################################### |
|
49 |
# Prints a warning <message> to standard output. |
|
50 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
51 |
# |
|
52 |
# usage: printMessage <message> |
|
53 |
# arguments: <message> |
|
54 |
# Message string to print. |
|
55 |
# return: n/a |
|
56 |
# |
|
57 |
function printWarning { |
|
58 |
local string="WARNING: $1" |
|
59 |
# if a log file is specified |
|
60 |
if [ -n "$LOG_FILE" ]; then |
|
61 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
62 |
fi |
|
63 |
printf "$(tput setaf 3)>>> $string$(tput sgr 0)" |
|
64 |
} |
|
65 |
|
|
66 |
### print an information message ############################################### |
|
67 |
# Prints an information <message> to standard output. |
|
68 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
69 |
# |
|
70 |
# usage: printInfo <message> |
|
71 |
# arguments: <message> |
|
72 |
# Message string to print. |
|
73 |
# return: n/a |
|
74 |
# |
|
75 |
function printInfo { |
|
76 |
local string="INFO: $1" |
|
77 |
# if a log file is specified |
|
78 |
if [ -n "$LOG_FILE" ]; then |
|
79 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
80 |
fi |
|
81 |
printf "$(tput setaf 2)>>> $string$(tput sgr 0)" |
|
82 |
} |
|
83 |
|
|
84 |
### print a message to file #################################################### |
|
85 |
# Appends a <message> to a log file, specified by the variable 'LOG_FILE'. |
|
86 |
# |
|
87 |
# usage printLog <message> |
|
88 |
# arguments: <message> |
|
89 |
# Message string to print. |
|
90 |
# return: n/a |
|
91 |
# |
|
92 |
function printLog { |
|
93 |
local string="LOG: $1" |
|
94 |
# if a log file is specified |
|
95 |
if [ -n "$LOG_FILE" ]; then |
|
96 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
97 |
fi |
|
98 |
} |
|
99 |
|
|
100 |
### exit the script normally ################################################### |
|
101 |
# Prints a delimiter and exits the script normally (returns 0). |
|
102 |
# |
|
103 |
# usage: quitScript |
|
104 |
# arguments: n/a |
|
105 |
# return: 0 |
|
106 |
# No error or warning occurred. |
|
107 |
# |
|
108 |
function quitScript { |
|
109 |
printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n" |
|
110 |
printf "\n" |
|
111 |
printf "######################################################################\n" |
|
112 |
exit 0 |
|
113 |
} |
|
114 |
|
|
115 |
### read a user input ########################################################## |
|
116 |
# Reads a single character user input from a set up <options> and stores it in |
|
117 |
# a given <return> variable. |
|
118 |
# |
|
119 |
# usage: readUserInput <options> <return> |
|
120 |
# arguments: <options> |
|
121 |
# String definiing the set of valid characters. |
|
122 |
# If the string is empty, the user can input any character. |
|
123 |
# <return> |
|
124 |
# Variable to store the selected character to. |
|
125 |
# return: n/a |
|
126 |
# |
|
127 |
function readUserInput { |
|
128 |
local input="" |
|
129 |
# read user input |
|
130 |
while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do |
|
131 |
read -p "your selection: " -n 1 -e input |
|
132 |
if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then |
|
133 |
printWarning "[$input] is no valid action\n" |
|
134 |
fi |
|
135 |
done |
|
136 |
printLog "[$input] has been selected\n" |
|
137 |
eval $2="$input" |
|
138 |
} |
|
139 |
|
|
140 |
### check whether argument is an option ######################################## |
|
141 |
# Checks a <string> whether it is an option. |
|
142 |
# Options are defined to either start with '--' followed by any string, or |
|
143 |
# to start with a single '-' followed by a single character, or |
|
144 |
# to start with a single '-' followed by a single character, a '=' and any string. |
|
145 |
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--' |
|
146 |
# |
|
147 |
# usage: parseIsOption <string> |
|
148 |
# arguments: <string> |
|
149 |
# A string to check whether it is an option. |
|
150 |
# return: 0 |
|
151 |
# <string> is an option. |
|
152 |
# -1 |
|
153 |
# <string> is not an option. |
|
154 |
# |
|
155 |
function parseIsOption { |
|
156 |
if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then |
|
157 |
return 0 |
|
158 |
else |
|
159 |
return -1 |
|
160 |
fi |
|
161 |
} |
|
162 |
|
|
163 |
### set the log file ########################################################### |
|
164 |
# Sets a specified <infile> as log file and checks whether it already exists. |
|
165 |
# If so, the log may either be appended to the file, its content can be cleared, |
|
166 |
# or no log is generated at all. |
|
167 |
# The resulting path is stored in <outvar>. |
|
168 |
# |
|
169 |
# usage: setLogFile [--option=<option>] [--quiet] <infile> <outvar> |
|
170 |
# arguments: --option=<option> |
|
171 |
# Select what to do if <file> already exists. |
|
172 |
# Possible values are 'a', 'c', 'r' and 'n'. |
|
173 |
# - a: append (starts with a separator) |
|
174 |
# - c: continue (does not insert a seperator) |
|
175 |
# - r: delete and restart |
|
176 |
# - n: no log |
|
177 |
# If no option is secified but <file> exists, an interactive selection is provided. |
|
178 |
# --quiet |
|
179 |
# Suppress all messages. |
|
180 |
# <infile> |
|
181 |
# Path of the wanted log file. |
|
182 |
# <outvar> |
|
183 |
# Variable to store the path of the log file to. |
|
184 |
# return: 0 |
|
185 |
# No error or warning occurred. |
|
186 |
# -1 |
|
187 |
# Error: invalid input |
|
188 |
# |
|
189 |
function setLogFile { |
|
190 |
local filepath="" |
|
191 |
local option="" |
|
192 |
local quiet=false |
|
193 |
|
|
194 |
# parse arguments |
|
195 |
local otherargs=() |
|
196 |
while [ $# -gt 0 ]; do |
|
197 |
if ( parseIsOption $1 ); then |
|
198 |
case "$1" in |
|
199 |
-o=*|--option=*) |
|
200 |
option=${1#*=}; shift 1;; |
|
201 |
-o*|--option*) |
|
202 |
option="$2"; shift 2;; |
|
203 |
-q|--quiet) |
|
204 |
quiet=true; shift 1;; |
|
205 |
*) |
|
206 |
printError "invalid option: $1\n"; shift 1;; |
|
207 |
esac |
|
208 |
else |
|
209 |
otherargs+=("$1") |
|
210 |
shift 1 |
|
211 |
fi |
|
212 |
done |
|
213 |
filepath=$(realpath ${otherargs[0]}) |
|
214 |
|
|
215 |
# if file already exists |
|
216 |
if [ -e $filepath ]; then |
|
217 |
# if no option was specified, ask what to do |
|
218 |
if [ -z "$option" ]; then |
|
219 |
printWarning "log file $filepath already esists\n" |
|
220 |
local userinput="" |
|
221 |
printf "Select what to do:\n" |
|
222 |
printf " [A] - append log\n" |
|
223 |
printf " [R] - restart log (delete existing file)\n" |
|
224 |
printf " [N] - no log\n" |
|
225 |
readUserInput "AaRrNn" userinput |
|
226 |
option=${userinput,,} |
|
227 |
fi |
|
228 |
# evaluate option |
|
229 |
case "$option" in |
|
230 |
a|c) |
|
231 |
if [ $quiet = false ]; then |
|
232 |
printInfo "appending log to $filepath\n" |
|
233 |
fi |
|
234 |
if [ $option != c ]; then |
|
235 |
printf "\n" >> $filepath |
|
236 |
printf "######################################################################\n" >> $filepath |
|
237 |
printf "\n" >> $filepath |
|
238 |
fi |
|
239 |
;; |
|
240 |
r) |
|
241 |
echo -n "" > $filepath |
|
242 |
if [ $quiet = false ]; then |
|
243 |
printInfo "content of $filepath wiped\n" |
|
244 |
fi |
|
245 |
;; |
|
246 |
n) |
|
247 |
if [ $quiet = false ]; then |
|
248 |
printInfo "no log file will be generated\n" |
|
249 |
fi |
|
250 |
filepath="" |
|
251 |
;; |
|
252 |
*) # sanity check (return error) |
|
253 |
printError "unexpected argument: $option\n"; return -1;; |
|
254 |
esac |
|
255 |
else |
|
256 |
if [ $quiet = false ]; then |
|
257 |
printInfo "log file set to $filepath\n" |
|
258 |
fi |
|
259 |
fi |
|
260 |
|
|
261 |
eval ${otherargs[1]}="$filepath" |
|
262 |
|
|
263 |
return 0 |
|
264 |
} |
|
265 |
|
|
266 |
### check whether commands are available ####################################### |
|
267 |
# Checks whether the specified commands are available and can be executed. |
|
268 |
# |
|
269 |
# usage: checkCommands [<command> <command> ...] |
|
270 |
# arguments: <command> |
|
271 |
# Name of the command to check. |
|
272 |
# return: 0 |
|
273 |
# All requested commands are available. |
|
274 |
# >0 |
|
275 |
# Number of requested commands that were not found. |
|
276 |
# -1 |
|
277 |
# No argument given. |
|
278 |
# |
|
279 |
function checkCommands { |
|
280 |
local status=0 |
|
281 |
|
|
282 |
# return if no argument was specified |
|
283 |
if [ $# -eq 0 ]; then |
|
284 |
return -1 |
|
285 |
fi |
|
286 |
|
|
287 |
# check all specified commands |
|
288 |
while [ $# -gt 0 ]; do |
|
289 |
command -v $1 &>/dev/null |
|
290 |
if [ $? -ne 0 ]; then |
|
291 |
printWarning "Command '$1' not available.\n" |
|
292 |
status=$((status + 1)) |
|
293 |
fi |
|
294 |
shift 1 |
|
295 |
done |
|
296 |
|
|
297 |
return $status |
|
298 |
} |
|
299 |
|
|
300 |
################################################################################ |
|
301 |
# SPECIFIC FUNCTIONS # |
|
302 |
################################################################################ |
|
26 |
# load library |
|
27 |
source "$(dirname ${BASH_SOURCE[0]})/../../bash/setuplib.sh" |
|
303 | 28 |
|
304 | 29 |
### print welcome text ######################################################### |
305 | 30 |
# Prints a welcome message to standard out. |
tools/compiler/compilersetup.sh | ||
---|---|---|
23 | 23 |
|
24 | 24 |
#!/bin/bash |
25 | 25 |
|
26 |
################################################################################ |
|
27 |
# GENERIC FUNCTIONS # |
|
28 |
################################################################################ |
|
29 |
|
|
30 |
### print an error message ##################################################### |
|
31 |
# Prints a error <message> to standard output. |
|
32 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
33 |
# |
|
34 |
# usage: printError <message> |
|
35 |
# arguments: <message> |
|
36 |
# Message string to print. |
|
37 |
# return: n/a |
|
38 |
# |
|
39 |
function printError { |
|
40 |
local string="ERROR: $1" |
|
41 |
# if a log file is specified |
|
42 |
if [ -n "$LOG_FILE" ]; then |
|
43 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
44 |
fi |
|
45 |
printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2 |
|
46 |
} |
|
47 |
|
|
48 |
### print a warning message #################################################### |
|
49 |
# Prints a warning <message> to standard output. |
|
50 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
51 |
# |
|
52 |
# usage: printMessage <message> |
|
53 |
# arguments: <message> |
|
54 |
# Message string to print. |
|
55 |
# return: n/a |
|
56 |
# |
|
57 |
function printWarning { |
|
58 |
local string="WARNING: $1" |
|
59 |
# if a log file is specified |
|
60 |
if [ -n "$LOG_FILE" ]; then |
|
61 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
62 |
fi |
|
63 |
printf "$(tput setaf 3)>>> $string$(tput sgr 0)" |
|
64 |
} |
|
65 |
|
|
66 |
### print an information message ############################################### |
|
67 |
# Prints an information <message> to standard output. |
|
68 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
69 |
# |
|
70 |
# usage: printInfo <message> |
|
71 |
# arguments: <message> |
|
72 |
# Message string to print. |
|
73 |
# return: n/a |
|
74 |
# |
|
75 |
function printInfo { |
|
76 |
local string="INFO: $1" |
|
77 |
# if a log file is specified |
|
78 |
if [ -n "$LOG_FILE" ]; then |
|
79 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
80 |
fi |
|
81 |
printf "$(tput setaf 2)>>> $string$(tput sgr 0)" |
|
82 |
} |
|
83 |
|
|
84 |
### print a message to file #################################################### |
|
85 |
# Appends a <message> to a log file, specified by the variable 'LOG_FILE'. |
|
86 |
# |
|
87 |
# usage printLog <message> |
|
88 |
# arguments: <message> |
|
89 |
# Message string to print. |
|
90 |
# return: n/a |
|
91 |
# |
|
92 |
function printLog { |
|
93 |
local string="LOG: $1" |
|
94 |
# if a log file is specified |
|
95 |
if [ -n "$LOG_FILE" ]; then |
|
96 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
97 |
fi |
|
98 |
} |
|
99 |
|
|
100 |
### exit the script normally ################################################### |
|
101 |
# Prints a delimiter and exits the script normally (returns 0). |
|
102 |
# |
|
103 |
# usage: quitScript |
|
104 |
# arguments: n/a |
|
105 |
# return: 0 |
|
106 |
# No error or warning occurred. |
|
107 |
# |
|
108 |
function quitScript { |
|
109 |
printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n" |
|
110 |
printf "\n" |
|
111 |
printf "######################################################################\n" |
|
112 |
exit 0 |
|
113 |
} |
|
114 |
|
|
115 |
### read a user input ########################################################## |
|
116 |
# Reads a single character user input from a set up <options> and stores it in |
|
117 |
# a given <return> variable. |
|
118 |
# |
|
119 |
# usage: readUserInput <options> <return> |
|
120 |
# arguments: <options> |
|
121 |
# String definiing the set of valid characters. |
|
122 |
# If the string is empty, the user can input any character. |
|
123 |
# <return> |
|
124 |
# Variable to store the selected character to. |
|
125 |
# return: n/a |
|
126 |
# |
|
127 |
function readUserInput { |
|
128 |
local input="" |
|
129 |
# read user input |
|
130 |
while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do |
|
131 |
read -p "your selection: " -n 1 -e input |
|
132 |
if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then |
|
133 |
printWarning "[$input] is no valid action\n" |
|
134 |
fi |
|
135 |
done |
|
136 |
printLog "[$input] has been selected\n" |
|
137 |
eval $2="$input" |
|
138 |
} |
|
139 |
|
|
140 |
### check whether argument is an option ######################################## |
|
141 |
# Checks a <string> whether it is an option. |
|
142 |
# Options are defined to either start with '--' followed by any string, or |
|
143 |
# to start with a single '-' followed by a single character, or |
|
144 |
# to start with a single '-' followed by a single character, a '=' and any string. |
|
145 |
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--' |
|
146 |
# |
|
147 |
# usage: parseIsOption <string> |
|
148 |
# arguments: <string> |
|
149 |
# A string to check whether it is an option. |
|
150 |
# return: 0 |
|
151 |
# <string> is an option. |
|
152 |
# -1 |
|
153 |
# <string> is not an option. |
|
154 |
# |
|
155 |
function parseIsOption { |
|
156 |
if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then |
|
157 |
return 0 |
|
158 |
else |
|
159 |
return -1 |
|
160 |
fi |
|
161 |
} |
|
162 |
|
|
163 |
### set the log file ########################################################### |
|
164 |
# Sets a specified <infile> as log file and checks whether it already exists. |
|
165 |
# If so, the log may either be appended to the file, its content can be cleared, |
|
166 |
# or no log is generated at all. |
|
167 |
# The resulting path is stored in <outvar>. |
|
168 |
# |
|
169 |
# usage: setLogFile [--option=<option>] [--quiet] <infile> <outvar> |
|
170 |
# arguments: --option=<option> |
|
171 |
# Select what to do if <file> already exists. |
|
172 |
# Possible values are 'a', 'c', 'r' and 'n'. |
|
173 |
# - a: append (starts with a separator) |
|
174 |
# - c: continue (does not insert a seperator) |
|
175 |
# - r: delete and restart |
|
176 |
# - n: no log |
|
177 |
# If no option is secified but <file> exists, an interactive selection is provided. |
|
178 |
# --quiet |
|
179 |
# Suppress all messages. |
|
180 |
# <infile> |
|
181 |
# Path of the wanted log file. |
|
182 |
# <outvar> |
|
183 |
# Variable to store the path of the log file to. |
|
184 |
# return: 0 |
|
185 |
# No error or warning occurred. |
|
186 |
# -1 |
|
187 |
# Error: invalid input |
|
188 |
# |
|
189 |
function setLogFile { |
|
190 |
local filepath="" |
|
191 |
local option="" |
|
192 |
local quiet=false |
|
193 |
|
|
194 |
# parse arguments |
|
195 |
local otherargs=() |
|
196 |
while [ $# -gt 0 ]; do |
|
197 |
if ( parseIsOption $1 ); then |
|
198 |
case "$1" in |
|
199 |
-o=*|--option=*) |
|
200 |
option=${1#*=}; shift 1;; |
|
201 |
-o*|--option*) |
|
202 |
option="$2"; shift 2;; |
|
203 |
-q|--quiet) |
|
204 |
quiet=true; shift 1;; |
|
205 |
*) |
|
206 |
printError "invalid option: $1\n"; shift 1;; |
|
207 |
esac |
|
208 |
else |
|
209 |
otherargs+=("$1") |
|
210 |
shift 1 |
|
211 |
fi |
|
212 |
done |
|
213 |
filepath=$(realpath ${otherargs[0]}) |
|
214 |
|
|
215 |
# if file already exists |
|
216 |
if [ -e $filepath ]; then |
|
217 |
# if no option was specified, ask what to do |
|
218 |
if [ -z "$option" ]; then |
|
219 |
printWarning "log file $filepath already esists\n" |
|
220 |
local userinput="" |
|
221 |
printf "Select what to do:\n" |
|
222 |
printf " [A] - append log\n" |
|
223 |
printf " [R] - restart log (delete existing file)\n" |
|
224 |
printf " [N] - no log\n" |
|
225 |
readUserInput "AaRrNn" userinput |
|
226 |
option=${userinput,,} |
|
227 |
fi |
|
228 |
# evaluate option |
|
229 |
case "$option" in |
|
230 |
a|c) |
|
231 |
if [ $quiet = false ]; then |
|
232 |
printInfo "appending log to $filepath\n" |
|
233 |
fi |
|
234 |
if [ $option != c ]; then |
|
235 |
printf "\n" >> $filepath |
|
236 |
printf "######################################################################\n" >> $filepath |
|
237 |
printf "\n" >> $filepath |
|
238 |
fi |
|
239 |
;; |
|
240 |
r) |
|
241 |
echo -n "" > $filepath |
|
242 |
if [ $quiet = false ]; then |
|
243 |
printInfo "content of $filepath wiped\n" |
|
244 |
fi |
|
245 |
;; |
|
246 |
n) |
|
247 |
if [ $quiet = false ]; then |
|
248 |
printInfo "no log file will be generated\n" |
|
249 |
fi |
|
250 |
filepath="" |
|
251 |
;; |
|
252 |
*) # sanity check (return error) |
|
253 |
printError "unexpected argument: $option\n"; return -1;; |
|
254 |
esac |
|
255 |
else |
|
256 |
if [ $quiet = false ]; then |
|
257 |
printInfo "log file set to $filepath\n" |
|
258 |
fi |
|
259 |
fi |
|
260 |
|
|
261 |
eval ${otherargs[1]}="$filepath" |
|
262 |
|
|
263 |
return 0 |
|
264 |
} |
|
265 |
|
|
266 |
### check whether commands are available ####################################### |
|
267 |
# Checks whether the specified commands are available and can be executed. |
|
268 |
# |
|
269 |
# usage: checkCommands [<command> <command> ...] |
|
270 |
# arguments: <command> |
|
271 |
# Name of the command to check. |
|
272 |
# return: 0 |
|
273 |
# All requested commands are available. |
|
274 |
# >0 |
|
275 |
# Number of requested commands that were not found. |
|
276 |
# -1 |
|
277 |
# No argument given. |
|
278 |
# |
|
279 |
function checkCommands { |
|
280 |
local status=0 |
|
281 |
|
|
282 |
# return if no argument was specified |
|
283 |
if [ $# -eq 0 ]; then |
|
284 |
return -1 |
|
285 |
fi |
|
286 |
|
|
287 |
# check all specified commands |
|
288 |
while [ $# -gt 0 ]; do |
|
289 |
command -v $1 &>/dev/null |
|
290 |
if [ $? -ne 0 ]; then |
|
291 |
printWarning "Command '$1' not available.\n" |
|
292 |
status=$((status + 1)) |
|
293 |
fi |
|
294 |
shift 1 |
|
295 |
done |
|
296 |
|
|
297 |
return $status |
|
298 |
} |
|
299 |
|
|
300 |
################################################################################ |
|
301 |
# SPECIFIC FUNCTIONS # |
|
302 |
################################################################################ |
|
26 |
# load library |
|
27 |
source "$(dirname ${BASH_SOURCE[0]})/../bash/setuplib.sh" |
|
303 | 28 |
|
304 | 29 |
### print welcome text ######################################################### |
305 | 30 |
# Prints a welcome message to standard out. |
tools/ide/QtCreator/QtCreatorSetup.sh | ||
---|---|---|
23 | 23 |
|
24 | 24 |
#!/bin/bash |
25 | 25 |
|
26 |
################################################################################ |
|
27 |
# GENERIC FUNCTIONS # |
|
28 |
################################################################################ |
|
29 |
|
|
30 |
### print an error message ##################################################### |
|
31 |
# Prints a error <message> to standard output. |
|
32 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
33 |
# |
|
34 |
# usage: printError <message> |
|
35 |
# arguments: <message> |
|
36 |
# Message string to print. |
|
37 |
# return: n/a |
|
38 |
# |
|
39 |
function printError { |
|
40 |
local string="ERROR: $1" |
|
41 |
# if a log file is specified |
|
42 |
if [ -n "$LOG_FILE" ]; then |
|
43 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
44 |
fi |
|
45 |
printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2 |
|
46 |
} |
|
47 |
|
|
48 |
### print a warning message #################################################### |
|
49 |
# Prints a warning <message> to standard output. |
|
50 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
51 |
# |
|
52 |
# usage: printMessage <message> |
|
53 |
# arguments: <message> |
|
54 |
# Message string to print. |
|
55 |
# return: n/a |
|
56 |
# |
|
57 |
function printWarning { |
|
58 |
local string="WARNING: $1" |
|
59 |
# if a log file is specified |
|
60 |
if [ -n "$LOG_FILE" ]; then |
|
61 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
62 |
fi |
|
63 |
printf "$(tput setaf 3)>>> $string$(tput sgr 0)" |
|
64 |
} |
|
65 |
|
|
66 |
### print an information message ############################################### |
|
67 |
# Prints an information <message> to standard output. |
|
68 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
69 |
# |
|
70 |
# usage: printInfo <message> |
|
71 |
# arguments: <message> |
|
72 |
# Message string to print. |
|
73 |
# return: n/a |
|
74 |
# |
|
75 |
function printInfo { |
|
76 |
local string="INFO: $1" |
|
77 |
# if a log file is specified |
|
78 |
if [ -n "$LOG_FILE" ]; then |
|
79 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
80 |
fi |
|
81 |
printf "$(tput setaf 2)>>> $string$(tput sgr 0)" |
|
82 |
} |
|
83 |
|
|
84 |
### print a message to file #################################################### |
|
85 |
# Appends a <message> to a log file, specified by the variable 'LOG_FILE'. |
|
86 |
# |
|
87 |
# usage printLog <message> |
|
88 |
# arguments: <message> |
|
89 |
# Message string to print. |
|
90 |
# return: n/a |
|
91 |
# |
|
92 |
function printLog { |
|
93 |
local string="LOG: $1" |
|
94 |
# if a log file is specified |
|
95 |
if [ -n "$LOG_FILE" ]; then |
|
96 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
97 |
fi |
|
98 |
} |
|
99 |
|
|
100 |
### exit the script normally ################################################### |
|
101 |
# Prints a delimiter and exits the script normally (returns 0). |
|
102 |
# |
|
103 |
# usage: quitScript |
|
104 |
# arguments: n/a |
|
105 |
# return: 0 |
|
106 |
# No error or warning occurred. |
|
107 |
# |
|
108 |
function quitScript { |
|
109 |
printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n" |
|
110 |
printf "\n" |
|
111 |
printf "######################################################################\n" |
|
112 |
exit 0 |
|
113 |
} |
|
114 |
|
|
115 |
### read a user input ########################################################## |
|
116 |
# Reads a single character user input from a set up <options> and stores it in |
|
117 |
# a given <return> variable. |
|
118 |
# |
|
119 |
# usage: readUserInput <options> <return> |
|
120 |
# arguments: <options> |
|
121 |
# String definiing the set of valid characters. |
|
122 |
# If the string is empty, the user can input any character. |
|
123 |
# <return> |
|
124 |
# Variable to store the selected character to. |
|
125 |
# return: n/a |
|
126 |
# |
|
127 |
function readUserInput { |
|
128 |
local input="" |
|
129 |
# read user input |
|
130 |
while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do |
|
131 |
read -p "your selection: " -n 1 -e input |
|
132 |
if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then |
|
133 |
printWarning "[$input] is no valid action\n" |
|
134 |
fi |
|
135 |
done |
|
136 |
printLog "[$input] has been selected\n" |
|
137 |
eval $2="$input" |
|
138 |
} |
|
139 |
|
|
140 |
### check whether argument is an option ######################################## |
|
141 |
# Checks a <string> whether it is an option. |
|
142 |
# Options are defined to either start with '--' followed by any string, or |
|
143 |
# to start with a single '-' followed by a single character, or |
|
144 |
# to start with a single '-' followed by a single character, a '=' and any string. |
|
145 |
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--' |
|
146 |
# |
|
147 |
# usage: parseIsOption <string> |
|
148 |
# arguments: <string> |
|
149 |
# A string to check whether it is an option. |
|
150 |
# return: 0 |
|
151 |
# <string> is an option. |
|
152 |
# -1 |
|
153 |
# <string> is not an option. |
|
154 |
# |
|
155 |
function parseIsOption { |
|
156 |
if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then |
|
157 |
return 0 |
|
158 |
else |
|
159 |
return -1 |
|
160 |
fi |
|
161 |
} |
|
162 |
|
|
163 |
### set the log file ########################################################### |
|
164 |
# Sets a specified <infile> as log file and checks whether it already exists. |
|
165 |
# If so, the log may either be appended to the file, its content can be cleared, |
|
166 |
# or no log is generated at all. |
|
167 |
# The resulting path is stored in <outvar>. |
|
168 |
# |
|
169 |
# usage: setLogFile [--option=<option>] [--quiet] <infile> <outvar> |
|
170 |
# arguments: --option=<option> |
|
171 |
# Select what to do if <file> already exists. |
|
172 |
# Possible values are 'a', 'c', 'r' and 'n'. |
|
173 |
# - a: append (starts with a separator) |
|
174 |
# - c: continue (does not insert a seperator) |
|
175 |
# - r: delete and restart |
|
176 |
# - n: no log |
|
177 |
# If no option is secified but <file> exists, an interactive selection is provided. |
|
178 |
# --quiet |
|
179 |
# Suppress all messages. |
|
180 |
# <infile> |
|
181 |
# Path of the wanted log file. |
|
182 |
# <outvar> |
|
183 |
# Variable to store the path of the log file to. |
|
184 |
# return: 0 |
|
185 |
# No error or warning occurred. |
|
186 |
# -1 |
|
187 |
# Error: invalid input |
|
188 |
# |
|
189 |
function setLogFile { |
|
190 |
local filepath="" |
|
191 |
local option="" |
|
192 |
local quiet=false |
|
193 |
|
|
194 |
# parse arguments |
|
195 |
local otherargs=() |
|
196 |
while [ $# -gt 0 ]; do |
|
197 |
if ( parseIsOption $1 ); then |
|
198 |
case "$1" in |
|
199 |
-o=*|--option=*) |
|
200 |
option=${1#*=}; shift 1;; |
|
201 |
-o*|--option*) |
|
202 |
option="$2"; shift 2;; |
|
203 |
-q|--quiet) |
|
204 |
quiet=true; shift 1;; |
|
205 |
*) |
|
206 |
printError "invalid option: $1\n"; shift 1;; |
|
207 |
esac |
|
208 |
else |
|
209 |
otherargs+=("$1") |
|
210 |
shift 1 |
|
211 |
fi |
|
212 |
done |
|
213 |
filepath=$(realpath ${otherargs[0]}) |
|
214 |
|
|
215 |
# if file already exists |
|
216 |
if [ -e $filepath ]; then |
|
217 |
# if no option was specified, ask what to do |
|
218 |
if [ -z "$option" ]; then |
|
219 |
printWarning "log file $filepath already esists\n" |
|
220 |
local userinput="" |
|
221 |
printf "Select what to do:\n" |
|
222 |
printf " [A] - append log\n" |
|
223 |
printf " [R] - restart log (delete existing file)\n" |
|
224 |
printf " [N] - no log\n" |
|
225 |
readUserInput "AaRrNn" userinput |
|
226 |
option=${userinput,,} |
|
227 |
fi |
|
228 |
# evaluate option |
|
229 |
case "$option" in |
|
230 |
a|c) |
|
231 |
if [ $quiet = false ]; then |
|
232 |
printInfo "appending log to $filepath\n" |
|
233 |
fi |
|
234 |
if [ $option != c ]; then |
|
235 |
printf "\n" >> $filepath |
|
236 |
printf "######################################################################\n" >> $filepath |
|
237 |
printf "\n" >> $filepath |
|
238 |
fi |
|
239 |
;; |
|
240 |
r) |
|
241 |
echo -n "" > $filepath |
|
242 |
if [ $quiet = false ]; then |
|
243 |
printInfo "content of $filepath wiped\n" |
|
244 |
fi |
|
245 |
;; |
|
246 |
n) |
|
247 |
if [ $quiet = false ]; then |
|
248 |
printInfo "no log file will be generated\n" |
|
249 |
fi |
|
250 |
filepath="" |
|
251 |
;; |
|
252 |
*) # sanity check (return error) |
|
253 |
printError "unexpected argument: $option\n"; return -1;; |
|
254 |
esac |
|
255 |
else |
|
256 |
if [ $quiet = false ]; then |
|
257 |
printInfo "log file set to $filepath\n" |
|
258 |
fi |
|
259 |
fi |
|
260 |
|
|
261 |
eval ${otherargs[1]}="$filepath" |
|
262 |
|
|
263 |
return 0 |
|
264 |
} |
|
265 |
|
|
266 |
### check whether commands are available ####################################### |
|
267 |
# Checks whether the specified commands are available and can be executed. |
|
268 |
# |
|
269 |
# usage: checkCommands [<command> <command> ...] |
|
270 |
# arguments: <command> |
|
271 |
# Name of the command to check. |
|
272 |
# return: 0 |
|
273 |
# All requested commands are available. |
|
274 |
# >0 |
|
275 |
# Number of requested commands that were not found. |
|
276 |
# -1 |
|
277 |
# No argument given. |
|
278 |
# |
|
279 |
function checkCommands { |
|
280 |
local status=0 |
|
281 |
|
|
282 |
# return if no argument was specified |
|
283 |
if [ $# -eq 0 ]; then |
|
284 |
return -1 |
|
285 |
fi |
|
286 |
|
|
287 |
# check all specified commands |
|
288 |
while [ $# -gt 0 ]; do |
|
289 |
command -v $1 &>/dev/null |
|
290 |
if [ $? -ne 0 ]; then |
|
291 |
printWarning "Command '$1' not available.\n" |
|
292 |
status=$((status + 1)) |
|
293 |
fi |
|
294 |
shift 1 |
|
295 |
done |
|
296 |
|
|
297 |
return $status |
|
298 |
} |
|
299 |
|
|
300 |
################################################################################ |
|
301 |
# SPECIFIC FUNCTIONS # |
|
302 |
################################################################################ |
|
26 |
# load library |
|
27 |
source "$(dirname ${BASH_SOURCE[0]})/../../bash/setuplib.sh" |
|
303 | 28 |
|
304 | 29 |
### print welcome text ######################################################### |
305 | 30 |
# Prints a welcome message to standard out. |
tools/ide/idesetup.sh | ||
---|---|---|
23 | 23 |
|
24 | 24 |
#!/bin/bash |
25 | 25 |
|
26 |
################################################################################ |
|
27 |
# GENERIC FUNCTIONS # |
|
28 |
################################################################################ |
|
29 |
|
|
30 |
### print an error message ##################################################### |
|
31 |
# Prints a error <message> to standard output. |
|
32 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
33 |
# |
|
34 |
# usage: printError <message> |
|
35 |
# arguments: <message> |
|
36 |
# Message string to print. |
|
37 |
# return: n/a |
|
38 |
# |
|
39 |
function printError { |
|
40 |
local string="ERROR: $1" |
|
41 |
# if a log file is specified |
|
42 |
if [ -n "$LOG_FILE" ]; then |
|
43 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
|
44 |
fi |
|
45 |
printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2 |
|
46 |
} |
|
47 |
|
|
48 |
### print a warning message #################################################### |
|
49 |
# Prints a warning <message> to standard output. |
|
50 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
|
51 |
# |
|
52 |
# usage: printMessage <message> |
|
53 |
# arguments: <message> |
|
54 |
# Message string to print. |
Also available in: Unified diff