Merge: doc: fixed some typos and other misc. corrections
[nit.git] / misc / nit_env.sh
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Source this script inside a bash or sh session to setup:
16 #
17 # * PATH
18 # * MANPATH
19 # * bash_completion
20 #
21 # If `install` is given as an argument, then the script sourcing is automatically inserted in the `.profile`.
22
23 # Final function that sets up the shell environment.
24 __nit_env() {
25 #echo "Install in $1:"
26
27 local str="$1/bin"
28 echo "$PATH" | grep -q "$str" || export PATH="$str:$PATH"
29 str="$1/share/bin"
30 echo "$MANPATH" | grep -q "$str" || export MANPATH="$str:$MANPATH"
31
32 if [ -n "$BASH" -a -n "$PS1" ]; then
33 . "$1/misc/bash_completion/nit"
34 fi
35
36 }
37
38 # Fast setup when invoked from .profile (see `install` at the end)
39 #
40 # Because [d]ash does not support argument to source (`.`),
41 # we pass the root dir as a local environment variable.
42 if [ -n "$NIT_DIR" ]; then
43 __nit_env "$NIT_DIR"
44 return 2> /dev/null # `return` fails if invoked as is; i.e. not trough source (`.`)
45 exit
46 fi
47
48 # Guess the nit root directory and return it in `dirname`.
49 __nit_env_guess() {
50 dirname=$NIT_DIR
51 [ -f "$dirname/src/nitc.nit" ] && return 0
52
53 dirname=$PWD
54 [ -f "$dirname/src/nitc.nit" ] && return 0
55
56 if [ -n "$BASH_SOURCE" ]; then
57 dirname=`dirname "$BASH_SOURCE"`/..
58 [ -f "$dirname/src/nitc.nit" ] && return 0
59 fi
60
61 dirname=`dirname "$0"`/..
62 [ -f "$dirname/src/nitc.nit" ] && return 0
63
64 echo "Cannot find the Nit root directory. Run the script from the Nit root directory."
65 return 1
66 }
67
68 # Main method of the script.
69 #
70 # A function is used so that `return` have a sane effect
71 __nit_env_main() {
72 local install
73 local dirname
74
75 # Install required?
76 if [ "$1" = "install" ]; then
77 install=true
78 shift
79 fi
80
81 # Get the root directory
82 case "$#" in
83 1)
84 # Get it from the argument
85 dirname=$1
86 if [ ! -f "$dirname/src/nitc.nit" ]; then
87 if [ -d "$dirname/" ]; then
88 echo "$dirname: not a Nit root directory"
89 else
90 echo "$dirname: not a directory"
91 fi
92 return 1
93 fi
94 ;;
95 0)
96 # Guess it
97 __nit_env_guess || return 1
98 ;;
99 *) echo "usage: source nit_env.sh [install] [path/to/nitdir]"
100 return 1
101 ;;
102 esac
103
104 # Get a canonical path
105 local ret=`pwd`
106 cd $dirname > /dev/null
107 local fulldir=`pwd`
108 cd "$ret"
109
110 # Check misuse
111 if [ -n "$BASH" -a "$0" = "$BASH_SOURCE" ]; then
112 echo "nit_env.sh should be sourced not executed as is. E.g. run with 'source ./nit_env.sh'"
113 fi
114
115 # Setup the environment
116 __nit_env "$fulldir"
117
118 # Register in the .profile if required
119 if [ -n "$install" ]; then
120 local ne="$fulldir/misc/nit_env.sh"
121 local str="test -r \"$ne\" && NIT_DIR=\"$fulldir\" . \"$ne\""
122 if [ -f "$HOME/.profile" ]; then
123 echo "$str" >> "$HOME/.profile"
124 echo "successfuly registered nit in .profile"
125 elif [ -f "$HOME/.bashrc" ]; then
126 echo "$str" >> "$HOME/.bashrc"
127 echo "successfuly registered nit in .bashrc"
128 elif [ -f "$HOME/.bash_profile" ]; then
129 echo "$str" >> "$HOME/.bash_profile"
130 echo "succesfuly registerd nit .bash_profile"
131 elif [ -f "$HOME/.zshrc" ]; then
132 echo "$str" >> "$HOME/.zshrc"
133 echo "successfuly registered nit in .zshrc"
134 else
135 echo "$str" > "$HOME/.profile"
136 echo "couldn't find .bashrc, .bash_profile, .profile. Created .profile and registered nit in it"
137 fi
138 fi
139 }
140
141 __nit_env_main "$@"