Statistics
| Branch: | Tag: | Revision:

amiro-blt / ide / QtCreator / QtCreatorSetup.sh @ 1da30dfc

History | View | Annotate | Download (29 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=1
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
    read -p "your selection: " -n $numchars -e _userinput
141
    if [ -z $_userinput ] || ( [ $numchars == 1 ] && [ ! -z "$options" ] && [[ ! $_userinput =~ ^["$options"]$ ]] ); then
142
      printWarning "[$_userinput] is no valid action\n"
143
    fi
144
  done
145
  printLog "[$_userinput] has been selected\n"
146
  if [ ! -z "$outvar" ]; then
147
    eval $outvar="$_userinput"
148
  fi
149

    
150
  return 0
151
}
152

    
153
### set the log file
154
# arguments:
155
#   --file, -f    name of file [required]
156
#   --option, -o  option if file already exists [optional]
157
#                 a: append
158
#                 r: delete and restart
159
#                 n: disable log
160
#                 A: silent append (no message in the log file)
161
# return: error code
162
#   -  0: no error
163
#   -  1: warning: file already exists
164
#   -  2: warning: no file specified
165
#   - -1: error: invalid arguments
166
function setLogFile {
167
  # parse arguments
168
  local arguments=$(getopt -l file:,option: -o f:o: -- "$@")
169
  if [ $? != 0 ]; then
170
    printError "could not interprete arguments."
171
    return -1
172
  fi
173
  eval set -- "$arguments"
174

    
175
  # evaluate arguments
176
  local fname=""
177
  local option=""
178
  while [ true ]; do
179
    case "$1" in
180
      --file|f)
181
        if [ ! -z $2 ]; then
182
          fname="$(dirname $2)/$(basename $2)"
183
        else
184
          fname=""
185
        fi
186
        shift 2
187
        ;;
188
      --option|-o)
189
        case $2 in
190
          A)
191
            option="A"
192
            ;;
193
          a)
194
            option="a"
195
            ;;
196
          r)
197
            option="r"
198
            ;;
199
          n)
200
            option="n"
201
            ;;
202
          *) # sanity check (return error)
203
            printError "unexpected argument: $1\n"; return -1
204
            ;;
205
        esac
206
        shift 2
207
        ;;
208
      --) # end of options reached
209
        shift; break
210
        ;;
211
      *) # sanity check (return error)
212
        printError "unexpected argument: $1\n"; return -1
213
        ;;
214
    esac
215
  done
216

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

    
235
    # evaluate option
236
    case $option in
237
      A)
238
        LOG_FILE=$(realpath $fname)
239
        ;;
240
      a)
241
        printInfo "appending log to existing file\n"
242
        LOG_FILE=$(realpath $fname)
243
        printf "\n" >> $LOG_FILE
244
        printf "######################################################################\n" >> $LOG_FILE
245
        printf "\n" >> $LOG_FILE
246
        ;;
247
      r)
248
        printInfo "old log file deleted, starting clean\n"
249
        LOG_FILE=$(realpath $fname)
250
        rm $LOG_FILE
251
        ;;
252
      n)
253
        printInfo "no log file will be generated\n"
254
        LOG_FILE=""
255
        ;;
256
      *) # sanity check (return error)
257
        printError "unexpected argument: $1\n"; return -1
258
        ;;
259
    esac
260

    
261
    return 1
262
  else
263
    printInfo "log file set to $(realpath $fname)\n"
264
    LOG_FILE=$(realpath $fname)
265
    return 0
266
  fi
267
}
268

    
269
################################################################################
270
# SPECIFIC FUNCTIONS                                                           #
271
################################################################################
272

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

    
296
### print help text
297
# arguments: n/a
298
# return: n/a
299
function printHelp {
300
  printInfo "printing help text\n"
301
  printf "usage:  $(basename ${BASH_SOURCE[0]}) [options]\n"
302
  printf "options:\n"
303
  printf "  --help, -h              Print this help text.\n"
304
  printf "  --clean, -c             Delete project files.\n"
305
  printf "  --wipe, -w              Delete project and .user files.\n"
306
  printf "  --LightRing, -L         Create a project for the LightRing module.\n"
307
  printf "  --PowerManagement, -P   Create a project for the PowerManagement module.\n"
308
  printf "  --DiWheelDrive, -D      Create a project for the DiWheelDrive module.\n"
309
  printf "  --All, -A               Create projects for all modules.\n"
310
  printf "  --quit, -q              Quit the script.\n"
311
  printf "  --log=<file>            Specify a log file.\n"
312
}
313

    
314
### read directory where to create/delete projects
315
# arguments:
316
#   - $1: variable to store the path to [required]
317
# return: n/a
318
function getProjectDir {
319
  printLog "reading path for project files\n"
320
  local amirobltdir="$(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/)"
321
  local pojectdir=""
322
  read -p "Path where to create/delete project files: " -i $amirobltdir -e projectdir
323
  printLog "user selected path $(realpath $projectdir)\n"
324
  eval $1="$(realpath $projectdir)"
325
}
326

    
327
### retrieves the ARM-NONE-EABI-GCC include directory
328
# arguments:
329
#   - $1: variable to store the path to [required]
330
# return:
331
#  -  0: no error
332
#  - -1: error: arm-none-eabi-gcc not found
333
function retrieveGccIncludeDir {
334
  # retrieve binary path or link
335
  local binpath="$(which arm-none-eabi-gcc)"
336
  if [ -z "$binpath" ]; then
337
    printError "arm-none-eabi-gcc not found\n"
338
    return -1
339
  fi
340

    
341
  # traverse any links
342
  while [ -L "$binpath" ]; do
343
    binpath=$(readlink $binpath)
344
  done
345
  printInfo "gcc-arm-none-eabi detected: $binpath\n"
346

    
347
  # return include path
348
  eval $1=$(realpath $(dirname ${binpath})/../arm-none-eabi/include/)
349

    
350
  return 0
351
}
352

    
353
### delete project files
354
# arguments:
355
#  --path, -p <path>  path where to delete the project files
356
#  --out, -o <var>    variable to store the path to
357
#  --wipe, -w         delete .user files as well
358
# return:
359
#  -  0: no error
360
#  -  1: warning: function aborted by user
361
#  - -1: error: unexpected user input
362
function deleteProjects {
363
  # parse arguments
364
  local arguments=$(getopt -l path:,out:,wipe -o p:o:w -- "$@")
365
  if [ $? != 0 ]; then
366
    printError "could not interprete arguments."
367
    return -1
368
  fi
369
  eval set -- "$arguments"
370

    
371
  local projectdir=""
372
  local outvar=""
373
  local wipe=false
374

    
375
  # handle command line arguments
376
  while [ true ]; do
377
    case $1 in
378
      --path|-p)
379
        projectdir="$2"; shift 2
380
        ;;
381
      --out|-o)
382
        outvar=$2; shift 2
383
        ;;
384
      --wipe|-w)
385
        wipe=true; shift 1
386
        ;;
387
      --) # end of options reached
388
        shift; break
389
        ;;
390
      *) # sanity check (exit with error)
391
        printError "unexpected argument: $1\n"; return 1
392
        ;;
393
    esac
394
  done
395

    
396
  # print message
397
  if [ $wipe != true ]; then
398
    printInfo "deleting all QtCreator project files (*.includes, *.files, *.config, and *.creator)\n"
399
  else
400
    printInfo "deleting all QtCreator project files (*.includes, *.files, *.config, *.creator, and *.user)\n"
401
  fi
402

    
403
  # read project directory if required
404
  if [ -z "$projectdir" ]; then
405
    getProjectDir projectdir
406
  fi
407

    
408
  # remove all project files
409
  rm ${projectdir}/LightRing.includes 2>&1 | tee -a $LOG_FILE
410
  rm ${projectdir}/PowerManagement.includes 2>&1 | tee -a $LOG_FILE
411
  rm ${projectdir}/DiWheelDrive.includes 2>&1 | tee -a $LOG_FILE
412

    
413
  rm ${projectdir}/LightRing.files 2>&1 | tee -a $LOG_FILE
414
  rm ${projectdir}/PowerManagement.files 2>&1 | tee -a $LOG_FILE
415
  rm ${projectdir}/DiWheelDrive.files 2>&1 | tee -a $LOG_FILE
416

    
417
  rm ${projectdir}/LightRing.config 2>&1 | tee -a $LOG_FILE
418
  rm ${projectdir}/PowerManagement.config 2>&1 | tee -a $LOG_FILE
419
  rm ${projectdir}/DiWheelDrive.config 2>&1 | tee -a $LOG_FILE
420

    
421
  rm ${projectdir}/LightRing.creator 2>&1 | tee -a $LOG_FILE
422
  rm ${projectdir}/PowerManagement.creator 2>&1 | tee -a $LOG_FILE
423
  rm ${projectdir}/DiWheelDrive.creator 2>&1 | tee -a $LOG_FILE
424

    
425
  if [ $wipe == true ]; then
426
    rm ${projectdir}/LightRing.user 2>&1 | tee -a $LOG_FILE
427
    rm ${projectdir}/PowerManagement.user 2>&1 | tee -a $LOG_FILE
428
    rm ${projectdir}/DiWheelDrive.user 2>&1 | tee -a $LOG_FILE
429
  fi
430

    
431
  # store the path to the output variable, if required
432
  if [ ! -z "$outvar" ]; then
433
    eval $outvar="$projectdir"
434
  fi
435

    
436
  return 0
437
}
438

    
439
### create LightRing project files
440
# arguments:
441
#  --path, -p <path>  path where to create the project files
442
#  --out, -o <var>    variable to store the path to
443
# return:
444
#  -  0: no error
445
#  -  1: warning: function aborted by user
446
#  - -1: error: unexpected user input
447
function createLightRingProject {
448
  # parse arguments
449
  local arguments=$(getopt -l path:,out: -o p:o: -- "$@")
450
  if [ $? != 0 ]; then
451
    printError "could not interprete arguments."
452
    return -1
453
  fi
454
  eval set -- "$arguments"
455

    
456
  local projectdir=""
457
  local outvar=""
458

    
459
  # handle command line arguments
460
  while [ true ]; do
461
    case $1 in
462
      --path|-p)
463
        projectdir="$2"; shift 2
464
        ;;
465
      --out|-o)
466
        outvar=$2; shift 2
467
        ;;
468
      --) # end of options reached
469
        shift; break
470
        ;;
471
      *) # sanity check (exit with error)
472
        printError "unexpected argument: $1\n"; return 1
473
        ;;
474
    esac
475
  done
476

    
477
  # print message
478
  printInfo "creating QtCreator project files for the LightRing module\n"
479

    
480
  # read project directory if required
481
  if [ -z "$projectdir" ]; then
482
    getProjectDir projectdir
483
  fi
484

    
485
  # retrieve gcc-arm-none-eabi include dir
486
  local gccincludedir=""
487
  retrieveGccIncludeDir gccincludedir
488
  if [ -z "$gccincludedir" ]; then
489
    return -1
490
  fi
491

    
492
  # create project files
493
  # generate a file that contains all subdirectories as includes (but ignore hidden and documentation directories)
494
  find $gccincludedir -type d > ${projectdir}/LightRing.includes
495
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Source/) -type d | grep -v "ARMCM4_STM32" >> ${projectdir}/LightRing.includes
496
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Demo/ARMCM3_STM32F103_LightRing_GCC/Boot/) -type d | grep -v "uip\|fatfs\|ethernetlib\|cmd\|ide" >> ${projectdir}/LightRing.includes
497
  # generate a file that specifies all files
498
  echo -n "" > ${projectdir}/LightRing.files
499
  for path in `cat ${projectdir}/LightRing.includes`; do
500
    find $path -maxdepth 1 -type f \( ! -iname ".*" \) | grep -v "/arm-none-eabi/" | grep -E ".*(\.h|\.c|\.x)$" >> ${projectdir}/LightRing.files
501
  done
502
  # generate a default project configuration file if none exists so far
503
  if [ ! -f ${projectdir}/LightRing.config ]; then
504
    echo -e "// Add predefined macros for your project here. For example:" > ${projectdir}/LightRing.config
505
    echo -e "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/LightRing.config
506
    echo -e "" >> ${projectdir}/LightRing.config
507
  fi
508
  # generate a default .creator file if none exists so far
509
  if [ ! -f ${projectdir}/LightRing.creator ]; then
510
    echo -e "[general]" > ${projectdir}/LightRing.creator
511
    echo -e "" >> ${projectdir}/LightRing.creator
512
  fi
513

    
514
  # store the path to the output variable, if required
515
  if [ ! -z "$outvar" ]; then
516
    eval $outvar="$projectdir"
517
  fi
518

    
519
  return 0
520
}
521

    
522
### create PowerManagement project files
523
# arguments:
524
#  --path, -p <path>  path where to create the project files
525
#  --out, -o <var>    variable to store the path to
526
# return:
527
#  -  0: no error
528
#  -  1: warning: function aborted by user
529
#  - -1: error: unexpected user input
530
function createPowerManagementProject {
531
  # parse arguments
532
  local arguments=$(getopt -l path:,out: -o p:o: -- "$@")
533
  if [ $? != 0 ]; then
534
    printError "could not interprete arguments."
535
    return -1
536
  fi
537
  eval set -- "$arguments"
538

    
539
  local projectdir=""
540
  local outvar=""
541

    
542
  # handle command line arguments
543
  while [ true ]; do
544
    case $1 in
545
      --path|-p)
546
        projectdir="$2"; shift 2
547
        ;;
548
      --out|-o)
549
        outvar=$2; shift 2
550
        ;;
551
      --) # end of options reached
552
        shift; break
553
        ;;
554
      *) # sanity check (exit with error)
555
        printError "unexpected argument: $1\n"; return 1
556
        ;;
557
    esac
558
  done
559

    
560
  # print message
561
  printInfo "creating QtCreator project files for the PowerManagement module\n"
562

    
563
  # read project directory if required
564
  if [ -z "$projectdir" ]; then
565
    getProjectDir projectdir
566
  fi
567

    
568
  # retrieve gcc-arm-none-eabi include dir
569
  local gccincludedir=""
570
  retrieveGccIncludeDir gccincludedir
571
  if [ -z "$gccincludedir" ]; then
572
    return -1
573
  fi
574

    
575
  # create project files
576
  # generate a file that contains all subdirectories as includes (but ignore hidden and documentation directories)
577
  find $gccincludedir -type d > ${projectdir}/PowerManagement.includes
578
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Source/) -type d | grep -v "ARMCM3_STM32" >> ${projectdir}/PowerManagement.includes
579
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Demo/ARMCM4_STM32F405_Power_Management_GCC/Boot/) -type d | grep -v "uip\|fatfs\|ethernetlib\|cmd\|ide" >> ${projectdir}/PowerManagement.includes
580
  # generate a file that specifies all files
581
  echo -n "" > ${projectdir}/PowerManagement.files
582
  for path in `cat ${projectdir}/PowerManagement.includes`; do
583
    find $path -maxdepth 1 -type f \( ! -iname ".*" \) | grep -v "/arm-none-eabi/" | grep -E ".*(\.h|\.c|\.x)$" >> ${projectdir}/PowerManagement.files
584
  done
585
  # generate a default project configuration file if none exists so far
586
  if [ ! -f ${projectdir}/PowerManagement.config ]; then
587
    echo -e "// Add predefined macros for your project here. For example:" > ${projectdir}/PowerManagement.config
588
    echo -e "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/PowerManagement.config
589
    echo -e "" >> ${projectdir}/PowerManagement.config
590
  fi
591
  # generate a default .creator file if none exists so far
592
  if [ ! -f ${projectdir}/PowerManagement.creator ]; then
593
    echo -e "[general]" > ${projectdir}/PowerManagement.creator
594
    echo -e "" >> ${projectdir}/PowerManagement.creator
595
  fi
596

    
597
  # store the path to the output variable, if required
598
  if [ ! -z "$outvar" ]; then
599
    eval $outvar="$projectdir"
600
  fi
601

    
602
  return 0
603
}
604

    
605
### create DiWheelDrive project files
606
# arguments:
607
#  --path, -p <path>  path where to create the project files
608
#  --out, -o <var>    variable to store the path to
609
# return:
610
#  -  0: no error
611
#  -  1: warning: function aborted by user
612
#  - -1: error: unexpected user input
613
function createDiWheelDriveProject {
614
  # parse arguments
615
  local arguments=$(getopt -l path:,out: -o p:o: -- "$@")
616
  if [ $? != 0 ]; then
617
    printError "could not interprete arguments."
618
    return -1
619
  fi
620
  eval set -- "$arguments"
621

    
622
  local projectdir=""
623
  local outvar=""
624

    
625
  # handle command line arguments
626
  while [ true ]; do
627
    case $1 in
628
      --path|-p)
629
        projectdir="$2"; shift 2
630
        ;;
631
      --out|-o)
632
        outvar=$2; shift 2
633
        ;;
634
      --) # end of options reached
635
        shift; break
636
        ;;
637
      *) # sanity check (exit with error)
638
        printError "unexpected argument: $1\n"; return 1
639
        ;;
640
    esac
641
  done
642

    
643
  # print message
644
  printInfo "creating QtCreator project files for the DiWheelDrive module\n"
645

    
646
  # read project directory if required
647
  if [ -z "$projectdir" ]; then
648
    getProjectDir projectdir
649
  fi
650

    
651
  # retrieve gcc-arm-none-eabi include dir
652
  local gccincludedir=""
653
  retrieveGccIncludeDir gccincludedir
654
  if [ -z "$gccincludedir" ]; then
655
    return -1
656
  fi
657

    
658
  # create project files
659
  # generate a file that contains all subdirectories as includes (but ignore hidden and documentation directories)
660
  find $gccincludedir -type d > ${projectdir}/DiWheelDrive.includes
661
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Source/) -type d | grep -v "ARMCM4_STM32" >> ${projectdir}/DiWheelDrive.includes
662
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Demo/ARMCM3_STM32F103_DiWheelDrive_GCC/Boot/) -type d | grep -v "uip\|fatfs\|ethernetlib\|cmd\|ide" >> ${projectdir}/DiWheelDrive.includes
663
  # generate a file that specifies all files
664
  echo -n "" > ${projectdir}/DiWheelDrive.files
665
  for path in `cat ${projectdir}/DiWheelDrive.includes`; do
666
    find $path -maxdepth 1 -type f \( ! -iname ".*" \) | grep -v "/arm-none-eabi/" | grep -E ".*(\.h|\.c|\.x)$" >> ${projectdir}/DiWheelDrive.files
667
  done
668
  # generate a default project configuration file if none exists so far
669
  if [ ! -f ${projectdir}/DiWheelDrive.config ]; then
670
    echo -e "// Add predefined macros for your project here. For example:" > ${projectdir}/DiWheelDrive.config
671
    echo -e "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/DiWheelDrive.config
672
    echo -e "" >> ${projectdir}/DiWheelDrive.config
673
  fi
674
  # generate a default .creator file if none exists so far
675
  if [ ! -f ${projectdir}/DiWheelDrive.creator ]; then
676
    echo -e "[general]" > ${projectdir}/DiWheelDrive.creator
677
    echo -e "" >> ${projectdir}/DiWheelDrive.creator
678
  fi
679

    
680
  # store the path to the output variable, if required
681
  if [ ! -z "$outvar" ]; then
682
    eval $outvar="$projectdir"
683
  fi
684

    
685
  return 0
686
}
687

    
688
### create project files for al modules
689
# arguments:
690
#  --path, -p <path>  path where to create the project files
691
#  --out, -o <var>    variable to store the path to
692
# return:
693
#  -  0: no error
694
#  -  1: warning: function aborted by user
695
#  - -1: error: unexpected user input
696
function createAllProjects {
697
  # parse arguments
698
  local arguments=$(getopt -l path:,out: -o p:o: -- "$@")
699
  if [ $? != 0 ]; then
700
    printError "could not interprete arguments."
701
    return -1
702
  fi
703
  eval set -- "$arguments"
704

    
705
  local projectdir=""
706
  local outvar=""
707

    
708
  # handle command line arguments
709
  while [ true ]; do
710
    case $1 in
711
      --path|-p)
712
        projectdir="$2"; shift 2
713
        ;;
714
      --out|-o)
715
        outvar=$2; shift 2
716
        ;;
717
      --) # end of options reached
718
        shift; break
719
        ;;
720
      *) # sanity check (exit with error)
721
        printError "unexpected argument: $1\n"; return 1
722
        ;;
723
    esac
724
  done
725

    
726
  # print message
727
  printInfo "creating QtCreator projects files for all modules\n"
728

    
729
  # read project directory if required
730
  if [ -z "$projectdir" ]; then
731
    getProjectDir projectdir
732
  fi
733

    
734
  # create projects
735
  createLightRingProject --path="$projectdir"
736
  createPowerManagementProject --path="$projectdir"
737
  createDiWheelDriveProject --path="$projectdir"
738

    
739
  # store the path to the output variable, if required
740
  if [ ! -z "$outvar" ]; then
741
    eval $outvar="$projectdir"
742
  fi
743

    
744
  return 0
745
}
746

    
747
################################################################################
748
# SCRIPT MAIN FUNCTION                                                         #
749
################################################################################
750

    
751
### main function of this script
752
# arguments: see help text
753
# return: error code
754
#   - 0: no error
755
#   - 1: error
756
function main {
757

    
758
  # parse arguments
759
  local arguments=$(getopt -l help,clean,wipe,LightRing,PowerManagement,DiWheelDrive,All,quit,log:,LOG:,noinfo -o hcwLPDAq -- "$@")
760
  if [ $? != 0 ]; then
761
    printError "could not interprete arguments."
762
    exit 1
763
  fi
764
  eval set -- "$arguments"
765

    
766
  # print welcome/info text if not suppressed
767
  if [[ $@ != *"--noinfo"* ]]; then
768
    printWelcomeText
769
  else
770
    printf "######################################################################\n"
771
  fi
772
  printf "\n"
773

    
774
  # set log file if specified
775
  if [[ $@ == *--log* || $@ == *--LOG* ]]; then
776
    # get the parameter (file name)
777
    local cmdidx=1
778
    while [ "${!cmdidx}" != "--log" ] && [ "${!cmdidx}" != "--LOG" ]; do
779
      cmdidx=$[cmdidx + 1]
780
    done
781
    local filenameidx=$[cmdidx + 1]
782
    # optionally force silent appending
783
    if [ "${!cmdidx}" == "--LOG" ]; then
784
      setLogFile --file="${!filenameidx}" --option=A
785
    else
786
      setLogFile --file="${!filenameidx}"
787
      printf "\n"
788
    fi
789
  fi
790

    
791
  # log script name
792
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
793

    
794
  # if --help or -h was specified, print the help text and exit
795
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
796
    printHelp
797
    printf "\n"
798
    exitScript
799
  fi
800

    
801
  # handle command line arguments
802
  local projectfilesdir=""
803
  while [ true ]; do
804
    case $1 in
805
      --help|-h) # already handled; ignore
806
        shift 1;
807
        ;;
808
      --clean|-c)
809
        if [ -z "$projectfilesdir" ]; then
810
          deleteProjects --out=projectfilesdir
811
        else
812
          deleteProjects --path="$projectfilesdir"
813
        fi
814
        printf "\n"
815
        shift 1
816
        ;;
817
      --wipe|-w)
818
          if [ -z "$projectfilesdir" ]; then
819
          deleteProjects --out=projectfilesdir
820
        else
821
          deleteProjects --wipe --path="$projectfilesdir"
822
        fi
823
        printf "\n"
824
        shift 1
825
        ;;
826
      --LightRing|-L)
827
        if [ -z "$projectfilesdir" ]; then
828
          createLightRingProject --out=projectfilesdir
829
        else
830
          createLightRingProject --path="$projectfilesdir"
831
        fi
832
        printf "\n"
833
        shift 1
834
        ;;
835
      --PowerManagement|-P)
836
        if [ -z "$projectfilesdir" ]; then
837
          createPowerManagementProject --out=projectfilesdir
838
        else
839
          createPowerManagementProject --path="$projectfilesdir"
840
        fi
841
        printf "\n"
842
        shift 1
843
        ;;
844
      --DiWheelDrive|-D)
845
        if [ -z "$projectfilesdir" ]; then
846
          createDiWheelDriveProject --out=projectfilesdir
847
        else
848
          createDiWheelDriveProject --path="$projectfilesdir"
849
        fi
850
        printf "\n"
851
        shift 1
852
        ;;
853
      --All|-A)
854
        if [ -z "$projectfilesdir" ]; then
855
          createAllProjects --out=projectfilesdir
856
        else
857
          createAllProjects --path="$projectfilesdir"
858
        fi
859
        printf "\n"
860
        shift 1
861
        ;;
862
      --quit|-q)
863
        quitScript
864
        ;;
865
      --log|--LOG) # already handled; ignore
866
        shift 2
867
        ;;
868
      --noinfo) # already processed; ignore
869
        shift 1
870
        ;;
871
      --) # end of options reached
872
        shift; break
873
        ;;
874
      *) # sanity check (exit with error)
875
        printError "unexpected argument: $1\n"; exit -1
876
        ;;
877
    esac
878
  done
879

    
880
  # interactive menu
881
  while [ true ]; do
882
    # main menu info prompt and selection
883
    printInfo "QtCreator setup main menu\n"
884
    printf "Please select one of the following actions:\n"
885
    printf "  [C] - clean project files\n"
886
    printf "  [W] - wipe project and .user files\n"
887
    printf "  [L] - create a project for the LightRing module\n"
888
    printf "  [P] - create a project for the PowerManagement module\n"
889
    printf "  [D] - create a project for the DiWheelDrive module\n"
890
    printf "  [A] - create a project for all modules\n"
891
    printf "  [Q] - quit this setup\n"
892
    readUserInput --num-chars=1 --options="CcWwLlPpDdAaQq" --out=userinput
893
    printf "\n"
894

    
895
    # evaluate user selection
896
    case $userinput in
897
      C|c)
898
        deleteProjects; printf "\n"
899
        ;;
900
      W|w)
901
        deleteProjects --wipe; printf "\n"
902
        ;;
903
      L|l)
904
        createLightRingProject; printf "\n"
905
        ;;
906
      P|p)
907
        createPowerManagementProject; printf "\n"
908
        ;;
909
      D|d)
910
        createDiWheelDriveProject; printf "\n"
911
        ;;
912
      A|a)
913
        createAllProjects; printf "\n"
914
        ;;
915
      Q|q)
916
        quitScript
917
        ;;
918
      *) # sanity check (exit with error)
919
        printError "unexpected argument: $userinput\n"; exit -1
920
        ;;
921
    esac
922
  done
923

    
924
  exit 0
925
}
926

    
927
################################################################################
928
# SCRIPT ENTRY POINT                                                           #
929
################################################################################
930

    
931
main "$@"
932