Statistics
| Branch: | Tag: | Revision:

amiro-os / kernel / kernelsetup.sh @ 96621a83

History | View | Annotate | Download (30.1 KB)

1 e545e620 Thomas Schöpping
################################################################################
2
# AMiRo-OS is an operating system designed for the Autonomous Mini Robot       #
3
# (AMiRo) platform.                                                            #
4 96621a83 Thomas Schöpping
# Copyright (C) 2016..2020  Thomas Schöpping et al.                            #
5 e545e620 Thomas Schöpping
#                                                                              #
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 57cbd1cd Thomas Schöpping
### check whether commands are available #######################################
267
# Checks whether the specified commands are available and can be executed.
268
#
269 872e1ebd Thomas Schöpping
# usage:      checkCommands [<command> <command> ...]
270 57cbd1cd Thomas Schöpping
# 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 e545e620 Thomas Schöpping
################################################################################
301
# SPECIFIC FUNCTIONS                                                           #
302
################################################################################
303
304
### print welcome text #########################################################
305
# Prints a welcome message to standard out.
306
#
307
# usage:      printWelcomeText
308
# arguments:  n/a
309
# return:     n/a
310
#
311
function printWelcomeText {
312
  printf "######################################################################\n"
313
  printf "#                                                                    #\n"
314
  printf "#              Welcome to the ChibiOS submodule setup!               #\n"
315
  printf "#                                                                    #\n"
316
  printf "######################################################################\n"
317
  printf "#                                                                    #\n"
318 96621a83 Thomas Schöpping
  printf "# Copyright (c) 2016..2020  Thomas Schöpping                         #\n"
319 e545e620 Thomas Schöpping
  printf "#                                                                    #\n"
320
  printf "# This is free software; see the source for copying conditions.      #\n"
321
  printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n"
322
  printf "# A PARTICULAR PURPOSE. The development of this software was         #\n"
323
  printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction  #\n"
324
  printf "# Technology. The Excellence Cluster EXC 227 is a grant of the       #\n"
325
  printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n"
326
  printf "# Excellence Initiative.                                             #\n"
327
  printf "#                                                                    #\n"
328
  printf "######################################################################\n"
329
}
330
331
### print help #################################################################
332
# Prints a help text to standard out.
333
#
334
# usage:      printHelp
335
# arguments:  n/a
336
# return:     n/a
337
#
338
function printHelp {
339
  printInfo "printing help text\n"
340
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [-i|--init] [-p|--patch] [-d|--documentation=<option>] [-w|--wipe] [-q|--quit] [--log=<file>]\n"
341
  printf "options:  -h, --help\n"
342
  printf "              Print this help text.\n"
343
  printf "          -i, --init\n"
344
  printf "              Initialize ChibiOS submodule.\n"
345
  printf "          -p, --patch,\n"
346 daafd0b0 Thomas Schöpping
  printf "              Apply patches to ChibiOS.\n"
347 e545e620 Thomas Schöpping
  printf "          -d, --documentation <option>\n"
348
  printf "              Possible options are: 'g' and 'o' (can be combined).\n"
349
  printf "              - g: Generate HTML documentation of ChibiOS.\n"
350
  printf "              - o: Open HTML documentation of ChibiOS.\n"
351
  printf "          -w, --wipe\n"
352
  printf "              Wipe ChibiOS submodule.\n"
353
  printf "          -q, --quit\n"
354
  printf "              Quit the script.\n"
355
  printf "          --log=<file>\n"
356
  printf "              Specify a log file.\n"
357
}
358
359
### initialize ChibiOS submodule ###############################################
360
# Initializes the ChibiOS Git submodule.
361
#
362
# usage:      initChibiOS
363
# arguments:  n/a
364
# return:     0
365
#                 No error or warning occurred.
366
#             1
367
#                 Warning: Aborted by user.
368
#             -1
369
#                 Error: Unexpected user input.
370 57cbd1cd Thomas Schöpping
#             -1
371
#                 Error: Missing dependency.
372 e545e620 Thomas Schöpping
#
373
function initChibiOS {
374
  printInfo "initializing ChibiOS submodule...\n"
375
  local userdir=$(pwd)
376
  local kerneldir=$(dirname $(realpath ${BASH_SOURCE[0]}))
377
  local chibiosdir=${kerneldir}/ChibiOS
378
379
  # if the kernel folder is not empty
380
  if [ ! -z "$(ls -A $chibiosdir)" ]; then
381
    printWarning "$chibiosdir is not empty. Delete and reinitialize? [y/n]\n"
382
    local userinput=""
383
    readUserInput "YyNn" userinput
384
    case "$userinput" in
385
      Y|y)
386
        wipeChibiOS
387
        ;;
388
      N|n)
389
        printWarning "ChibiOS initialization aborted by user\n"
390
        return 1
391
        ;;
392
      *) # sanity check (return error)
393
        printError "unexpected input: $userinput\n"; return -1;;
394
    esac
395
  fi
396
397 57cbd1cd Thomas Schöpping
  # check dependencies
398
  checkCommands git
399
  if [ $? -ne 0 ]; then
400
    printError "Missing dependencies detected.\n"
401
    return -2
402
  fi
403
404 e545e620 Thomas Schöpping
  # initialize submodule to default branch
405
  cd $kerneldir
406
  git submodule update --init $chibiosdir 2>&1 | tee -a $LOG_FILE
407
  while [ ${PIPESTATUS[0]} -ne 0 ]; do
408
    printWarning "initialitaion failed. Retry? [y/n]\n"
409
    local userinput=""
410
    readUserInput "YyNn" userinput
411
    case "$userinput" in
412
      Y|y)
413
        git submodule update --init $chibiosdir 2>&1 | tee -a $LOG_FILE;;
414
      N|n)
415
        printWarning "ChibiOS initialization aborted by user\n"
416
        cd $userdir
417
        return 1
418
        ;;
419
      *) # sanity check (return error)
420
        printError "unexpected input: $userinput\n"; return -1;;
421
    esac
422
  done
423
  cd $userdir
424
425
  return 0
426
}
427
428
### patch ChibiOS ##############################################################
429
# Applies patches to ChibiOS submodule.
430
#
431
# usage:      patchChibiOS
432
# arguments:  n/a
433
# return:     0
434
#                 No error or warning occurred.
435
#             1
436
#                 Warning: ChibiOS not initialized yet.
437
#             2
438
#                 Warning: Setup aborted by user.
439
#             -1
440
#                 Error: Unexpected user input.
441
#
442
function patchChibiOS {
443
  printInfo "applying patches to ChibiOS\n"
444
  local userdir=$(pwd)
445
  local kerneldir=$(dirname $(realpath ${BASH_SOURCE[0]}))
446
  local chibiosdir=${kerneldir}/ChibiOS
447
  local git_branch_patched="AMiRo-OS"
448
449 57cbd1cd Thomas Schöpping
  # check dependencies
450
  checkCommands git
451
  if [ $? -ne 0 ]; then
452
    printError "Missing dependencies detected.\n"
453
    return -2
454
  fi
455
456 e545e620 Thomas Schöpping
  # if the ChibiOS folder is empty
457
  if [ -z "$(ls -A $chibiosdir)" ]; then
458
    printWarning "$chibiosdir is empty. Please initialize first.\n"
459
    return 1
460
  else 
461
    # get some information from Git
462
    cd $chibiosdir
463
    local git_branch_current=$(git rev-parse --abbrev-ref HEAD)
464
    local git_branches=$(git for-each-ref --format="%(refname)")
465
    local git_dirtyfiles=($(git ls-files -dmo --exclude-standard --exclude=/doc))
466
    cd $userdir
467
468
    local issues=0
469
    # if the current branch is already $git_branch_patched
470
    if [ "$git_branch_current" = "$git_branch_patched" ]; then
471
      issues=$((issues + 1))
472
      printWarning "current branch is already $git_branch_patched\n"
473
    fi
474
    # if the current branch is bot $git_branch_patched, but another branch $git_branch_patched already exists
475
    if [ "$git_branch_current" != "$git_branch_patched" ] && [[ "$git_branches" = *"$git_branch_patched"* ]]; then
476
      issues=$((issues + 1))
477
      printWarning "another branch $git_branch_patched already exists\n"
478
    fi
479
    # if there are untracked, modified, or deleted files
480
    if [ ${#git_dirtyfiles[@]} != 0 ]; then
481
      issues=$((issues + 1))
482
      printWarning "there are ${#git_dirtyfiles[@]} untracked, modified, or deleted files\n"
483
    fi
484
    if [ $issues -gt 0 ]; then
485
      local userinput=""
486
      printWarning "$issues issues detected. Do you want to continue? [y/n]\n"
487
      readUserInput "YyNn" userinput
488
      case "$userinput" in
489
        Y|y)
490
          ;;
491
        N|n)
492
          printfWarning "ChibiOS patching aborted by user\n"
493
          return 2
494
          ;;
495
        *) # sanity check (return error)
496
          printError "unexpected input: $userinput\n"; return -1;;
497
      esac
498
    fi
499
500
    # create a new branch and apply the patches
501
    local patches=${kerneldir}/patches/*.patch
502
    cd $chibiosdir
503
    git checkout -b "$git_branch_patched" 2>&1 | tee -a $LOG_FILE
504
    for patch in $patches; do
505 82b6a25c Thomas Schöpping
      printInfo "applying $(basename ${patch})...\n"
506 75d6970a Thomas Schöpping
      git apply --whitespace=nowarn --ignore-space-change --ignore-whitespace < $patch 2>&1 | tee -a $LOG_FILE
507 e545e620 Thomas Schöpping
#      # These lines are disabled for safety reasons:
508
#      #   Filed commits are detected as valid changes by the super-project.
509 82b6a25c Thomas Schöpping
#      #   This would lead to errorneous updates of the super-project, so to point to one of those (local) commits.
510 e545e620 Thomas Schöpping
#      #   Since these commits are not pushed upstream, initialization of the super-project will therefore fail, because
511 82b6a25c Thomas Schöpping
#      #   the referenced hashes (after patching) do not exist in a clean copy of this sub-project.
512 e545e620 Thomas Schöpping
#      git add $(git ls-files -dmo --exclude-standard --exclude=/doc) $(git diff --name-only) 2>&1 | tee -a $LOG_FILE
513
#      git commit --message="$patch applied" 2>&1 | tee -a $LOG_FILE
514
    done
515
    cd $userdir
516
517
    return 0
518
  fi
519
}
520
521
### ChibiOS dcoumentation setup ################################################
522
#
523
# usage:      documentation [<option>]
524
# arguments:  <option>
525
#                 Can be either 'g' or 'o' to generate or open HTML documentation respectively.
526
# return:     0
527
#                 No error or warning occurred.
528
#             1
529
#                 Warning: Kernel not nitialized yet.
530
#             2
531
#                 Warning: Setup aborted by user.
532
#             3
533
#                 Warning: Issues occurred.
534
#             -1
535
#                 Error: Unexpected user input.
536 44a8dba7 Thomas Schöpping
#             -2
537
#                 Error: Missing dependencies.
538 e545e620 Thomas Schöpping
#
539
function documentation {
540
  local userdir=$(pwd)
541
  local kerneldir=$(dirname $(realpath ${BASH_SOURCE[0]}))
542
  local chibiosdir=${kerneldir}/ChibiOS
543
544
  # if the ChibiOS folder is empty
545
  if [ -z "$(ls -A $chibiosdir)" ]; then
546
    printWarning "$chibiosdir is empty. Please initialize first.\n"
547
    return 1
548
  else
549
    local option="";
550
    # if no argument was specified, ask what to do
551
    if [ $# -eq 0 ]; then
552
      printInfo "ChibiOS documentation setup\n"
553
      printf "Please select one of the following actions:\n"
554
      printf "  [G] - generate HTML documentation\n"
555
      printf "  [O] - open HTML documentation\n"
556
      printf "  [A] - abort this setup\n"
557
      local userinput
558
      readUserInput "GgOoAa" userinput
559
      option=${userinput,,}
560
      if [ $option = "a" ]; then
561
        printInfo "ChibiOS documentation setup aborted by user\n"
562
        return 2
563
      fi
564
    else
565
      option="$1"
566
    fi
567
568
    local issues=0
569
    case "$option" in
570
      # generate HTML documentation
571
      g)
572 44a8dba7 Thomas Schöpping
        # check dependencies
573
        checkCommands doxygen
574
        if [ $? -ne 0 ]; then
575
          printError "Missing dependencies detected.\n"
576
          return -2
577
        fi
578
579 e545e620 Thomas Schöpping
        # ChibiOS/HAL: check if required files exis
580
        if [ -f ${chibiosdir}/doc/hal/makehtml.sh ]; then
581
          printInfo "generating ChibiOS/HAL documentation...\n"
582
          cd ${chibiosdir}/doc/hal
583
          ${chibiosdir}/doc/hal/makehtml.sh 2>&1 | tee -a $LOG_FILE
584
          cd $userdir
585
          printInfo "access ChibiOS/HAL documentation via ${chibiosdir}doc/hal/html/index.html\n"
586
        else
587
          issues=$((issues + 1))
588
          printError "could not generate ChibiOS/HAL documentation\n"
589
        fi
590
        # ChibiOS/RT: check if required files exis
591
        if [ -f ${chibiosdir}/doc/rt/makehtml.sh ]; then
592
          printInfo "generating ChibiOS/RT documentation...\n"
593
          cd ${chibiosdir}/doc/rt
594
          ${chibiosdir}/doc/rt/makehtml.sh 2>&1 | tee -a $LOG_FILE
595
          cd $userdir
596
          printInfo "access ChibiOS/RT documentation via ${chibiosdir}doc/rt/html/index.html\n"
597
        else
598
          issues=$((issues + 1))
599
          printError "could not generate ChibiOS/RT documentation\n"
600
        fi
601
        # ChibiOS/NIL: check if required files exis
602
        if [ -f ${chibiosdir}/doc/nil/makehtml.sh ]; then
603
          printInfo "generating ChibiOS/NIL documentation...\n"
604
          cd ${chibiosdir}/doc/nil
605
          ${chibiosdir}/doc/nil/makehtml.sh 2>&1 | tee -a $LOG_FILE
606
          cd $userdir
607
          printInfo "access ChibiOS/NIL documentation via ${chibiosdir}edoc/nil/html/index.html\n"
608
        else
609
          issues=$((issues + 1))
610
          printError "could not generate ChibiOS/NIL documentation\n"
611
        fi
612
        ;;
613
614
      # open HTML documentation
615
      o)
616
        # ChibiOS/HAL: check if required files exis
617
        if [ -f ${chibiosdir}/doc/hal/html/index.html ]; then
618
          printInfo "open ChibiOS/HAL documentation\n"
619
          xdg-open ${chibiosdir}/doc/hal/html/index.html &> /dev/null &
620
        else
621
          issues=$((issues + 1))
622
          printError "could not open ChibiOS/HAL documentation\n"
623
        fi
624
        # ChibiOS/RT: check if required files exis
625
        if [ -f ${chibiosdir}/doc/rt/html/index.html ]; then
626
          printInfo "open ChibiOS/RT documentation\n"
627
          xdg-open ${chibiosdir}/doc/rt/html/index.html &> /dev/null &
628
        else
629
          issues=$((issues + 1))
630
          printError "could not open ChibiOS/RT documentation\n"
631
        fi
632
        # ChibiOS/NIL: check if required files exis
633
        if [ -f ${chibiosdir}/doc/nil/html/index.html ]; then
634
          printInfo "open ChibiOS/NIL documentation\n"
635
          xdg-open ${chibiosdir}/doc/nil/html/index.html &> /dev/null &
636
        else
637
          issues=$((issues + 1))
638
          printError "could not open ChibiOS/NIL documentation\n"
639
        fi
640
        ;;
641
642
      *) # sanity check (return error)
643
        printError "unexpected input: $userinput\n"; return -1;;
644
    esac
645
646
    if [ $issues -gt 0 ]; then
647
      return 3
648
    else
649
      return 0
650
    fi
651
  fi
652
}
653
654
### reset ChibiOS submodule and wipe directory #################################
655
# Resets the ChibiOS Git submodule and wipes the directory.
656
#
657
# usage:      wipeChibiOS
658
# arguments:  n/a
659
# return:     0
660
#                 No error or warning occurred.
661
#             1
662
#                 Warning: Submodule directory is already empty.
663
#             2
664
#                 Warning: Wiping aborted by user.
665
#             -1
666
#                 Error: Unexpected user input.
667
#
668
function wipeChibiOS {
669
  printInfo "reset and wipe Git submodule $kerneldir\n"
670
  local userdir=$(pwd)
671
  local kerneldir=$(dirname $(realpath ${BASH_SOURCE[0]}))
672
  local chibiosdir=${kerneldir}/ChibiOS
673
  local git_branch_patched="AMiRo-OS"
674
675 57cbd1cd Thomas Schöpping
  # check dependencies
676
  checkCommands git
677
  if [ $? -ne 0 ]; then
678
    printError "Missing dependencies detected.\n"
679
    return -2
680
  fi
681
682 e545e620 Thomas Schöpping
  # if the ChibiOS folder is empty
683
  if [ -z "$(ls -A $chibiosdir)" ]; then
684
    printInfo "$chibiosdir is alread empty\n"
685
    return 1
686
  else 
687
    # get some information from Git
688
    cd $kerneldir
689
    local git_basehash=($(git ls-tree -d HEAD $kerneldir)); git_basehash=${git_basehash[2]}
690
    cd $chibiosdir
691
    local git_branch_current=$(git rev-parse --abbrev-ref HEAD)
692
    local git_difftobase="$(git diff ${git_basehash}..HEAD)"
693
    local git_commits=$(git log --format=oneline ${git_basehash}..HEAD)
694
    local git_dirtyfiles=($(git ls-files -dmo --exclude-standard --exclude=/doc))
695
    cd $userdir
696
    local issues=0
697
    # if the HEAD is neither detached, nor is the current branch $git_branch_patched
698
    if [ "$git_branch_current" != "HEAD" ] && [ "$git_branch_current" != "$git_branch_patched" ]; then
699
      issues=$((issues + 1))
700
      printWarning "modifications to ChibiOS Git submodule detected\n"
701
    fi
702
    # if HEAD is ahead of submodule base commit but with more than just applied patches
703
    if [ -n "$git_difftobase" ] && [ -n "$(echo $git_commits | grep -Ev '\.patch applied$')" ]; then
704
      issues=$((issues + 1))
705
      printWarning "HEAD is ahead of submodule base by unexpected commits\n"
706
    fi
707
    # if there are untracked, modified, or deleted files
708
    if [ ${#git_dirtyfiles[@]} != 0 ]; then
709
      issues=$((issues + 1))
710
      printWarning "there are ${#git_dirtyfiles[@]} untracked, modified, or deleted files\n"
711
    fi
712
    if [ $issues -gt 0 ]; then
713
      local userinput=""
714
      printWarning "$issues issues detected. Do you want to continue? [y/n]\n"
715
      readUserInput "YyNn" userinput
716
      case "$userinput" in
717
        Y|y)
718
          ;;
719
        N|n)
720
          printfWarning "wiping ChibiOS Git submodule aborted by user\n"
721
          return 2
722
          ;;
723
        *) # sanity check (return error)
724
          printError "unexpected input: $userinput\n"; return -1;;
725
      esac
726
    fi
727
728
    # checkout base commit and delete all local branches
729
    cd $kerneldir
730
    git submodule update --force --checkout $kerneldir | tee -a $LOG_FILE
731
    cd $chibiosdir
732
    local git_branches=($(git for-each-ref --format="%(refname)"))
733
    for branch in $git_branches; do
734
      if [[ $branch = *"heads/"* ]]; then
735
        git branch -D ${branch##*/} | tee -a $LOG_FILE
736
      fi
737
    done
738
    cd $userdir
739
740
    # deinitialize ChibiOS submodule and delete any remaining files
741
    cd $kerneldir
742
    git submodule deinit -f $chibiosdir 2>&1 | tee -a $LOG_FILE
743
    rm -rf $chibiosdir/*
744
    cd $userdir
745
746
    return 0
747
  fi
748
}
749
750
### main function of this script ###############################################
751
# The kernel setup provides comfortable initialization, patching, documentation
752
# generation and cleanup for ChibiOS.
753
#
754
# usage:      see function printHelp
755
# arguments:  see function printHelp
756
# return:     0
757
#                 No error or warning occurred.
758
#
759
function main {
760
  # print welcome/info text if not suppressed
761
  if [[ $@ != *"--noinfo"* ]]; then
762
    printWelcomeText
763
  else
764
    printf "######################################################################\n"
765
  fi
766
  printf "\n"
767
768
  # if --help or -h was specified, print the help text and exit
769
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
770
    printHelp
771
    printf "\n"
772
    quitScript
773
  fi
774
775
  # set log file if specified
776
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
777
    # get the parameter (file name)
778
    local cmdidx=1
779
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
780
      cmdidx=$[cmdidx + 1]
781
    done
782
    local cmd="${!cmdidx}"
783
    local logfile=""
784
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
785
      logfile=${cmd#*=}
786
    else
787
      local filenameidx=$((cmdidx + 1))
788
      logfile="${!filenameidx}"
789
    fi
790
    # optionally force silent appending
791
    if [[ "$cmd" = "--LOG"* ]]; then
792
      setLogFile --option=c --quiet "$logfile" LOG_FILE
793
    else
794
      setLogFile "$logfile" LOG_FILE
795
      printf "\n"
796
    fi
797
  fi
798
  # log script name
799
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
800
801
  # parse arguments
802
  local otherargs=()
803
  while [ $# -gt 0 ]; do
804
    if ( parseIsOption $1 ); then
805
      case "$1" in
806
        -h|--help) # already handled; ignore
807
          shift 1;;
808
        -i|--init)
809
           initChibiOS; printf "\n"; shift 1;;
810
        -p|--patch)
811
           patchChibiOS; printf "\n"; shift 1;;
812
        -d=*|--documentation=*)
813
           documentation "${1#*=}"; printf "\n"; shift 1;;
814
        -d|--documentation)
815
           if ( ! parseIsOption $2 ); then
816
             documentation "$2"; printf "\n"; shift 2
817
           else
818
             documentation; printf "\n"; shift 1
819
           fi;;
820
        -w|--wipe)
821
          wipeChibiOS; printf "\n"; shift 1;;
822
        -q|--quit)
823
          quitScript; shift 1;;
824
        --log=*|--LOG=*) # already handled; ignore
825
          shift 1;;
826
        --log|--LOG) # already handled; ignore
827
          shift 2;;
828
        --noinfo) # already handled; ignore
829
          shift 1;;
830
        *)
831
          printError "invalid option: $1\n"; shift 1;;
832
      esac
833
    else
834
      otherargs+=("$1")
835
      shift 1
836
    fi
837
  done
838
839
  # interactive menu
840
  while ( true ); do
841
    # main menu info prompt and selection
842
    printInfo "ChibiOS kernel setup main menu\n"
843
    printf "Please select one of the following actions:\n"
844
    printf "  [I] - initialize ChibiOS submodule\n"
845
    printf "  [P] - apply patches to ChibiOS\n"
846
    printf "  [D] - generate or open HTML documentation\n"
847
    printf "  [W] - wipe ChibiOS submodule\n"
848
    printf "  [Q] - quit this setup\n"
849
    local userinput=""
850
    readUserInput "IiPpDdWwQq" userinput
851
    printf "\n"
852
853
    # evaluate user selection
854
    case "$userinput" in
855
      I|i)
856
        initChibiOS; printf "\n";;
857
      P|p)
858
        patchChibiOS; printf "\n";;
859
      D|d)
860
        documentation; printf "\n";;
861
      W|w)
862
        wipeChibiOS; printf "\n";;
863
      Q|q)
864
        quitScript;;
865
      *) # sanity check (exit with error)
866
        printError "unexpected argument: $userinput\n";;
867
    esac
868
  done
869
870
  exit 0
871
}
872
873
################################################################################
874
# SCRIPT ENTRY POINT                                                           #
875
################################################################################
876
877
main "$@"