summaryrefslogtreecommitdiff
path: root/packages/cern-scripts/src/main/resources/lib/activate.inc.sh
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2016-01-28 00:14:30 +0100
committerYves Fischer <yvesf-git@xapek.org>2016-01-28 00:14:30 +0100
commit7038eec15db5db3539d13982a9a19724d6b614b2 (patch)
treea5e6e2d6fea4a61a2967127656aa21cec94d88b8 /packages/cern-scripts/src/main/resources/lib/activate.inc.sh
parentb0a61ca5d2339d9087ee1649d1503ccc31ba9b0a (diff)
downloaddebian-packages-7038eec15db5db3539d13982a9a19724d6b614b2.tar.gz
debian-packages-7038eec15db5db3539d13982a9a19724d6b614b2.zip
move env files
Diffstat (limited to 'packages/cern-scripts/src/main/resources/lib/activate.inc.sh')
-rw-r--r--packages/cern-scripts/src/main/resources/lib/activate.inc.sh68
1 files changed, 68 insertions, 0 deletions
diff --git a/packages/cern-scripts/src/main/resources/lib/activate.inc.sh b/packages/cern-scripts/src/main/resources/lib/activate.inc.sh
new file mode 100644
index 0000000..79bed6b
--- /dev/null
+++ b/packages/cern-scripts/src/main/resources/lib/activate.inc.sh
@@ -0,0 +1,68 @@
+
+# non-interactive activate
+ __activate_env() {
+ for func in $(typeset -F | cut -f 3 -d ' ' | grep -E '^__env_'); do
+ if [[ "$func" == "__env_$1" ]]; then
+ $func
+ echo "Activated $1"
+ return 0
+ fi
+ done
+ echo "Error: Environment not found $1" >&2
+ return 1
+}
+
+# print all defined envs
+list_envs() {
+ for env in $(typeset -F | sed -n -e 's/^declare -f __env_//p'); do
+ active=false
+ for env_active in $ENV_ACTIVE; do
+ if [ "$env_active" == "$env" ]; then
+ active=true
+ fi
+ done
+ if $active; then
+ echo -n "${env}* "
+ else
+ echo -n "$env "
+ fi
+ done
+ echo ""
+}
+
+plain_list_envs() {
+ typeset -F | sed -n -e 's/^declare -f __env_//p'
+}
+
+# Interactive activate
+activate() {
+ err=false
+ for env in $*; do
+ if ! typeset -F | grep -q -E "^declare -f __env_${env}$"; then
+ echo "Environment not defined: $env"
+ err=true
+ fi
+ done
+ if $err; then
+ return 1
+ fi
+
+ for env in $*; do
+ if typeset -F | grep -q -E "^declare -f __env_${env}$"; then
+ export ENV_ACTIVE="$ENV_ACTIVE${ENV_ACTIVE:+ }$env"
+ __env_${env}
+ fi
+ done
+ echo "Active: $ENV_ACTIVE"
+}
+
+# initialization
+for env_request in $ENV_ACTIVE; do
+ __activate_env $env_request
+done
+
+# bash-completion for `activate'
+_activate() {
+ _init_completion -s || return
+ COMPREPLY=( $(compgen -W "$(list_envs)" -- $cur) )
+} && complete -F _activate activate