Revision 316a2b34

View differences:

compiler/GCC/gccsetup.sh
309 309
  printf "              Print this help text.\n"
310 310
  printf "          -i, --install\n"
311 311
  printf "              Install another version.\n"
312
  printf "          -u, --uninstall\n"
313
  printf "              Unistall a version.\n"
312 314
  printf "          -c, --change\n"
313 315
  printf "              Change the default version.\n"
314 316
  printf "          -q, --quit\n"
......
320 322
### detect installed versions ##################################################
321 323
# Detect all installed version of arm-none-eabi-gcc, if any.
322 324
#
323
# usage:      detectInstalledVersions <binarray> <current>
325
# usage:      detectInstalledVersions <binarray> <current> [<current_idx>]
324 326
# arguments:  <binarray>
325 327
#                 Array variable to store all detected binary paths to. 
326 328
#             <current>
327 329
#                 Variable to store the currently active binary to.
330
#             <current_idx>
331
#                 Index of the curretly selected version in the output array (<binarray>).
328 332
# return:     n/a
329 333
#
330 334
function detectInstalledVersions {
......
341 345
    armgcc_commanddir=$(dirname $armgcc_command)
342 346
    armgcc_currentbin=$armgcc_command
343 347
    while [ -L $armgcc_currentbin ]; do
344
      armgcc_currentbin=$(readlink $armgcc_currentbin)
348
      # differentiate between relative and absolute paths
349
      if [[ $(readlink $armgcc_currentbin) = /* ]]; then
350
        armgcc_currentbin=$(readlink $armgcc_currentbin)
351
      else
352
        armgcc_currentbin=$(realpath $(dirname $armgcc_currentbin)/$(readlink $armgcc_currentbin))
353
      fi
345 354
    done
346 355
    # the installation location is assumed to be two directories up
347
    armgcc_installdir=$(realpath $(dirname ${armgcc_currentbin})/../../)
356
    armgcc_installdir=$(realpath $(dirname ${armgcc_currentbin})/../..)
348 357
    # list all detected instalations
349 358
    for dir in $(ls -d ${armgcc_installdir}/*/); do
350 359
      if [ -f ${dir}/bin/arm-none-eabi-gcc ]; then
......
352 361
        armgcc_bincnt=$((armgcc_bincnt + 1))
353 362
      fi
354 363
    done
355
  fi
356 364

  
357
  eval "$1=(${armgcc_bins[*]})"
358
  eval $2="$armgcc_currentbin"
365
    # set the output variables
366
    eval "$1=(${armgcc_bins[*]})"
367
    eval $2="$armgcc_currentbin"
368
    if [ -n "$3" ]; then
369
      for (( bin=0; bin<${#armgcc_bins[@]}; ++bin )); do
370
        if [ ${armgcc_bins[bin]} = "$armgcc_currentbin" ]; then
371
          eval $3=$bin
372
        fi
373
      done
374
    fi
375
  else
376
    eval "$1=()"
377
    eval $2=""
378
    if [ -n "$3" ]; then
379
      eval $3=""
380
    fi
381
  fi
359 382
}
360 383

  
361 384
### install new version ########################################################
......
401 424
  # read download URL form user
402 425
  printLog "read installation url from user\n"
403 426
  local armgcc_downloadurl=""
404
  while [[ "$armgcc_downloadurl" != *".tar.bz2" ]]; do
427
  while [ -z "$armgcc_downloadurl" ]; do
405 428
    read -p "Download link for the installation file: " -e armgcc_downloadurl
429
    if [ -z "$armgcc_downloadurl" ]; then
430
      printWarning "installation aborted by user\n"
431
      return 1
432
    fi
406 433
    if [[ $armgcc_downloadurl != *".tar.bz2" ]]; then
407 434
      printWarning "please specify a .tar.bz2 file\n"
435
      armgcc_downloadurl=""
436
    fi
437
    if [ ! wget --spider $armgcc_downloadurl 2>/dev/null ]; then
438
      printWarning "$armgcc_downloadurl can not be reached\n"
439
      armgcc_downloadurl=""
408 440
    fi
409 441
  done
410 442
  printLog "user selected $armgcc_downloadurl\n"
......
512 544
        # don't do anything if the line is already present
513 545
        local bashrc_entrylines=$(grep -x -n "$bashrc_entry" $bashrc_file | cut -f1 -d:) # string of line numbers
514 546
        bashrc_entrylines=(${bashrc_entrylines//"\n"/" "}) # array of line numbers
515
        printf "$bashrc_entrylines\n"
516 547
        if [[ ${#bashrc_entrylines[@]} = 0 ]]; then
517 548
          # insert the entry before the closing identifier
518 549
          sed -i "${bashrc_idlines[1]}"'i'"$bashrc_entry" $bashrc_file
......
560 591
  return 0
561 592
}
562 593

  
594
### uninstall a version ########################################################
595
# Select an installed version and uninstall it from the system.
596
#
597
# usage:      uninstallVersion <versions> <current_idx> <linkdir>
598
# arguments:  <version>
599
#                 Array of available versions (full path to binary).
600
#             <current_idx>
601
#                 Index of the currently selected version in the array.
602
#             <linkdir>
603
#                 Path where to delete old links.
604
# return:     0
605
#                 No error or warning occurred.
606
#             1
607
#                 Warning: Installation aborted by user.
608
#             -1
609
#                 Error: An exception occurred.
610
#
611
function uninstallVersion {
612
  local versions=("${!1}")
613
  local current_idx="$2"
614
  local linkdir="$3"
615

  
616
  # check whether at least two installations were detected
617
  if [ ${#versions[@]} -eq 0 ]; then
618
    printError "no installation detected\n"
619
    return -1
620
  else
621
    # print all available versions
622
    printInfo "choose the installation to uninstall to or type 'A' to abort:\n"
623
    for (( cnt=0; cnt<${#versions[@]}; ++cnt )); do
624
      if [ $cnt -eq $current_idx ]; then
625
        printf "*%3u: %s\n" $(($cnt + 1)) ${versions[$cnt]}
626
      else
627
        printf " %3u: %s\n" $(($cnt + 1)) ${versions[$cnt]}
628
      fi
629
    done
630

  
631
    # read user selection
632
    printLog "read user slection\n"
633
    local userinput=""
634
    while [ -z $userinput ] ; do
635
      read -p "your selection: " -e userinput
636
      printLog "user selection: $userinput\n"
637
      if [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#versions[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; then
638
        printWarning "Please enter an integer between 1 and ${#versions[@]} or 'A' to abort.\n"
639
        userinput=""
640
      fi
641
      if [ ${#versions[@]} -gt 1 ] && [ $((userinput - 1)) -eq $current_idx ]; then
642
        printWarning "Unable to uninstall currently selected version (as long as there are others).\n"
643
        userinput=""
644
      fi
645
    done
646

  
647
    if [[ "$userinput" =~ ^[Aa]$ ]]; then
648
      printWarning "aborted by user\n"
649
      return 1
650
    else
651
      local idx=$((userinput - 1))
652
      printf "\n"
653
      # prompt selected and aks user for confirmation
654
      printInfo "${versions[$idx]} will be removed. Continue? [y/n]\n"
655
      readUserInput "YyNn" userinput
656
      case "$userinput" in
657
        Y|y)
658
          ;;
659
        N|n)
660
          printWarning "uninstallation process aborted by user\n"
661
          return 1
662
          ;;
663
        *) # sanity check (exit with error)
664
          printError "invalid option: $userinput\n"
665
          return -1
666
          ;;
667
      esac
668
      # find and delete any links pointing to the version to be deleted
669
      for link in `find $linkdir -maxdepth 1 -type l`; do
670
        local l=$link
671
        # follow the link to the actual binary
672
        while [ -L $l ]; do
673
          # differentiate between relative and absolute paths
674
          if [[ $(readlink $l) = /* ]]; then
675
            l=$(readlink $l)
676
          else
677
            l=$(realpath $(dirname $l)/$(readlink $l))
678
          fi
679
        done
680
        # delete the link if it points to the version to be uninstalled
681
        if [ $(dirname $l) == $(dirname ${versions[$idx]}) ]; then
682
          rm $link
683
        fi
684
      done
685
      # delete the version directory (assumed to be one directory up)
686
      rm -rf $(realpath $(dirname ${versions[$idx]})/..)
687
      printInfo "${versions[$idx]} has been removed.\n"
688
    fi
689
  fi
690

  
691
  return 0
692
}
693

  
563 694
### change default version #####################################################
564 695
# Change the default arm-none-eabi-gcc version.
565 696
#
566
# usage:      installNewVersion <versions> <linkdir>
697
# usage:      changeDefaultVersion <versions> <linkdir>
567 698
# argumenst:  <versions>
568 699
#                 Array of available versions (full path to binary).
569 700
#             <linkdir>
......
669 800
  # detect installed versions and inform user
670 801
  local installedversions=()
671 802
  local currentversion=""
672
  detectInstalledVersions installedversions currentversion
803
  local currentversionidx="n/a"
804
  detectInstalledVersions installedversions currentversion currentversionidx
673 805
  case "${#installedversions[@]}" in
674 806
    0)
675 807
      printInfo "no installation has been detected\n";;
......
694 826
      case "$1" in
695 827
        -h|--help) # already handled; ignore
696 828
          shift 1;;
697
        -i|--init)
829
        -i|--install)
698 830
          if [ -z "$currentversion" ]; then
699 831
            installNewVersion
700 832
          else
701
            installNewVersion --install=$(realpath $(dirname "$currentversion")/../../) --link=$(realpath $(dirname "$currentversion")/../../)
833
            installNewVersion --install=$(realpath $(dirname $currentversion)/../..) --link=$(realpath $(dirname $currentversion)/../..)
834
          fi
835
          detectInstalledVersions installedversions currentversion currentversionidx
836
          printf "\n"; shift 1;;
837
        -u|--uninstall)
838
          if [ ! -z "$currentversion" ]; then
839
            uninstallVersion installedversions[@] $currentversionidx $(realpath $(dirname $currentversion)/../..)
840
            detectInstalledVersions installedversions currentversion currentversionidx
841
          else
842
            printError "no installation detected\n"
702 843
          fi
703 844
          printf "\n"; shift 1;;
704 845
        -c|--change)
705
          changeDefaultVersion installedversions[@] $(realpath $(dirname "$currentversion")/../../); printf "\n"; shift 1;;
846
          if [ ! -z "$currentversion" ]; then
847
            changeDefaultVersion installedversions[@] $(realpath $(dirname $currentversion)/../..)
848
          else
849
            printError "no installation detected\n"
850
          fi
851
          printf "\n"; shift 1;;
706 852
        -q|--quit)
707 853
          quitScript; shift 1;;
708 854
        --log=*|--LOG=*) # already handled; ignore
......
726 872
    printInfo "GCC setup main menu\n"
727 873
    printf "Please select one of the following actions:\n"
728 874
    printf "  [I] - install another version\n"
875
    printf "  [U] - uninstall a version\n"
729 876
    printf "  [C] - change default version\n"
730 877
    printf "  [Q] - quit this setup\n"
731 878
    local userinput=""
732
    readUserInput "IiCcQq" userinput
879
    readUserInput "IiUuCcQq" userinput
733 880
    printf "\n"
734 881

  
735 882
    # evaluate user selection
......
738 885
        if [ -z "$currentversion" ]; then
739 886
          installNewVersion
740 887
        else
741
          installNewVersion --install=$(realpath $(dirname "$currentversion")/../../) --link=$(realpath $(dirname "$currentversion")/../../)
888
          installNewVersion --install=$(realpath $(dirname $currentversion)/../..) --link=$(realpath $(dirname $currentversion)/../..)
889
        fi
890
        detectInstalledVersions installedversions currentversion currentversionidx
891
        printf "\n";;
892
      U|u)
893
        if [ ! -z "$currentversion" ]; then
894
          uninstallVersion installedversions[@] $currentversionidx $(realpath $(dirname $currentversion)/../..)
895
          detectInstalledVersions installedversions currentversion currentversionidx
896
        else
897
          printError "no installation detected\n"
742 898
        fi
743 899
        printf "\n";;
744 900
      C|c)
745
        changeDefaultVersion installedversions[@] $(realpath $(dirname "$currentversion")/../../); printf "\n";;
901
        if [ ! -z "$currentversion" ]; then
902
          changeDefaultVersion installedversions[@] $(realpath $(dirname $currentversion)/../..)
903
        else
904
          printError "no installation detected\n"
905
        fi
906
        printf "\n";;
746 907
      Q|q)
747 908
        quitScript;;
748 909
      *) # sanity check (exit with error)

Also available in: Unified diff