From 3a07654752375085a88c9585d5e247a8d16dfc44 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 22 Oct 2015 10:53:09 -0400 Subject: [PATCH] misc: add `nit_env.sh` to auto-magically configure shell environements Signed-off-by: Jean Privat --- misc/nit_env.sh | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 misc/nit_env.sh diff --git a/misc/nit_env.sh b/misc/nit_env.sh new file mode 100644 index 0000000..d0c1c0a --- /dev/null +++ b/misc/nit_env.sh @@ -0,0 +1,126 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Source this script inside a bash or sh session to setup: +# +# * PATH +# * MANPATH +# * bash_completion +# +# If `install` is given as an argument, then the script sourcing is automatically inserted in the `.profile`. + +# Final function that sets up the shell environment. +__nit_env() { + #echo "Install in $1:" + + local str="$1/bin" + echo "$PATH" | grep -q "$str" || export PATH="$str:$PATH" + str="$1/share/bin" + echo "$MANPATH" | grep -q "$str" || export MANPATH="$str:$MANPATH" + + if [ -n "$BASH" -a -n "$PS1" ]; then + . "$1/misc/bash_completion/nit" + fi + +} + +# Fast setup when invoked from .profile (see `install` at the end) +# +# Because [d]ash does not support argument to source (`.`), +# we pass the root dir as a local environment variable. +if [ -n "$NIT_DIR" ]; then + __nit_env "$NIT_DIR" + return 2> /dev/null # `return` fails if invoked as is; i.e. not trough source (`.`) + exit +fi + +# Guess the nit root directory and return it in `dirname`. +__nit_env_guess() { + dirname=$NIT_DIR + [ -f "$dirname/src/nitc.nit" ] && return 0 + + dirname=$PWD + [ -f "$dirname/src/nitc.nit" ] && return 0 + + if [ -n "$BASH_SOURCE" ]; then + dirname=`dirname "$BASH_SOURCE"`/.. + [ -f "$dirname/src/nitc.nit" ] && return 0 + fi + + dirname=`dirname "$0"`/.. + [ -f "$dirname/src/nitc.nit" ] && return 0 + + echo "Cannot find the Nit root directory. Run the script from the Nit root directory." + return 1 +} + +# Main method of the script. +# +# A function is used so that `return` have a sane effect +__nit_env_main() { + local install + local dirname + + # Install required? + if [ "$1" = "install" ]; then + install=true + shift + fi + + # Get the root directory + case "$#" in + 1) + # Get it from the argument + dirname=$1 + if [ ! -f "$dirname/src/nitc.nit" ]; then + if [ -d "$dirname/" ]; then + echo "$dirname: not a Nit root directory" + else + echo "$dirname: not a directory" + fi + return 1 + fi + ;; + 0) + # Guess it + __nit_env_guess || return 1 + ;; + *) echo "usage: source nit_env.sh [install] [path/to/nitdir]" + return 1 + ;; + esac + + # Get a canonical path + local ret=`pwd` + cd $dirname > /dev/null + local fulldir=`pwd` + cd "$ret" + + # Check misuse + if [ -n "$BASH" -a "$0" = "$BASH_SOURCE" ]; then + echo "nit_env.sh should be sourced not executed as is. E.g. run with 'source ./nit_env.sh'" + fi + + # Setup the environment + __nit_env "$fulldir" + + # Register in the .profile if required + if [ -n "$install" ]; then + local ne="$fulldir/misc/nit_env.sh" + local str="test -r \"$ne\" && NIT_DIR=\"$fulldir\" . \"$ne\"" + grep -q "$str" "$HOME/.profile" || echo "$str" >> "$HOME/.profile" + fi +} + +__nit_env_main "$@" -- 1.7.9.5