amiro-blt / ide / setup_IDE.sh @ 1da30dfc
History | View | Annotate | Download (13.5 KB)
1 |
################################################################################ |
---|---|
2 |
# AMiRo-BLT is an bootloader and toolchain designed for the Autonomous Mini # |
3 |
# Robot (AMiRo) platform. # |
4 |
# Copyright (C) 2016..2017 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 |
#!/bin/bash |
25 |
|
26 |
################################################################################ |
27 |
# GENERIC FUNCTIONS # |
28 |
################################################################################ |
29 |
|
30 |
### print an error |
31 |
# arguments: |
32 |
# - error string [required] |
33 |
# return: n/a |
34 |
function printError { |
35 |
local string="ERROR: $1" |
36 |
|
37 |
# if a log file is specified |
38 |
if [ ! -z $LOG_FILE ]; then |
39 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
40 |
fi |
41 |
printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2 |
42 |
} |
43 |
|
44 |
### print a warning |
45 |
# arguments: |
46 |
# - warning string [required] |
47 |
# return: n/a |
48 |
function printWarning { |
49 |
local string="WARNING: $1" |
50 |
|
51 |
# if a log file is specified |
52 |
if [ ! -z $LOG_FILE ]; then |
53 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
54 |
fi |
55 |
printf "$(tput setaf 3)>>> $string$(tput sgr 0)" 1>&2 |
56 |
} |
57 |
|
58 |
### print an information text |
59 |
# arguments: |
60 |
# - information string [required] |
61 |
# return: n/a |
62 |
function printInfo { |
63 |
local string="INFO: $1" |
64 |
|
65 |
# if a log file is specified |
66 |
if [ ! -z $LOG_FILE ]; then |
67 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
68 |
fi |
69 |
printf "$(tput setaf 2)>>> $string$(tput sgr 0)" 1>&2 |
70 |
} |
71 |
|
72 |
### print a message to the log file |
73 |
# arguments: |
74 |
# - information string [required] |
75 |
# return: n/a |
76 |
function printLog { |
77 |
local string="LOG: $1" |
78 |
|
79 |
# if a log file is specified |
80 |
if [ ! -z $LOG_FILE ]; then |
81 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
82 |
fi |
83 |
} |
84 |
|
85 |
### exit the script normally |
86 |
# arguments: n/a |
87 |
# return: 0 (success) |
88 |
function quitScript { |
89 |
printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n" |
90 |
printf "\n" |
91 |
printf "######################################################################\n" |
92 |
|
93 |
exit 0 |
94 |
} |
95 |
|
96 |
### read a user input |
97 |
# arguments: |
98 |
# --numchars, -n maximum number of characters to read |
99 |
# --options, -o possible chacters to select (only applies if numchars=1) |
100 |
# --out, -e output variable to store the user input to |
101 |
# return: error code |
102 |
# - 0: no error |
103 |
# - -1: error: invalid arguments |
104 |
function readUserInput { |
105 |
# parse arguments |
106 |
local arguments=$(getopt -l num-chars:,options:,out: -o n:o:e: -- "$@") |
107 |
if [ $? != 0 ]; then |
108 |
printError "could not interprete arguments." |
109 |
return -1 |
110 |
fi |
111 |
eval set -- "$arguments" |
112 |
|
113 |
# evaluate arguments |
114 |
local numchars=1 |
115 |
local options="" |
116 |
local outvar="" |
117 |
while [ true ]; do |
118 |
case "$1" in |
119 |
--num-chars|-n) |
120 |
numchars=$2; shift 2 |
121 |
;; |
122 |
--options|-o) |
123 |
options="$2"; shift 2 |
124 |
;; |
125 |
--out|-e) |
126 |
outvar=$2; shift 2 |
127 |
;; |
128 |
--) # end of options reached |
129 |
shift; break |
130 |
;; |
131 |
*) # sanity check (return error) |
132 |
printError "unexpected argument: $1\n"; return -1 |
133 |
;; |
134 |
esac |
135 |
done |
136 |
|
137 |
# read user input |
138 |
local _userinput="" |
139 |
while [ -z $_userinput ] || ( [ $numchars == 1 ] && [ ! -z "$options" ] && [[ ! $_userinput =~ ^["$options"]$ ]] ); do |
140 |
read -p "your selection: " -n $numchars -e _userinput |
141 |
if [ -z $_userinput ] || ( [ $numchars == 1 ] && [ ! -z "$options" ] && [[ ! $_userinput =~ ^["$options"]$ ]] ); then |
142 |
printWarning "[$_userinput] is no valid action\n" |
143 |
fi |
144 |
done |
145 |
printLog "[$_userinput] has been selected\n" |
146 |
if [ ! -z "$outvar" ]; then |
147 |
eval $outvar="$_userinput" |
148 |
fi |
149 |
|
150 |
return 0 |
151 |
} |
152 |
|
153 |
### set the log file |
154 |
# arguments: |
155 |
# --file, -f name of file [required] |
156 |
# --option, -o option if file already exists [optional] |
157 |
# a: append |
158 |
# r: delete and restart |
159 |
# n: disable log |
160 |
# A: silent append (no message in the log file) |
161 |
# return: error code |
162 |
# - 0: no error |
163 |
# - 1: warning: file already exists |
164 |
# - 2: warning: no file specified |
165 |
# - -1: error: invalid arguments |
166 |
function setLogFile { |
167 |
# parse arguments |
168 |
local arguments=$(getopt -l file:,option: -o f:o: -- "$@") |
169 |
if [ $? != 0 ]; then |
170 |
printError "could not interprete arguments." |
171 |
return -1 |
172 |
fi |
173 |
eval set -- "$arguments" |
174 |
|
175 |
# evaluate arguments |
176 |
local fname="" |
177 |
local option="" |
178 |
while [ true ]; do |
179 |
case "$1" in |
180 |
--file|f) |
181 |
if [ ! -z $2 ]; then |
182 |
fname="$(dirname $2)/$(basename $2)" |
183 |
else |
184 |
fname="" |
185 |
fi |
186 |
shift 2 |
187 |
;; |
188 |
--option|-o) |
189 |
case $2 in |
190 |
A) |
191 |
option="A" |
192 |
;; |
193 |
a) |
194 |
option="a" |
195 |
;; |
196 |
r) |
197 |
option="r" |
198 |
;; |
199 |
n) |
200 |
option="n" |
201 |
;; |
202 |
*) # sanity check (return error) |
203 |
printError "unexpected argument: $1\n"; return -1 |
204 |
;; |
205 |
esac |
206 |
shift 2 |
207 |
;; |
208 |
--) # end of options reached |
209 |
shift; break |
210 |
;; |
211 |
*) # sanity check (return error) |
212 |
printError "unexpected argument: $1\n"; return -1 |
213 |
;; |
214 |
esac |
215 |
done |
216 |
|
217 |
# if no log file was specified |
218 |
if [ -z $fname ]; then |
219 |
printWarning "no log file specified (no log will be genreated)\n" |
220 |
LOG_FILE="" |
221 |
return 2 |
222 |
# if file already exists |
223 |
elif [ -f $fname ]; then |
224 |
# if no option was specified, ask what to do |
225 |
if [ -z $option ]; then |
226 |
printWarning "log file ${fname} already esists\n" |
227 |
printf "Options:\n" |
228 |
printf " [A] - append log\n" |
229 |
printf " [R] - restart log (delete existing file)\n" |
230 |
printf " [N] - no log\n" |
231 |
readUserInput --num-chars=1 --options="AaRrNn" --out=userinput |
232 |
option=${userinput,,} |
233 |
fi |
234 |
|
235 |
# evaluate option |
236 |
case $option in |
237 |
A) |
238 |
LOG_FILE=$(realpath $fname) |
239 |
;; |
240 |
a) |
241 |
printInfo "appending log to existing file\n" |
242 |
LOG_FILE=$(realpath $fname) |
243 |
printf "\n" >> $LOG_FILE |
244 |
printf "######################################################################\n" >> $LOG_FILE |
245 |
printf "\n" >> $LOG_FILE |
246 |
;; |
247 |
r) |
248 |
printInfo "old log file deleted, starting clean\n" |
249 |
LOG_FILE=$(realpath $fname) |
250 |
rm $LOG_FILE |
251 |
;; |
252 |
n) |
253 |
printInfo "no log file will be generated\n" |
254 |
LOG_FILE="" |
255 |
;; |
256 |
*) # sanity check (return error) |
257 |
printError "unexpected argument: $1\n"; return -1 |
258 |
;; |
259 |
esac |
260 |
|
261 |
return 1 |
262 |
else |
263 |
printInfo "log file set to $(realpath $fname)\n" |
264 |
LOG_FILE=$(realpath $fname) |
265 |
return 0 |
266 |
fi |
267 |
} |
268 |
|
269 |
################################################################################ |
270 |
# SPECIFIC FUNCTIONS # |
271 |
################################################################################ |
272 |
|
273 |
### print welcome text |
274 |
# arguments: n/a |
275 |
# return: n/a |
276 |
function printWelcomeText { |
277 |
printf "######################################################################\n" |
278 |
printf "# #\n" |
279 |
printf "# Welcome to the AMiRo-BLT IDE setup! #\n" |
280 |
printf "# #\n" |
281 |
printf "######################################################################\n" |
282 |
printf "# #\n" |
283 |
printf "# Copyright (c) 2016..2017 Thomas Schöpping #\n" |
284 |
printf "# #\n" |
285 |
printf "# This is free software; see the source for copying conditions. #\n" |
286 |
printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR #\n" |
287 |
printf "# A PARTICULAR PURPOSE. The development of this software was #\n" |
288 |
printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction #\n" |
289 |
printf "# Technology. The Excellence Cluster EXC 227 is a grant of the #\n" |
290 |
printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n" |
291 |
printf "# Excellence Initiative. #\n" |
292 |
printf "# #\n" |
293 |
printf "######################################################################\n" |
294 |
} |
295 |
|
296 |
### print help text |
297 |
# arguments: n/a |
298 |
# return: n/a |
299 |
function printHelp { |
300 |
printInfo "printing help text\n" |
301 |
printf "usage: $(basename ${BASH_SOURCE[0]}) [options]\n" |
302 |
printf "options:\n" |
303 |
printf " --help, -h Print this help text.\n" |
304 |
printf " --QtCreator, -c Generate QtCreator project files.\n" |
305 |
printf " --quit, -q Quit the script.\n" |
306 |
printf " --log=<file> Specify a log file.\n" |
307 |
} |
308 |
|
309 |
### enter QtCreator setup |
310 |
# arguments n/a |
311 |
# return: |
312 |
# - 0: no error |
313 |
# - 1: warning: function aborted by user |
314 |
# - -1: error: unexpected user input |
315 |
function qtCreatorSetup { |
316 |
printInfo "entering QtCreator setup\n" |
317 |
printf "\n" |
318 |
if [ -z "$LOG_FILE" ]; then |
319 |
$(realpath $(dirname ${BASH_SOURCE}))/QtCreator/QtCreatorSetup.sh --noinfo |
320 |
else |
321 |
$(realpath $(dirname ${BASH_SOURCE}))/QtCreator/QtCreatorSetup.sh --LOG="$LOG_FILE" --noinfo |
322 |
fi |
323 |
|
324 |
return 0 |
325 |
} |
326 |
|
327 |
################################################################################ |
328 |
# SCRIPT MAIN FUNCTION # |
329 |
################################################################################ |
330 |
|
331 |
### main function of this script |
332 |
# arguments: see help text |
333 |
# return: error code |
334 |
# - 0: no error |
335 |
# - 1: error |
336 |
function main { |
337 |
|
338 |
# parse arguments |
339 |
local arguments=$(getopt -l help,QtCreator,quit,log:,LOG:,noinfo -o hcq -- "$@") |
340 |
if [ $? != 0 ]; then |
341 |
printError "could not interprete arguments." |
342 |
exit 1 |
343 |
fi |
344 |
eval set -- "$arguments" |
345 |
|
346 |
# print welcome/info text if not suppressed |
347 |
if [[ $@ != *"--noinfo"* ]]; then |
348 |
printWelcomeText |
349 |
else |
350 |
printf "######################################################################\n" |
351 |
fi |
352 |
printf "\n" |
353 |
|
354 |
# set log file if specified |
355 |
if [[ $@ == *--log* || $@ == *--LOG* ]]; then |
356 |
# get the parameter (file name) |
357 |
local cmdidx=1 |
358 |
while [ "${!cmdidx}" != "--log" ] && [ "${!cmdidx}" != "--LOG" ]; do |
359 |
cmdidx=$[cmdidx + 1] |
360 |
done |
361 |
local filenameidx=$[cmdidx + 1] |
362 |
# optionally force silent appending |
363 |
if [ "${!cmdidx}" == "--LOG" ]; then |
364 |
setLogFile --file="${!filenameidx}" --option=A |
365 |
else |
366 |
setLogFile --file="${!filenameidx}" |
367 |
printf "\n" |
368 |
fi |
369 |
fi |
370 |
|
371 |
# log script name |
372 |
printLog "this is $(realpath ${BASH_SOURCE[0]})\n" |
373 |
|
374 |
# if --help or -h was specified, print the help text and exit |
375 |
if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then |
376 |
printHelp |
377 |
printf "\n" |
378 |
exitScript |
379 |
fi |
380 |
|
381 |
# handle command line arguments |
382 |
while [ true ]; do |
383 |
case $1 in |
384 |
--help|-h) # already handled; ignore |
385 |
shift 1; |
386 |
;; |
387 |
--QtCreator) |
388 |
qtCreatorSetup; printf "\n"; shift 1 |
389 |
;; |
390 |
--quit|-q) |
391 |
quitScript |
392 |
;; |
393 |
--log|--LOG) # already handled; ignore |
394 |
shift 2 |
395 |
;; |
396 |
--noinfo) # already processed; ignore |
397 |
shift 1 |
398 |
;; |
399 |
--) # end of options reached |
400 |
shift; break |
401 |
;; |
402 |
*) # sanity check (exit with error) |
403 |
printError "unexpected argument: $1\n"; exit -1 |
404 |
;; |
405 |
esac |
406 |
done |
407 |
|
408 |
# interactive menu |
409 |
while [ true ]; do |
410 |
# main menu info prompt and selection |
411 |
printInfo "AMiRo-BLT IDE setup main menu\n" |
412 |
printf "Please select one of the following actions:\n" |
413 |
printf " [C] - enter QtCreator setup\n" |
414 |
printf " [Q] - quit this setup\n" |
415 |
readUserInput --num-chars=1 --options="CcQq" --out=userinput |
416 |
printf "\n" |
417 |
|
418 |
# evaluate user selection |
419 |
case $userinput in |
420 |
C|c) |
421 |
qtCreatorSetup; printf "\n" |
422 |
;; |
423 |
Q|q) |
424 |
quitScript |
425 |
;; |
426 |
*) # sanity check (exit with error) |
427 |
printError "unexpected argument: $userinput\n"; exit -1 |
428 |
;; |
429 |
esac |
430 |
done |
431 |
|
432 |
exit 0 |
433 |
} |
434 |
|
435 |
################################################################################ |
436 |
# SCRIPT ENTRY POINT # |
437 |
################################################################################ |
438 |
|
439 |
main "$@" |