Statistics
| Branch: | Tag: | Revision:

amiro-blt / ide / setup_IDE.sh @ 7a91596e

History | View | Annotate | Download (13.6 KB)

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

    
24
#!/bin/bash
25

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

    
30
### print an error
31
# arguments:
32
#   - error string [required]
33
# return: n/a
34
function printError {
35
  local string="ERROR:   $1"
36

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

    
44
### print a warning
45
# arguments:
46
#   - warning string [required]
47
# return: n/a
48
function printWarning {
49
  local string="WARNING: $1"
50

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

    
58
### print an information text
59
# arguments:
60
#   - information string [required]
61
# return: n/a
62
function printInfo {
63
  local string="INFO:    $1"
64

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

    
72
### print a message to the log file
73
# arguments:
74
#   - information string [required]
75
# return: n/a
76
function printLog {
77
  local string="LOG:     $1"
78

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

    
85
### exit the script normally
86
# arguments: n/a
87
# return: 0 (success)
88
function quitScript {
89
  printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n"
90
  printf "\n"
91
  printf "######################################################################\n"
92

    
93
  exit 0
94
}
95

    
96
### read a user input
97
# arguments:
98
#   --numchars, -n  maximum number of characters to read
99
#   --options, -o   possible chacters to select (only applies if numchars=1)
100
#   --out, -e       output variable to store the user input to
101
# return: error code
102
#   -  0: no error
103
#   - -1: error: invalid arguments
104
function readUserInput {
105
  # parse arguments
106
  local arguments=$(getopt -l num-chars:,options:,out: -o n:o:e: -- "$@")
107
  if [ $? != 0 ]; then
108
    printError "could not interprete arguments."
109
    return -1
110
  fi
111
  eval set -- "$arguments"
112

    
113
  # evaluate arguments
114
  local numchars=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

    
137
  # 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"
147
    fi
148
  done
149
  printLog "[$_userinput] has been selected\n"
150
  if [ ! -z "$outvar" ]; then
151
    eval $outvar="$_userinput"
152
  fi
153

    
154
  return 0
155
}
156

    
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."
175
    return -1
176
  fi
177
  eval set -- "$arguments"
178

    
179
  # evaluate arguments
180
  local fname=""
181
  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
219
  done
220

    
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
  # if file already exists
227
  elif [ -f $fname ]; then
228
    # 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"
232
      printf "  [A] - append log\n"
233
      printf "  [R] - restart log (delete existing file)\n"
234
      printf "  [N] - no log\n"
235
      readUserInput --num-chars=1 --options="AaRrNn" --out=userinput
236
      option=${userinput,,}
237
    fi
238

    
239
    # evaluate option
240
    case $option in
241
      A)
242
        LOG_FILE=$(realpath $fname)
243
        ;;
244
      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
250
        ;;
251
      r)
252
        printInfo "old log file deleted, starting clean\n"
253
        LOG_FILE=$(realpath $fname)
254
        rm $LOG_FILE
255
        ;;
256
      n)
257
        printInfo "no log file will be generated\n"
258
        LOG_FILE=""
259
        ;;
260
      *) # sanity check (return error)
261
        printError "unexpected argument: $1\n"; return -1
262
        ;;
263
    esac
264

    
265
    return 1
266
  else
267
    printInfo "log file set to $(realpath $fname)\n"
268
    LOG_FILE=$(realpath $fname)
269
    return 0
270
  fi
271
}
272

    
273
################################################################################
274
# SPECIFIC FUNCTIONS                                                           #
275
################################################################################
276

    
277
### print welcome text
278
# arguments: n/a
279
# return: n/a
280
function printWelcomeText {
281
  printf "######################################################################\n"
282
  printf "#                                                                    #\n"
283
  printf "#                Welcome to the AMiRo-BLT IDE setup!                 #\n"
284
  printf "#                                                                    #\n"
285
  printf "######################################################################\n"
286
  printf "#                                                                    #\n"
287
  printf "# Copyright (c) 2016..2017  Thomas Schöpping                         #\n"
288
  printf "#                                                                    #\n"
289
  printf "# This is free software; see the source for copying conditions.      #\n"
290
  printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n"
291
  printf "# A PARTICULAR PURPOSE. The development of this software was         #\n"
292
  printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction  #\n"
293
  printf "# Technology. The Excellence Cluster EXC 227 is a grant of the       #\n"
294
  printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n"
295
  printf "# Excellence Initiative.                                             #\n"
296
  printf "#                                                                    #\n"
297
  printf "######################################################################\n"
298
}
299

    
300
### print help text
301
# arguments: n/a
302
# return: n/a
303
function printHelp {
304
  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 "  --QtCreator, -c   Generate QtCreator project files.\n"
309
  printf "  --quit, -q        Quit the script.\n"
310
  printf "  --log=<file>      Specify a log file.\n"
311
}
312

    
313
### enter QtCreator setup
314
# arguments n/a
315
# return:
316
#  -  0: no error
317
#  -  1: warning: function aborted by user
318
#  - -1: error: unexpected user input
319
function qtCreatorSetup {
320
  printInfo "entering QtCreator setup\n"
321
  printf "\n"
322
  if [ -z "$LOG_FILE" ]; then
323
    $(realpath $(dirname ${BASH_SOURCE}))/QtCreator/QtCreatorSetup.sh --noinfo
324
  else
325
    $(realpath $(dirname ${BASH_SOURCE}))/QtCreator/QtCreatorSetup.sh --LOG="$LOG_FILE" --noinfo
326
  fi
327

    
328
  return 0
329
}
330

    
331
################################################################################
332
# SCRIPT MAIN FUNCTION                                                         #
333
################################################################################
334

    
335
### main function of this script
336
# arguments: see help text
337
# return: error code
338
#   - 0: no error
339
#   - 1: error
340
function main {
341

    
342
  # parse arguments
343
  local arguments=$(getopt -l help,QtCreator,quit,log:,LOG:,noinfo -o hcq -- "$@")
344
  if [ $? != 0 ]; then
345
    printError "could not interprete arguments."
346
    exit 1
347
  fi
348
  eval set -- "$arguments"
349

    
350
  # print welcome/info text if not suppressed
351
  if [[ $@ != *"--noinfo"* ]]; then
352
    printWelcomeText
353
  else
354
    printf "######################################################################\n"
355
  fi
356
  printf "\n"
357

    
358
  # set log file if specified
359
  if [[ $@ == *--log* || $@ == *--LOG* ]]; then
360
    # get the parameter (file name)
361
    local cmdidx=1
362
    while [ "${!cmdidx}" != "--log" ] && [ "${!cmdidx}" != "--LOG" ]; do
363
      cmdidx=$[cmdidx + 1]
364
    done
365
    local filenameidx=$[cmdidx + 1]
366
    # optionally force silent appending
367
    if [ "${!cmdidx}" == "--LOG" ]; then
368
      setLogFile --file="${!filenameidx}" --option=A
369
    else
370
      setLogFile --file="${!filenameidx}"
371
      printf "\n"
372
    fi
373
  fi
374

    
375
  # log script name
376
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
377

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

    
385
  # handle command line arguments
386
  while [ true ]; do
387
    case $1 in
388
      --help|-h) # already handled; ignore
389
        shift 1;
390
        ;;
391
      --QtCreator)
392
        qtCreatorSetup; printf "\n"; shift 1
393
        ;;
394
      --quit|-q)
395
        quitScript
396
        ;;
397
      --log|--LOG) # already handled; ignore
398
        shift 2
399
        ;;
400
      --noinfo) # already processed; ignore
401
        shift 1
402
        ;;
403
      --) # end of options reached
404
        shift; break
405
        ;;
406
      *) # sanity check (exit with error)
407
        printError "unexpected argument: $1\n"; exit -1
408
        ;;
409
    esac
410
  done
411

    
412
  # interactive menu
413
  while [ true ]; do
414
    # main menu info prompt and selection
415
    printInfo "AMiRo-BLT IDE setup main menu\n"
416
    printf "Please select one of the following actions:\n"
417
    printf "  [C] - enter QtCreator setup\n"
418
    printf "  [Q] - quit this setup\n"
419
    readUserInput --num-chars=1 --options="CcQq" --out=userinput
420
    printf "\n"
421

    
422
    # evaluate user selection
423
    case $userinput in
424
      C|c)
425
        qtCreatorSetup; printf "\n"
426
        ;;
427
      Q|q)
428
        quitScript
429
        ;;
430
      *) # sanity check (exit with error)
431
        printError "unexpected argument: $userinput\n"; exit -1
432
        ;;
433
    esac
434
  done
435

    
436
  exit 0
437
}
438

    
439
################################################################################
440
# SCRIPT ENTRY POINT                                                           #
441
################################################################################
442

    
443
main "$@"