Statistics
| Branch: | Tag: | Revision:

amiro-blt / ide / idesetup.sh @ 0a42f078

History | View | Annotate | Download (15 KB)

1
################################################################################
2
# AMiRo-BLT is an bootloader and toolchain designed for the Autonomous Mini    #
3
# Robot (AMiRo) platform.                                                      #
4
# Copyright (C) 2016..2017  Thomas Schöpping et al.                            #
5
#                                                                              #
6
# This program is free software: you can redistribute it and/or modify         #
7
# it under the terms of the GNU General Public License as published by         #
8
# the Free Software Foundation, either version 3 of the License, or            #
9
# (at your option) any later version.                                          #
10
#                                                                              #
11
# This program is distributed in the hope that it will be useful,              #
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
14
# GNU General Public License for more details.                                 #
15
#                                                                              #
16
# You should have received a copy of the GNU General Public License            #
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.        #
18
#                                                                              #
19
# This research/work was supported by the Cluster of Excellence Cognitive      #
20
# Interaction Technology 'CITEC' (EXC 277) at Bielefeld University, which is   #
21
# funded by the German Research Foundation (DFG).                              #
22
################################################################################
23

    
24
#!/bin/bash
25

    
26
################################################################################
27
# GENERIC FUNCTIONS                                                            #
28
################################################################################
29

    
30
### print an error 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
  printLog "exiting $(realpath ${BASH_SOURCE[0]})\n"
110
  printf "######################################################################\n"
111
  exit 0
112
}
113

    
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
#
126
function readUserInput {
127
  local input=""
128
  # read user input
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"
133
    fi
134
  done
135
  printLog "[$input] has been selected\n"
136
  eval $2="$input"
137
}
138

    
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
158
    return -1
159
  fi
160
}
161

    
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=""
189
  local option=""
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
210
  done
211
  filepath=$(realpath ${otherargs[0]})
212

    
213
  # if file already exists
214
  if [ -e $filepath ]; then
215
    # if no option was specified, ask what to do
216
    if [ -z "$option" ]; then
217
      printWarning "log file $filepath already esists\n"
218
      local userinput=""
219
      printf "Select what to do:\n"
220
      printf "  [A] - append log\n"
221
      printf "  [R] - restart log (delete existing file)\n"
222
      printf "  [N] - no log\n"
223
      readUserInput "AaRrNn" userinput
224
      option=${userinput,,}
225
    fi
226
    # evaluate option
227
    case "$option" in
228
      a)
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
235
        ;;
236
      r)
237
        echo -n "" > $filepath
238
        if [ $quiet = false ]; then
239
          printInfo "content of $filepath wiped\n"
240
        fi
241
        ;;
242
      n)
243
        if [ $quiet = false ]; then
244
          printInfo "no log file will be generated\n"
245
        fi
246
        filepath=""
247
        ;;
248
      *) # sanity check (return error)
249
        printError "unexpected argument: $option\n"; return -1;;
250
    esac
251
  else
252
    if [ $quiet = false ]; then
253
      printInfo "log file set to $filepath\n"
254
    fi
255
  fi
256

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

    
259
  return 0
260
}
261

    
262
################################################################################
263
# SPECIFIC FUNCTIONS                                                           #
264
################################################################################
265

    
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
#
273
function printWelcomeText {
274
  printf "######################################################################\n"
275
  printf "#                                                                    #\n"
276
  printf "#                Welcome to the AMiRo-BLT IDE setup!                 #\n"
277
  printf "#                                                                    #\n"
278
  printf "######################################################################\n"
279
  printf "#                                                                    #\n"
280
  printf "# Copyright (c) 2016..2017  Thomas Schöpping                         #\n"
281
  printf "#                                                                    #\n"
282
  printf "# This is free software; see the source for copying conditions.      #\n"
283
  printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n"
284
  printf "# A PARTICULAR PURPOSE. The development of this software was         #\n"
285
  printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction  #\n"
286
  printf "# Technology. The Excellence Cluster EXC 227 is a grant of the       #\n"
287
  printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n"
288
  printf "# Excellence Initiative.                                             #\n"
289
  printf "#                                                                    #\n"
290
  printf "######################################################################\n"
291
}
292

    
293
### print help #################################################################
294
# Prints a help text to standard out.
295
#
296
# usage:      printHelp
297
# arguments:  n/a
298
# return:     n/a
299
#
300
function printHelp {
301
  printInfo "printing help text\n"
302
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [--QtCreator] [-q|--quit] [--log=<file>]\n"
303
  printf "\n"
304
  printf "options:  -h, --help\n"
305
  printf "              Print this help text.\n"
306
  printf "          --QtCreator\n"
307
  printf "              Enter QtCreator setup.\n"
308
  printf "          -q, --quit\n"
309
  printf "              Quit the script.\n"
310
  printf "          --log=<file>\n"
311
  printf "              Specify a log file.\n"
312
}
313

    
314
### enter QtCreator setup ######################################################
315
# Enter the QtCreator IDE setup.
316
#
317
# usage:      qtCreatorSetup
318
# arguments:  n/a
319
# return:     n/a
320
function qtCreatorSetup {
321
  printInfo "entering QtCreator setup...\n"
322
  printf "\n"
323
  if [ -z "$LOG_FILE" ]; then
324
    $(realpath $(dirname ${BASH_SOURCE[0]}))/QtCreator/QtCreatorSetup.sh --noinfo
325
  else
326
    $(realpath $(dirname ${BASH_SOURCE[0]}))/QtCreator/QtCreatorSetup.sh --LOG="$LOG_FILE" --noinfo
327
  fi
328
}
329

    
330
### main function of this script ###############################################
331
# The IDE setup lets the user select an IDE of choice.
332
# As of now, only QtCreator is supported.
333
#
334
# usage:      see function printHelp
335
# arguments:  see function printHelp
336
# return:     0
337
#                 No error or warning occurred.
338
#
339
function main {
340
  # print welcome/info text if not suppressed
341
  if [[ $@ != *"--noinfo"* ]]; then
342
    printWelcomeText
343
  else
344
    printf "######################################################################\n"
345
  fi
346
  printf "\n"
347

    
348
  # set log file if specified
349
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
350
    # get the parameter (file name)
351
    local cmdidx=1
352
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
353
      cmdidx=$[cmdidx + 1]
354
    done
355
    local cmd="${!cmdidx}"
356
    local logfile=""
357
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
358
      logfile=${cmd#*=}
359
    else
360
      local filenameidx=$((cmdidx + 1))
361
      logfile="${!filenameidx}"
362
    fi
363
    # optionally force silent appending
364
    if [[ "$cmd" = "--LOG"* ]]; then
365
      setLogFile --option=a --quiet "$logfile" LOG_FILE
366
    else
367
      setLogFile "$logfile" LOG_FILE
368
      printf "\n"
369
    fi
370
  fi
371

    
372
  # log script name
373
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
374

    
375
  # if --help or -h was specified, print the help text and exit
376
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
377
    printHelp
378
    printf "\n"
379
    quitScript
380
  fi
381

    
382
  # parse arguments
383
  local otherargs=()
384
  while [ $# -gt 0 ]; do
385
    if ( parseIsOption $1 ); then
386
      case "$1" in
387
        -h|--help) # already handled; ignore
388
          shift 1;;
389
        --QtCreator)
390
          qtCreatorSetup; printf "\n"; shift 1;;
391
        -q|--quit)
392
          quitScript; printf "\n"; shift 1;;
393
        --log=*|--LOG=*) # already handled; ignore
394
          shift 1;;
395
        --log|--LOG) # already handled; ignore
396
          shift 2;;
397
        --noinfo) # already handled; ignore
398
          shift 1;;
399
        *)
400
          printError "invalid option: $1\n"; shift 1;;
401
      esac
402
    else
403
      otherargs+=("$1")
404
      shift 1
405
    fi
406
  done
407

    
408
  # interactive menu
409
  while ( true ); do
410
    # main menu info prompt and selection
411
    printInfo "AMiRo-BLT IDE setup main menu\n"
412
    printf "Please select one of the following actions:\n"
413
    printf "  [C] - enter QtCreator setup\n"
414
    printf "  [Q] - quit this setup\n"
415
    local userinput=""
416
    readUserInput "CcQq" userinput
417
    printf "\n"
418

    
419
    # evaluate user selection
420
    case "$userinput" in
421
      C|c)
422
        qtCreatorSetup; printf "\n";;
423
      Q|q)
424
        quitScript;;
425
      *) # sanity check (exit with error)
426
        printError "unexpected argument: $userinput\n";;
427
    esac
428
  done
429

    
430
  exit 0
431
}
432

    
433
################################################################################
434
# SCRIPT ENTRY POINT                                                           #
435
################################################################################
436

    
437
main "$@"