|
| 1 | +# Tykky shell functions to activate/deactivate environments |
| 2 | +__tykky_get_env_path() { |
| 3 | + # Get and validate installation path of a tykky environment. |
| 4 | + # If a name is passed as an argument, try to find it in colon-separated |
| 5 | + # list TYKKY_PATH |
| 6 | + |
| 7 | + __candidate="" |
| 8 | + case "$1" in |
| 9 | + */*) |
| 10 | + __candidate="${1%/}" |
| 11 | + ;; |
| 12 | + *) |
| 13 | + oldIFS=$IFS |
| 14 | + IFS=: |
| 15 | + for __tykky_path in ${TYKKY_PATH:-""}; do |
| 16 | + IFS=$oldIFS |
| 17 | + if [ -d "$__tykky_path/$1" ]; then |
| 18 | + if [ -n "$__candidate" ]; then |
| 19 | + echo "WARNING: Additional candidate tykky environments found in TYKKY_PATH for $1, only the first one will be considered" >&2 |
| 20 | + echo -e "\tfirst candidate: $__candidate" >&2 |
| 21 | + echo -e "\tnew candidate: $__tykky_path/$1" >&2 |
| 22 | + else |
| 23 | + __candidate="${__tykky_path%/}/${1%/}" |
| 24 | + fi |
| 25 | + #break |
| 26 | + fi |
| 27 | + done |
| 28 | + if [ -d "$1" ];then |
| 29 | + # If there is a matching foldername in the current directory |
| 30 | + # and the name does not match anyhing in tykky_path, also test that for convinience |
| 31 | + # this is to be consistent with that the first case allows relative paths |
| 32 | + # so then relative paths in the current directory withouth / should also work |
| 33 | + if [ -z "$__candidate" ];then |
| 34 | + __candidate=$PWD/$1 |
| 35 | + |
| 36 | + # Ambiguous reference as we might have multiple matches |
| 37 | + else |
| 38 | + _tp_candidate=$(readlink -f $__candidate) |
| 39 | + _d_candidate=$(readlink -f $1) |
| 40 | + # if same folder let's not emit a warning |
| 41 | + # following pseudo standard -> command line arguments override environment settings |
| 42 | + if [ ! "$_tp_candidate" = "$_d_candidate" ];then |
| 43 | + echo "WARNING: Multiple candidate tykky environments for $1, activating the one in the current directory" >&2 |
| 44 | + echo -e "\tfrom TYKKY_PATH: $_tp_candidate" >&2 |
| 45 | + echo -e "\tfrom current directory: $_d_candidate" >&2 |
| 46 | + fi |
| 47 | + |
| 48 | + fi |
| 49 | + fi |
| 50 | + |
| 51 | + ;; |
| 52 | + esac |
| 53 | + |
| 54 | + # Validation of genunine tykky installation |
| 55 | + if [ -f "$__candidate/common.sh" ] && [ -d "$__candidate/bin" ]; then |
| 56 | + echo "$__candidate" |
| 57 | + unset __candidate __tykky_path |
| 58 | + else |
| 59 | + unset __candidate __tykky_path |
| 60 | + echo "ERROR: $1 is not a valid tykky environment" >&2 |
| 61 | + if [ ! -d "$1" ] ; then |
| 62 | + echo -e "\t$1 does not exists or parent folder is not in TYKKY_PATH" >&2 |
| 63 | + echo -e "\tCurrent value: TYKKY_PATH=$TYKKY_PATH" >&2 |
| 64 | + fi |
| 65 | + false |
| 66 | + fi |
| 67 | + |
| 68 | +} |
0 commit comments