Statistics
| Branch: | Tag: | Revision:

amiro-os / setup.sh @ 9ff01927

History | View | Annotate | Download (21 KB)

1 e545e620 Thomas Schöpping
################################################################################
2
# AMiRo-OS is an operating system designed for the Autonomous Mini Robot       #
3
# (AMiRo) platform.                                                            #
4
# Copyright (C) 2016..2018  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 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
################################################################################
267
# SPECIFIC FUNCTIONS                                                           #
268
################################################################################
269
270
### print welcome text #########################################################
271
# Prints a welcome message to standard out.
272
#
273
# usage:      printWelcomeText
274
# arguments:  n/a
275
# return:     n/a
276
#
277
function printWelcomeText {
278
  printf "######################################################################\n"
279
  printf "#                                                                    #\n"
280
  printf "#                   Welcome to the AMiRo-OS setup!                   #\n"
281
  printf "#                                                                    #\n"
282
  printf "######################################################################\n"
283
  printf "#                                                                    #\n"
284
  printf "# Copyright (c) 2016..2018  Thomas Schöpping                         #\n"
285
  printf "#                                                                    #\n"
286
  printf "# This is free software; see the source for copying conditions.      #\n"
287
  printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n"
288
  printf "# A PARTICULAR PURPOSE. The development of this software was         #\n"
289
  printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction  #\n"
290
  printf "# Technology. The Excellence Cluster EXC 227 is a grant of the       #\n"
291
  printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n"
292
  printf "# Excellence Initiative.                                             #\n"
293
  printf "#                                                                    #\n"
294
  printf "######################################################################\n"
295
}
296
297
### print help #################################################################
298
# Prints a help text to standard out.
299
#
300
# usage:      printHelp
301
# arguments:  n/a
302
# return:     n/a
303
#
304
function printHelp {
305
  printInfo "printing help text\n"
306
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [-i|--init] [-k|--kernel] [-b|--bootloader] [-p|--periphery-LLD] [-c|--compiler] [--IDE] [-q|--quit] [--log=<file>]\n"
307
  printf "\n"
308
  printf "options:  -h, --help\n"
309
  printf "              Print this help text.\n"
310
  printf "          -i, --init\n"
311
  printf "              Run project initalization.\n"
312
  printf "          -k, --kernel\n"
313
  printf "              Enter kernel setup.\n"
314
  printf "          -b, --bootloader\n"
315
  printf "              Enter bootloader setup.\n"
316
  printf "          -p, --periphery-LLD\n"
317
  printf "              Enter periphery-LLD setup.\n"
318
  printf "          -c, --compiler\n"
319
  printf "              Enter compiler setup.\n"
320
  printf "          --IDE\n"
321
  printf "              Enter IDE setup.\n"
322
  printf "          -q, --quit\n"
323
  printf "              Quit the script.\n"
324
  printf "          --log=<file>\n"
325
  printf "              Specify a log file.\n"
326
}
327
328
### initialize whole project ###################################################
329
# Initializes the whole project, including fetching all submodules and so on.
330
#
331
# usage:      projectInitialization
332
# arguments:  n/a
333
# return:     n/a
334
#
335
function projectInitialization {
336
  printInfo "initializing project\n"
337
  printf "\n"
338
  if [ -z "$LOG_FILE" ]; then
339
    printInfo "initializing kernel submodule...\n"
340
    $(dirname $(realpath ${BASH_SOURCE[0]}))/kernel/kernelsetup.sh --init --quit --noinfo
341
    printf "\n"
342
    printInfo "initializing ChibiOS...\n"
343
    $(dirname $(realpath ${BASH_SOURCE[0]}))/kernel/kernelsetup.sh --patch --documentation=g --quit --noinfo
344
    printf "\n"
345
346
    printInfo "initializing bootloader submodule...\n"
347
    $(dirname $(realpath ${BASH_SOURCE[0]}))/bootloader/bootloadersetup.sh --init --quit --noinfo
348
    printf "\n"
349
    printInfo "initializing AMiRo-BLT...\n"
350
    $(dirname $(realpath ${BASH_SOURCE[0]}))/bootloader/AMiRo-BLT/setup.sh --stm32flash --SerialBoot --quit --noinfo
351
    printf "\n"
352
353
    printInfo "initializing periphery-LLD (AMiRo-LLD)...\n"
354
    $(dirname $(realpath ${BASH_SOURCE[0]}))/periphery-lld/peripherylldsetup.sh --init --quit --noinfo
355
    printf "\n"
356
  else
357
    printInfo "initializing kernel submodule...\n"
358
    $(dirname $(realpath ${BASH_SOURCE[0]}))/kernel/kernelsetup.sh --init --quit --LOG="$LOG_FILE" --noinfo
359
    printf "\n"
360
    printInfo "initializing ChibiOS...\n"
361
    $(dirname $(realpath ${BASH_SOURCE[0]}))/kernel/kernelsetup.sh --patch --documentation=g --quit --LOG="$LOG_FILE" --noinfo
362
    printf "\n"
363
364
    printInfo "initializing bootloader submodule...\n"
365
    $(dirname $(realpath ${BASH_SOURCE[0]}))/bootloader/bootloadersetup.sh --init --quit --LOG="$LOG_FILE" --noinfo
366
    printf "\n"
367
    printInfo "initializing AMiRo-BLT...\n"
368
    $(dirname $(realpath ${BASH_SOURCE[0]}))/bootloader/AMiRo-BLT/setup.sh --stm32flash --SerialBoot --quit --LOG="$LOG_FILE" --noinfo
369
    printf "\n"
370
371
    printInfo "initializing periphery-LLD (AMiRo-LLD)...\n"
372
    $(dirname $(realpath ${BASH_SOURCE[0]}))/periphery-lld/peripherylldsetup.sh --init --quit --LOG="$LOG_FILE" --noinfo
373
    printf "\n"
374
  fi
375
  printInfo "initialization complete\n"
376
}
377
378
### enter kernel setup #########################################################
379
# Enter the kernel setup.
380
#
381
# usage:      kernelSetup
382
# arguments:  n/a
383
# return:     n/a
384
#
385
function kernelSetup {
386
  printInfo "entering kernel setup\n"
387
  printf "\n"
388
  if [ -z $LOG_FILE ]; then
389
    $(dirname $(realpath ${BASH_SOURCE[0]}))/kernel/kernelsetup.sh --noinfo
390
  else
391
    $(dirname $(realpath ${BASH_SOURCE[0]}))/kernel/kernelsetup.sh --LOG="$LOG_FILE" --noinfo
392
  fi
393
}
394
395
### enter bootloader setup #####################################################
396
# Enter bootloader setup.
397
#
398
# usage:      bootloaderSetup
399
# arguments:  n/a
400
# return:     n/a
401
#
402
function bootloaderSetup {
403
  printInfo "entering bootloader setup\n"
404
  printf "\n"
405
  if [ -z $LOG_FILE ]; then
406
    $(dirname $(realpath ${BASH_SOURCE[0]}))/bootloader/bootloadersetup.sh --noinfo
407
  else
408
    $(dirname $(realpath ${BASH_SOURCE[0]}))/bootloader/bootloadersetup.sh --LOG="$LOG_FILE" --noinfo
409
  fi
410
}
411
412
### enter periphery LLD setup ##################################################
413
# Enter the Periphery-LLD setup.
414
#
415
# usage:      peripheryLldSetup
416
# arguments:  n/a
417
# return:     n/a
418
#
419
function peripheryLldSetup {
420
  printInfo "entering periphery-LLD setup\n"
421
  printf "\n"
422
  if [ -z $LOG_FILE ]; then
423
    $(dirname $(realpath ${BASH_SOURCE[0]}))/periphery-lld/peripherylldsetup.sh --noinfo
424
  else
425
    $(dirname $(realpath ${BASH_SOURCE[0]}))/periphery-lld/peripherylldsetup.sh --LOG="$LOG_FILE" --noinfo
426
  fi
427
}
428
429
### enter compiler setup #######################################################
430
# Enter the compiler setup of tzhe AMiRo-BLT submodule.
431
#
432
# usage:      compilerSetup
433
# arguments:  n/a
434
# return:     0
435
#                 No error or warning occurred.
436
#             1
437
#                 Warning: AMiRo-BLT submodule not nitialized yet-
438
#
439
function compilerSetup {
440
  # check if the AMiRo-BLT submodule is initialized and the script file exists
441
  local amirobltdir=$(dirname $(realpath ${BASH_SOURCE[0]}))/bootloader/AMiRo-BLT
442
  local compilerscriptfile=${amirobltdir}/tools/compiler/compilersetup.sh
443
  if [ -z "$(ls -A $amirobltdir)" ] || [ ! -f $compilerscriptfile ]; then
444
    printWarning "Please initialize AMiRo-BLT submodule first.\n"
445
    return 1
446
  else
447
    printInfo "entering compiler setup\n"
448
    if [ -z "$LOG_FILE" ]; then
449
      $compilerscriptfile --noinfo
450
    else
451
      $compilerscriptfile --LOG="$LOG_FILE" --noinfo
452
    fi
453
    return 0
454
  fi
455
}
456
457
### enter IDE setup ############################################################
458
# Enter IDE setup.
459
#
460
# usage:      ideSetup
461
# arguments:  n/a
462
# return:     n/a
463
#
464
function ideSetup {
465
  printInfo "entering IDE setup\n"
466
  printf "\n"
467
  if [ -z $LOG_FILE ]; then
468
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/ide/idesetup.sh --noinfo
469
  else
470
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/ide/idesetup.sh --LOG="$LOG_FILE" --noinfo
471
  fi
472
}
473
474
### main function of this script ###############################################
475
# Provides functions for project initialization, configuration of IDE and
476
# compiler setup, as well as entry points to the several Git submodules.
477
#
478
# usage:      see function printHelp
479
# arguments:  see function printHelp
480
# return:     0
481
#                 No error or warning occurred.
482
#
483
function main {
484
  # print welcome/info text if not suppressed
485
  if [[ $@ != *"--noinfo"* ]]; then
486
    printWelcomeText
487
  else
488
    printf "######################################################################\n"
489
  fi
490
  printf "\n"
491
492
  # if --help or -h was specified, print the help text and exit
493
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
494
    printHelp
495
    printf "\n"
496
    quitScript
497
  fi
498
499
  # set log file if specified
500
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
501
    # get the parameter (file name)
502
    local cmdidx=1
503
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
504
      cmdidx=$[cmdidx + 1]
505
    done
506
    local cmd="${!cmdidx}"
507
    local logfile=""
508
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
509
      logfile=${cmd#*=}
510
    else
511
      local filenameidx=$((cmdidx + 1))
512
      logfile="${!filenameidx}"
513
    fi
514
    # optionally force silent appending
515
    if [[ "$cmd" = "--LOG"* ]]; then
516
      setLogFile --option=c --quiet "$logfile" LOG_FILE
517
    else
518
      setLogFile "$logfile" LOG_FILE
519
      printf "\n"
520
    fi
521
  fi
522
  # log script name
523
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
524
525
  # parse arguments
526
  local otherargs=()
527
  while [ $# -gt 0 ]; do
528
    if ( parseIsOption $1 ); then
529
      case "$1" in
530
        -h|--help) # already handled; ignore
531
          shift 1;;
532
        -i|--init)
533
           projectInitialization; printf "\n"; shift 1;;
534
        -k|--kernel)
535
           kernelSetup; printf "\n"; shift 1;;
536
        -b|--bootloader)
537
           bootloaderSetup; printf "\n"; shift 1;;
538
        -p|--periphery-LLD)
539
           peripheryLldSetup; printf "\n"; shift 1;;
540
        -c|--compiler)
541
           compilerSetup; printf "\n"; shift 1;;
542
        --IDE)
543
           ideSetup; printf "\n"; shift 1;;
544
        -q|--quit)
545
          quitScript; shift 1;;
546
        --log=*|--LOG=*) # already handled; ignore
547
          shift 1;;
548
        --log|--LOG) # already handled; ignore
549
          shift 2;;
550
        --noinfo) # already handled; ignore
551
          shift 1;;
552
        *)
553
          printError "invalid option: $1\n"; shift 1;;
554
      esac
555
    else
556
      otherargs+=("$1")
557
      shift 1
558
    fi
559
  done
560
561
  # interactive menu
562
  while ( true ); do
563
    # main menu info prompt and selection
564
    printInfo "AMiRo-OS main menu\n"
565
    printf "Please select one of the following actions:\n"
566
    printf "  [I] - project initialization\n"
567
    printf "  [K] - enter kernel setup\n"
568
    printf "  [B] - enter bootloaders setup\n"
569
    printf "  [P] - enter periphery-LLD setup\n"
570
    printf "  [C] - enter compiler setup\n"
571
    printf "  [E] - enter IDE project setup\n"
572
    printf "  [Q] - quit this setup\n"
573
    local userinput=""
574
    readUserInput "IiKkBbPpCcEeQq" userinput
575
    printf "\n"
576
577
    # evaluate user selection
578
    case "$userinput" in
579
      I|i)
580
        projectInitialization; printf "\n";;
581
      K|k)
582
        kernelSetup; printf "\n";;
583
      B|b)
584
        bootloaderSetup; printf "\n";;
585
      P|p)
586
        peripheryLldSetup; printf "\n";;
587
      C|c)
588
        compilerSetup; printf "\n";;
589
      E|e)
590
        ideSetup; printf "\n";;
591
      Q|q)
592
        quitScript;;
593
      *) # sanity check (exit with error)
594
        printError "unexpected argument: $userinput\n";;
595
    esac
596
  done
597
598
  exit 0
599
}
600
601
################################################################################
602
# SCRIPT ENTRY POINT                                                           #
603
################################################################################
604
605
main "$@"