Revision 0a42f078 setup.sh

View differences:

setup.sh
27 27
# GENERIC FUNCTIONS                                                            #
28 28
################################################################################
29 29

  
30
### print an error
31
# arguments:
32
#   - error string [required]
33
# return: n/a
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
#
34 39
function printError {
35 40
  local string="ERROR:   $1"
36

  
37 41
  # if a log file is specified
38
  if [ ! -z $LOG_FILE ]; then
42
  if [ -n "$LOG_FILE" ]; then
39 43
    printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE
40 44
  fi
41 45
  printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2
42 46
}
43 47

  
44
### print a warning
45
# arguments:
46
#   - warning string [required]
47
# return: n/a
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
#
48 57
function printWarning {
49 58
  local string="WARNING: $1"
50

  
51 59
  # if a log file is specified
52
  if [ ! -z $LOG_FILE ]; then
60
  if [ -n "$LOG_FILE" ]; then
53 61
    printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE
54 62
  fi
55
  printf "$(tput setaf 3)>>> $string$(tput sgr 0)" 1>&2
63
  printf "$(tput setaf 3)>>> $string$(tput sgr 0)"
56 64
}
57 65

  
58
### print an information text
59
# arguments:
60
#   - information string [required]
61
# return: n/a
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
#
62 75
function printInfo {
63 76
  local string="INFO:    $1"
64

  
65 77
  # if a log file is specified
66
  if [ ! -z $LOG_FILE ]; then
78
  if [ -n "$LOG_FILE" ]; then
67 79
    printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE
68 80
  fi
69
  printf "$(tput setaf 2)>>> $string$(tput sgr 0)" 1>&2
81
  printf "$(tput setaf 2)>>> $string$(tput sgr 0)"
70 82
}
71 83

  
72
### print a message to the log file
73
# arguments:
74
#   - information string [required]
75
# return: n/a
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
#
76 92
function printLog {
77 93
  local string="LOG:     $1"
78

  
79 94
  # if a log file is specified
80
  if [ ! -z $LOG_FILE ]; then
95
  if [ -n "$LOG_FILE" ]; then
81 96
    printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE
82 97
  fi
83 98
}
84 99

  
85
### exit the script normally
86
# arguments: n/a
87
# return: 0 (success)
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
#
88 108
function quitScript {
89
  printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n"
90
  printf "\n"
109
  printLog "exiting $(realpath ${BASH_SOURCE[0]})\n"
91 110
  printf "######################################################################\n"
92

  
93 111
  exit 0
94 112
}
95 113

  
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
114
### read a user input ##########################################################
115
# Reads a single character user input from a set up <options> and stores it in
116
# a given <return> variable.
117
#
118
# usage:      readUserInput <options> <return>
119
# arguments:  <options>
120
#                 String definiing the set of valid characters.
121
#                 If the string is empty, the user can input any character.
122
#             <return>
123
#                 Variable to store the selected character to.
124
# return:     n/a
125
#
104 126
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=0
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

  
127
  local input=""
137 128
  # read user input
138
  local _userinput=""
139
  while [ -z $_userinput ] || ( [ $numchars == 1 ] && [ ! -z "$options" ] && [[ ! $_userinput =~ ^["$options"]$ ]] ); do
140
    if [ $numchars == 0 ]; then
141
      read -p "your selection: " -e _userinput
142
    else
143
      read -p "your selection: " -n $numchars -e _userinput
144
    fi
145
    if [ -z $_userinput ] || ( [ $numchars == 1 ] && [ ! -z "$options" ] && [[ ! $_userinput =~ ^["$options"]$ ]] ); then
146
      printWarning "[$_userinput] is no valid action\n"
129
  while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do
130
    read -p "your selection: " -n 1 -e input
131
    if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then
132
      printWarning "[$input] is no valid action\n"
147 133
    fi
148 134
  done
149
  printLog "[$_userinput] has been selected\n"
150
  if [ ! -z "$outvar" ]; then
151
    eval $outvar="$_userinput"
152
  fi
153

  
154
  return 0
135
  printLog "[$input] has been selected\n"
136
  eval $2="$input"
155 137
}
156 138

  
157
### set the log file
158
# arguments:
159
#   --file, -f    name of file [required]
160
#   --option, -o  option if file already exists [optional]
161
#                 a: append
162
#                 r: delete and restart
163
#                 n: disable log
164
#                 A: silent append (no message in the log file)
165
# return: error code
166
#   -  0: no error
167
#   -  1: warning: file already exists
168
#   -  2: warning: no file specified
169
#   - -1: error: invalid arguments
170
function setLogFile {
171
  # parse arguments
172
  local arguments=$(getopt -l file:,option: -o f:o: -- "$@")
173
  if [ $? != 0 ]; then
174
    printError "could not interprete arguments."
139
### check whether argument is an option ########################################
140
# Checks a <string> whether it is an option.
141
# Options are defined to either start with '--' followed by any string, or
142
# to start with a single '-' followed by a single character, or
143
# to start with a single '-' followed by a single character, a '=' and any string.
144
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--'
145
#
146
# usage:      parseIsOption <string>
147
# arguments:  <string>
148
#                 A string to check whether it is an option.
149
# return:     0
150
#                 <string> is an option.
151
#             -1
152
#                 <string> is not an option.
153
#
154
function parseIsOption {
155
  if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then
156
    return 0
157
  else
175 158
    return -1
176 159
  fi
177
  eval set -- "$arguments"
160
}
178 161

  
179
  # evaluate arguments
180
  local fname=""
162
### set the log file ###########################################################
163
# Sets a specified <infile> as log file and checks whether it already exists.
164
# If so, the log may either be appended to the file, its content can be cleared,
165
# or no log is generated at all.
166
# The resulting path is stored in <outvar>.
167
#
168
# usage:      setLogFile [--option=<option>] [--quiet] <infile> <outvar>
169
# arguments:  --option=<option>
170
#                 Select what to do if <file> already exists.
171
#                 Possible values are 'a', 'r' and 'n'.
172
#                 - a: append
173
#                 - r: delete and restart
174
#                 - n: no log
175
#                 If no option is secified but <file> exists, an interactive selection is provided.
176
#             --quiet
177
#                 Suppress all messages.
178
#             <infile>
179
#                 Path of the wanted log file.
180
#             <outvar>
181
#                 Variable to store the path of the log file to.
182
# return:     0
183
#                 No error or warning occurred.
184
#             -1
185
#                 Error: invalid input
186
#
187
function setLogFile {
188
  local filepath=""
181 189
  local option=""
182
  while [ true ]; do
183
    case "$1" in
184
      --file|f)
185
        if [ ! -z $2 ]; then
186
          fname="$(dirname $2)/$(basename $2)"
187
        else
188
          fname=""
189
        fi
190
        shift 2
191
        ;;
192
      --option|-o)
193
        case $2 in
194
          A)
195
            option="A"
196
            ;;
197
          a)
198
            option="a"
199
            ;;
200
          r)
201
            option="r"
202
            ;;
203
          n)
204
            option="n"
205
            ;;
206
          *) # sanity check (return error)
207
            printError "unexpected argument: $1\n"; return -1
208
            ;;
209
        esac
210
        shift 2
211
        ;;
212
      --) # end of options reached
213
        shift; break
214
        ;;
215
      *) # sanity check (return error)
216
        printError "unexpected argument: $1\n"; return -1
217
        ;;
218
    esac
190
  local quiet=false
191

  
192
  # parse arguments
193
  local otherargs=()
194
  while [ $# -gt 0 ]; do
195
    if ( parseIsOption $1 ); then
196
      case "$1" in
197
        -o=*|--option=*)
198
          option=${1#*=}; shift 1;;
199
        -o*|--option*)
200
          option="$2"; shift 2;;
201
        -q|--quiet)
202
          quiet=true; shift 1;;
203
        *)
204
          printError "invalid option: $1\n"; shift 1;;
205
      esac
206
    else
207
      otherargs+=("$1")
208
      shift 1
209
    fi
219 210
  done
211
  filepath=$(realpath ${otherargs[0]})
220 212

  
221
  # if no log file was specified
222
  if [ -z $fname ]; then
223
    printWarning "no log file specified (no log will be genreated)\n"
224
    LOG_FILE=""
225
    return 2
226 213
  # if file already exists
227
  elif [ -f $fname ]; then
214
  if [ -e $filepath ]; then
228 215
    # if no option was specified, ask what to do
229
    if [ -z $option ]; then
230
      printWarning "log file ${fname} already esists\n"
231
      printf "Options:\n"
216
    if [ -z "$option" ]; then
217
      printWarning "log file $filepath already esists\n"
218
      local userinput=""
219
      printf "Select what to do:\n"
232 220
      printf "  [A] - append log\n"
233 221
      printf "  [R] - restart log (delete existing file)\n"
234 222
      printf "  [N] - no log\n"
235
      readUserInput --num-chars=1 --options="AaRrNn" --out=userinput
223
      readUserInput "AaRrNn" userinput
236 224
      option=${userinput,,}
237 225
    fi
238

  
239 226
    # evaluate option
240
    case $option in
241
      A)
242
        LOG_FILE=$(realpath $fname)
243
        ;;
227
    case "$option" in
244 228
      a)
245
        printInfo "appending log to existing file\n"
246
        LOG_FILE=$(realpath $fname)
247
        printf "\n" >> $LOG_FILE
248
        printf "######################################################################\n" >> $LOG_FILE
249
        printf "\n" >> $LOG_FILE
229
        if [ $quiet = false ]; then
230
          printInfo "appending log to $filepath\n"
231
        fi
232
        printf "\n" >> $filepath
233
        printf "######################################################################\n" >> $filepath
234
        printf "\n" >> $filepath
250 235
        ;;
251 236
      r)
252
        printInfo "old log file deleted, starting clean\n"
253
        LOG_FILE=$(realpath $fname)
254
        rm $LOG_FILE
237
        echo -n "" > $filepath
238
        if [ $quiet = false ]; then
239
          printInfo "content of $filepath wiped\n"
240
        fi
255 241
        ;;
256 242
      n)
257
        printInfo "no log file will be generated\n"
258
        LOG_FILE=""
243
        if [ $quiet = false ]; then
244
          printInfo "no log file will be generated\n"
245
        fi
246
        filepath=""
259 247
        ;;
260 248
      *) # sanity check (return error)
261
        printError "unexpected argument: $1\n"; return -1
262
        ;;
249
        printError "unexpected argument: $option\n"; return -1;;
263 250
    esac
264

  
265
    return 1
266 251
  else
267
    printInfo "log file set to $(realpath $fname)\n"
268
    LOG_FILE=$(realpath $fname)
269
    return 0
252
    if [ $quiet = false ]; then
253
      printInfo "log file set to $filepath\n"
254
    fi
270 255
  fi
256

  
257
  eval ${otherargs[1]}="$filepath"
258

  
259
  return 0
271 260
}
272 261

  
273 262
################################################################################
274 263
# SPECIFIC FUNCTIONS                                                           #
275 264
################################################################################
276 265

  
277
### print welcome text
278
# arguments: n/a
279
# return: n/a
266
### print welcome text #########################################################
267
# Prints a welcome message to standard out.
268
#
269
# usage:      printWelcomeText
270
# arguments:  n/a
271
# return:     n/a
272
#
280 273
function printWelcomeText {
281 274
  printf "######################################################################\n"
282 275
  printf "#                                                                    #\n"
......
297 290
  printf "######################################################################\n"
298 291
}
299 292

  
300
### print help text
301
# arguments: n/a
302
# return: n/a
293
### print help #################################################################
294
# Prints a help text to standard out.
295
#
296
# usage:      printHelp
297
# arguments:  n/a
298
# return:     n/a
299
#
303 300
function printHelp {
304 301
  printInfo "printing help text\n"
305
  printf "usage:  $(basename ${BASH_SOURCE[0]}) [options]\n"
306
  printf "options:\n"
307
  printf "  --help, -h        Print this help text.\n"
308
  printf "  --stm32flash, -f  Run stm32flash tool setup.\n"
309
  printf "  --SerialBoot, -s  Run SerialBoot tool setup.\n"
310
  printf "  --IDE, -e         Enter IDE setup.\n"
311
  printf "  --quit, -q        Quit the script.\n"
312
  printf "  --log=<file>      Specify a log file.\n"
302
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [-f|--stm32flash] [-s|--SerialBoot] [-e|--IDE] [-c|--compiler] [-q|--quit] [--log=<file>]\n"
303
  printf "\n"
304
  printf "options:  -h, --help\n"
305
  printf "              Print this help text.\n"
306
  printf "          -f, --stm32flash\n"
307
  printf "              Run st,32flash tool setup.\n"
308
  printf "          -s, --SerialBoot\n"
309
  printf "              Run SerialBoot tool setup.\n"
310
  printf "          -e, --IDE\n"
311
  printf "              Enter IDE setup.\n"
312
  printf "          -c, --compiler\n"
313
  printf "              Enter compiler setup.\n"
314
  printf "          -q, --quit\n"
315
  printf "              Quit the script.\n"
316
  printf "          --log=<file>\n"
317
  printf "              Specify a log file.\n"
313 318
}
314 319

  
315
### enter bootloader setup
316
# arguments n/a
317
# return:
318
#  -  0: no error
319
#  -  1: warning: function aborted by user
320
#  - -1: error: unexpected user input
320
### stm32flash tool setup ######################################################
321
# Fetches the source code for the stm32flash tool and builds the binary.
322
# If the tool was already initialized, it can be wiped and rebuilt.
323
#
324
# usage:      stm32flashSetup
325
# arguments:  n/a
326
# return:     0
327
#                 No error or warning occurred.
328
#             1
329
#                 Warning: Setup aborted by user.
330
#             -1
331
#                 Error: Unexpected user input.
332
#
321 333
function stm32flashSetup {
322 334
  local stm32flashdir=$(dirname $(realpath ${BASH_SOURCE[0]}))/Host/Source/stm32flash/
335
  local userdir=$(pwd)
323 336

  
324 337
  # if the stm32flash folder is not empty
325 338
  if [ ! -z "$(ls -A $stm32flashdir)" ]; then
326
    printWarning "$(realpath $stm32flashdir) is not empty. Delete and reinitialize? [y/n]\n"
327
    readUserInput --num-chars=1 --options="YyNn" --out=userinput
328
    case $userinput in
339
    printWarning "$stm32flashdir is not empty. Delete and reinitialize? [y/n]\n"
340
    local userinput=""
341
    readUserInput "YyNn" userinput
342
    case "$userinput" in
329 343
      Y|y)
330
        printInfo "wiping $stm32flashdir\n"
344
        printInfo "wiping ${stm32flashdir}...\n"
345
        # checkout base commit and delete all local branches
346
        git submodule update --force --checkout $stm32flashdir | tee -a $LOG_FILE
347
        cd $stm32flashdir
348
        local git_branches=($(git for-each-ref --format="%(refname)"))
349
        for branch in $git_branches ; do
350
          if [[ $branch = *"heads/"* ]]; then
351
            git branch -D ${branch##*/} | tee -a $LOG_FILE
352
          fi
353
        done
354
        cd $userdir
355
        # deinit stm32flash submodule and delete any remaining files
331 356
        git submodule deinit -f $stm32flashdir 2>&1 | tee -a $LOG_FILE
357
        rm -rf $stm32flashdir/*
332 358
        ;;
333 359
      N|n)
334 360
        printWarning "stm32flash setup aborted by user\n"
......
342 368
  fi
343 369

  
344 370
  # initialize submodule
345
  printInfo "initializing stm32flash submodule\n"
371
  printInfo "initializing stm32flash submodule...\n"
346 372
  git submodule update --init $stm32flashdir 2>&1 | tee -a $LOG_FILE
347 373

  
348 374
  # build the stm32flash tool
......
355 381
  return 0
356 382
}
357 383

  
358
### enter SerialBoot setup
359
# arguments n/a
360
# return:
361
#  -  0: no error
362
#  -  1: warning: function aborted by user
363
#  - -1: error: unexpected user input
384
### SerialBoot tool setup ######################################################
385
# Builds the SerialBoot tool.
386
# If the tool was built before, it can be deleted and rebuilt.
387
#
388
# usage:      serialBootSetup
389
# arguments:  n/a
390
# return:     0
391
#                 No errort or warning occurred.
392
#             1
393
#                 Warning: Setup aborted by user.
394
#             -1
395
#                 Error: Unexpected user input.
396
#
364 397
function serialBootSetup {
365 398
  local serialbootdir=$(dirname $(realpath ${BASH_SOURCE[0]}))/Host/Source/SerialBoot/
399
  local userdir=$(pwd)
366 400

  
367 401
  # if a build folder already exists
368 402
  if [ -d "${serialbootdir}/build/" ]; then
369 403
    printWarning "SerialBoot has been built before. Delete and rebuild? [y/n]\n"
370
    readUserInput --num-chars=1 --options="YyNn" --out=userinput
371
    case $userinput in
404
    local userinput=""
405
    readUserInput "YyNn" userinput
406
    case "$userinput" in
372 407
      Y|y)
373
        printInfo "deleting ${serialbootdir}build/\n"
408
        printInfo "deleting ${serialbootdir}build/...\n"
374 409
        rm -rf "${serialbootdir}build/"
375 410
        ;;
376 411
      N|n)
......
385 420
  fi
386 421

  
387 422
  # build SerialBoot
388
  printInfo "compiling SerialBoot\n"
389
  local userdir=${PWD}
390
  mkdir "${serialbootdir}build/"
391
  cd "${serialbootdir}build/"
423
  printInfo "compiling SerialBoot...\n"
424
  mkdir ${serialbootdir}build/
425
  cd ${serialbootdir}build/
392 426
  cmake .. 2>&1 | tee -a $LOG_FILE
393 427
  make 2>&1 | tee -a $LOG_FILE
394
  cd "$userdir"
428
  cd $userdir
395 429

  
396 430
  return 0
397 431
}
398 432

  
399
### enter IDE setup
400
# arguments n/a
401
# return: n/a
433
### IDE setup ##################################################################
434
# Enter the IDE setup.
435
#
436
# usage:      ideSetup
437
# arguments:  n/a
438
# return:     n/a
439
#
402 440
function ideSetup {
403
  printInfo "entering ideSetup setup\n"
441
  printInfo "entering IDE setup\n"
404 442
  printf "\n"
405
  if [ -z $LOG_FILE ]; then
406
    $(dirname $(realpath ${BASH_SOURCE[0]}))/ide/setup_IDE.sh --noinfo
443
  if [ -z "$LOG_FILE" ]; then
444
    $(dirname $(realpath ${BASH_SOURCE[0]}))/ide/idesetup.sh --noinfo
407 445
  else
408
    $(dirname $(realpath ${BASH_SOURCE[0]}))/ide/setup_IDE.sh --LOG="$LOG_FILE" --noinfo
446
    $(dirname $(realpath ${BASH_SOURCE[0]}))/ide/idesetup.sh --LOG="$LOG_FILE" --noinfo
409 447
  fi
410 448
}
411 449

  
412
################################################################################
413
# SCRIPT MAIN FUNCTION                                                         #
414
################################################################################
415

  
416
### main function of this script
417
# arguments: see help text
418
# return: error code
419
#   - 0: no error
420
#   - 1: error
421
function main {
422

  
423
  # parse arguments
424
  local arguments=$(getopt -l help,stm32flash,SerialBoot,IDE,quit,log:,LOG:,noinfo -o hfseq -- "$@")
425
  if [ $? != 0 ]; then
426
    printError "could not interprete arguments."
427
    exit 1
450
### compiler setup #############################################################
451
# Enter the compiler setup.
452
#
453
# usage:      compilerSetup
454
# arguments:  n/a
455
# return:     n/a
456
#
457
function compilerSetup {
458
  printInfo "entering IDE setup\n"
459
  printf "\n"
460
  if [ -z "$LOG_FILE" ]; then
461
    $(dirname $(realpath ${BASH_SOURCE[0]}))/compiler/compilersetup.sh --noinfo
462
  else
463
    $(dirname $(realpath ${BASH_SOURCE[0]}))/compiler/compilersetup.sh --LOG="$LOG_FILE" --noinfo
428 464
  fi
429
  eval set -- "$arguments"
465
}
430 466

  
467
### main function of this script ###############################################
468
# A setup to initialize dependencies, setup IDE projects and configure the
469
# compiler setup.
470
#
471
# usage:      see function printHelp
472
# arguments:  see function printHelp
473
# return:     0
474
#                 No error or warning occurred.
475
#
476
function main {
431 477
  # print welcome/info text if not suppressed
432 478
  if [[ $@ != *"--noinfo"* ]]; then
433 479
    printWelcomeText
......
437 483
  printf "\n"
438 484

  
439 485
  # set log file if specified
440
  if [[ $@ == *--log* || $@ == *--LOG* ]]; then
486
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
441 487
    # get the parameter (file name)
442 488
    local cmdidx=1
443
    while [ "${!cmdidx}" != "--log" ] && [ "${!cmdidx}" != "--LOG" ]; do
489
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
444 490
      cmdidx=$[cmdidx + 1]
445 491
    done
446
    local filenameidx=$[cmdidx + 1]
492
    local cmd="${!cmdidx}"
493
    local logfile=""
494
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
495
      logfile=${cmd#*=}
496
    else
497
      local filenameidx=$((cmdidx + 1))
498
      logfile="${!filenameidx}"
499
    fi
447 500
    # optionally force silent appending
448
    if [ "${!cmdidx}" == "--LOG" ]; then
449
      setLogFile --file="${!filenameidx}" --option=A
501
    if [[ "$cmd" = "--LOG"* ]]; then
502
      setLogFile --option=a --quiet "$logfile" LOG_FILE
450 503
    else
451
      setLogFile --file="${!filenameidx}"
504
      setLogFile "$logfile" LOG_FILE
452 505
      printf "\n"
453 506
    fi
454 507
  fi
......
463 516
    quitScript
464 517
  fi
465 518

  
466
  # handle command line arguments
467
  while [ true ]; do
468
    case $1 in
469
      --help|-h) # already handled; ignore
470
        shift 1;
471
        ;;
472
      --stm32flash|-f)
473
        stm32flashSetup; printf "\n"; shift 1
474
        ;;
475
      --SerialBoot|-s)
476
        serialBootSetup; printf "\n"; shift 1
477
        ;;
478
      --IDE|-e)
479
        IdeSetup; printf "\n"; shift 1;
480
        ;;
481
      --quit|-q)
482
        quitScript
483
        ;;
484
      --log|--LOG) # already handled; ignore
485
        shift 2
486
        ;;
487
      --noinfo) # already processed; ignore
488
        shift 1
489
        ;;
490
      --) # end of options reached
491
        shift; break
492
        ;;
493
      *) # sanity check (exit with error)
494
        printError "unexpected argument: $1\n"; exit -1
495
        ;;
496
    esac
519
  # parse arguments
520
  local otherargs=()
521
  while [ $# -gt 0 ]; do
522
    if ( parseIsOption $1 ); then
523
      case "$1" in
524
        -h|--help) # already handled; ignore
525
          shift 1;;
526
        -f|--stm32flash)
527
          stm32flashSetup; printf "\n"; shift 1;;
528
        -s|--SerialBoot)
529
          serialBootSetup; printf "\n"; shift 1;;
530
        -e|--IDE)
531
          ideSetup; printf "\n"; shift 1;;
532
        -c|--compiler)
533
          compilerSetup; printf "\n"; shift 1;;
534
        -q|--quit)
535
          quitScript; shift 1;;
536
        --log=*|--LOG=*) # already handled; ignore
537
          shift 1;;
538
        --log|--LOG) # already handled; ignore
539
          shift 2;;
540
        --noinfo) # already handled; ignore
541
          shift 1;;
542
        *)
543
          printError "invalid option: $1\n"; shift 1;;
544
      esac
545
    else
546
      otherargs+=("$1")
547
      shift 1
548
    fi
497 549
  done
498 550

  
499 551
  # interactive menu
500
  while [ true ]; do
552
  while ( true ); do
501 553
    # main menu info prompt and selection
502
    printInfo "AMiRo-BLT main menu\n"
554
    printInfo "AMiRo-BLT setup main menu\n"
503 555
    printf "Please select one of the following actions:\n"
504 556
    printf "  [F] - get and build stm32flash tool\n"
505 557
    printf "  [S] - build SerialBoot tool\n"
506 558
    printf "  [E] - IDE project setup\n"
559
    printf "  [C] - enter compiler setup\n"
507 560
    printf "  [Q] - quit this setup\n"
508
    readUserInput --num-chars=1 --options="FfSsEeQq" --out=userinput
561
    local userinput=""
562
    readUserInput "FfSsEeCcQq" userinput
509 563
    printf "\n"
510 564

  
511 565
    # evaluate user selection
512
    case $userinput in
513
      I|i)
514
        projectInitialization; printf "\n"
515
        ;;
566
    case "$userinput" in
516 567
      F|f)
517
        stm32flashSetup; printf "\n"
518
        ;;
568
        stm32flashSetup; printf "\n";;
519 569
      S|s)
520
        serialBootSetup; printf "\n"
521
        ;;
570
        serialBootSetup; printf "\n";;
522 571
      E|e)
523
        ideSetup; printf "\n"
524
        ;;
572
        ideSetup; printf "\n";;
573
      C|c)
574
        compilerSetup; printf "\n";;
525 575
      Q|q)
526
        quitScript
527
        ;;
576
        quitScript;;
528 577
      *) # sanity check (exit with error)
529
        printError "unexpected argument: $userinput\n"; exit -1
530
        ;;
578
        printError "unexpected argument: $userinput\n";;
531 579
    esac
532 580
  done
533 581

  
......
540 588

  
541 589
main "$@"
542 590

  
543

  
544
## initial info text (can be disabled with 'no_info' as last argument)
545
#if [[ ! ${!#} = "no_info" ]]; then
546
#  printf "######################################################################\n"
547
#  printf "#                                                                    #\n"
548
#  printf "#                  Welcome to the AMiRo-BLT setup!                   #\n"
549
#  printf "#                                                                    #\n"
550
#  printf "######################################################################\n"
551
#  printf "#                                                                    #\n"
552
#  printf "# Copyright (c) 2016..2017  Thomas Schöpping                         #\n"
553
#  printf "#                                                                    #\n"
554
#  printf "# This is free software; see the source for copying conditions.      #\n"
555
#  printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n"
556
#  printf "# A PARTICULAR PURPOSE. The development of this software was         #\n"
557
#  printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction  #\n"
558
#  printf "# Technology. The Excellence Cluster EXC 227 is a grant of the       #\n"
559
#  printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n"
560
#  printf "# Excellence Initiative.                                             #\n"
561
#  printf "#                                                                    #\n"
562
#  printf "######################################################################\n"
563
#  printf "\n"
564
#fi
565
#
566
## initialization of variables
567
#ARG_IDX=1
568
#USER_INPUT=${!ARG_IDX}
569
#
570
#while [[ ! $USER_INPUT =~ ^[Ee]$ ]]; do
571
#
572
#  # main menu info prompt and selection
573
#  printf "AMiRo-BLT setup\n"
574
#  printf "===============\n"
575
#  printf "\n"
576
#  if [[ -z $USER_INPUT || $USER_INPUT == "no_info" ]]; then
577
#    printf "Please select one of the following actions:\n"
578
#    printf "    [F] - stm32flash flashing tool setup\n"
579
#    printf "    [S] - SerialBoot flashing tool setup\n"
580
#    printf "    [Q] - setup QtCreator projects\n"
581
#    printf "    [E] - exit this setup\n"
582
#
583
#    while [[ ! $USER_INPUT =~ ^[FfSsQqEe]$ ]]; do
584
#      read -p "your selection: " -n 1 -e USER_INPUT
585
#      if [[ ! $USER_INPUT =~ ^[FfSsQqEe]$ ]]; then
586
#        printf "[%s] is no valid action. \n" $USER_INPUT
587
#      fi
588
#    done
589
#
590
#    printf "\n"
591
#    printf "######################################################################\n"
592
#    printf "\n"
593
#  fi
594
#
595
#  # action selection
596
#  case $USER_INPUT in
597
#
598
#    # stm32flash setup
599
#    F|f)
600
#      USER_DIR=${PWD}
601
#      cd $(dirname ${BASH_SOURCE[0]})
602
#
603
#      # initialize submodule
604
#      git submodule update --init ./Host/Source/stm32flash/
605
#
606
#      cd ./Host/Source/stm32flash/
607
#      BUILD_STM32FLASH=true
608
#
609
#      # test for existing bonary
610
#      if [ -f stm32flash ]; then
611
#        printf "WARNING: stm32flash binary already exists.\n"
612
#        read -p "Would you like to delete and rebuild it? [Y|n] " -n 1 -i "Y" -e USER_INPUT
613
#        if [[ $USER_INPUT =~ ^[Yy]$ ]]; then
614
#          BUILD_STM32FLASH=true
615
#          make clean
616
#        elif [[ ! $USER_INPUT =~ ^[YyNn]$ ]]; then
617
#          BUILD_STM32FLASH=false
618
#          printf "'%s' is no valid selection. Aborting setup.\n" $USER_INPUT
619
#        else
620
#          BUILD_STM32FLASH=false
621
#        fi
622
#     fi
623
#
624
#      # build the tool if required
625
#      if [ $BUILD_STM32FLASH = true ]; then
626
#       make
627
#      fi
628
#
629
#      cd $USER_DIR
630
#      ;;
631
#
632
#    # SerailBoot setup
633
#    S|s)
634
#      # print setup header
635
#      printf "SerialBoot setup\n"
636
#      printf "================\n"
637
#      printf "\n"
638
#
639
#      USER_DIR=${PWD}
640
#      cd $(dirname ${BASH_SOURCE[0]})/Host/Source/SerialBoot/
641
#      BUILD_SERIALBOOT=true
642
#
643
#      # test for existing binary
644
#      if [ -f build/SerialBoot ]; then
645
#        printf "WARNING: SerialBoot binary already exists.\n"
646
#        read -p "Would you like to delete and rebuild it? [Y|n] " -n 1 -i "Y" -e USER_INPUT
647
#        if [[ $USER_INPUT =~ ^[Yy]$ ]]; then
648
#          BUILD_SERIALBOOT=true
649
#          rm -rf build/
650
#        elif [[ ! $USER_INPUT =~ ^[YyNn]$ ]]; then
651
#          BUILD_SERIALBOOT=false
652
#          printf "'%s' is no valid selection. Aborting setup.\n" $USER_INPUT
653
#        else
654
#          BUILD_SERIALBOOT=false
655
#        fi
656
#      fi
657
#
658
#      # build the tool if requested
659
#      if [ $BUILD_SERIALBOOT = true ]; then
660
#        mkdir build
661
#        cd build
662
#        cmake ..
663
#        make
664
#      fi
665
#
666
#      cd $USER_DIR
667
#      ;;
668
#
669
#    # QtCreator setup
670
#    Q|q)
671
#      # calling the script with no arguments prints the help and returns
672
#      source $(dirname ${BASH_SOURCE[0]})/ide/QtCreator/QtCreatorSetup.sh no_info
673
#      printf "\n"
674
#      # set deafult arguments and call the script again
675
#      USER_INPUT="clean all"
676
#      read -p "select commands: " -i "$USER_INPUT" -e USER_INPUT
677
#      printf "\n"
678
#      source $(dirname ${BASH_SOURCE[0]})/ide/QtCreator/QtCreatorSetup.sh no_info $USER_INPUT
679
#      ;;
680
#
681
#    # exit
682
#    E|e)
683
#      break;
684
#      ;;
685
#
686
#    # sanity check
687
#    *)
688
#      printf "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
689
#      printf "ERROR (${LINENO}): unexpected state; aborting script\n"
690
#      printf "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
691
#      printf "\n"
692
#      exit
693
#      ;;
694
#
695
#  esac
696
#
697
#  printf "\n"
698
#  printf "######################################################################\n"
699
#  printf "\n"
700
#
701
#  let ARG_IDX++
702
#  USER_INPUT=${!ARG_IDX}
703
#
704
#done
705

  

Also available in: Unified diff