unified EDITOR variable setup
[feisty_meow.git] / scripts / core / variables.sh
1 #!/bin/bash
2
3 ##############
4
5 # variables script:
6 #   Defines the environment variables used by the personalized unix
7 #   environment.
8 # Author: Chris Koeritz
9
10 ##############
11
12 #hmmm: moved from functions.sh; does that hose everything up?
13
14   # defines a variable within the feisty meow environment and remembers that
15   # this is a new or modified definition.  if the feisty meow codebase is
16   # unloaded, then so are all the variables that were defined.
17   # this function always exports the variables it defines.
18   function define_yeti_variable()
19   {
20     #hmmm: simple implem just sets it up and exports the variable.
21     #  i.e., this method always exports.
22     export "${@}" 
23
24 #hmmm: eventual approach-- if variable exists already, save old value for restore,
25 # otherwise save null value for restore,
26 # have to handle unsetting if there was no prior value of one
27 # we newly defined.
28 # add variable name to a list of feisty defined variables.
29
30 return 0
31   }
32
33
34 ##############
35
36 # this section should always run or bash will reset them on us.
37 # these need to be as minimal as possible.
38
39 # sets the main prompt to a simple default, with user@host.
40 define_yeti_variable PS1='\u@\h $ ';
41 # sets the history length and max file size so we can get some long history around here.
42 define_yeti_variable HISTSIZE=1000000
43 define_yeti_variable HISTFILESIZE=8000000
44
45 # make the TERM available to all sub-shells.
46 define_yeti_variable TERM
47   
48 ##############
49   
50 # we'll run this again only if we think it's needed.
51 if [ -z "$CORE_VARIABLES_LOADED" ]; then
52
53   if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then echo variables initialization begins...; fi
54   
55   ##############
56   
57   # start with some simpler things.
58   
59 #hmmm: this needs to come from some configuration item.  especially for installs.
60 define_yeti_variable DEFAULT_FEISTYMEOW_ORG_DIR=/opt/feistymeow.org
61
62   define_yeti_variable SCRIPT_SYSTEM=feisty_meow
63   
64   # OS variable records the operating system we think we found.
65   if [ -z "$OS" ]; then
66     define_yeti_variable OS=UNIX
67   fi
68   define_yeti_variable IS_DARWIN=$(echo $OSTYPE | grep -i darwin)
69   
70   ##############
71
72   # guess the current platform.
73   IS_UNIX=$(uname | grep -i linux)
74   if [ -z "$IS_UNIX" ]; then IS_UNIX=$(uname | grep -i unix); fi
75   if [ -z "$IS_UNIX" ]; then IS_UNIX=$(uname | grep -i darwin); fi
76   IS_DOS=$(uname | grep -i ming)
77   if [ -z "$IS_DOS" ]; then IS_DOS=$(uname | grep -i cygwin); fi
78
79   # now if we're stuck in DOS, try to determine the type of system.
80   if [ ! -z "$IS_DOS" ]; then
81     # IS_MSYS will be non-empty if this is the msys toolset.  otherwise
82     # we assume that it's cygwin.
83     IS_MSYS=$(uname | grep -i ming)
84
85     # if not MSYS, then we'll assume cygwin and set the cygwin root var.
86     if [ -z "$IS_MSYS" ]; then
87       define_yeti_variable CYGROOT=$(cygpath -w -m /)
88     fi
89   fi
90
91   ##############
92   
93   # fallbacks to set crucial variables for feisty meow...
94   
95   # set the main root directory variable for the feisty meow codebase.
96   # this is only used for extreme failure modes, when the values were not
97   # pulled in from our auto-generated config.
98   if [ -z "$FEISTY_MEOW_APEX" ]; then
99     if [ -d "/opt/feistymeow.org/feisty_meow" ]; then
100       define_yeti_variable FEISTY_MEOW_APEX="/opt/feistymeow.org/feisty_meow"
101       define_yeti_variable FEISTY_MEOW_SCRIPTS="$FEISTY_MEOW_APEX/scripts"
102     elif [ -d "$HOME/feisty_meow" ]; then
103       define_yeti_variable FEISTY_MEOW_APEX="$HOME/feisty_meow"
104       define_yeti_variable FEISTY_MEOW_SCRIPTS="$FEISTY_MEOW_APEX/scripts"
105     fi
106   fi
107
108   # main declaration of the transients area.
109   if [ -z "$TMP" ]; then
110     define_yeti_variable TMP=$HOME/.tmp
111   fi
112
113   # set up our event logging file for any notable situation to be recorded in.
114   if [ -z "$FEISTY_MEOW_EVENT_LOG" ]; then
115     define_yeti_variable FEISTY_MEOW_EVENT_LOG="$TMP/$USER-feisty_meow-events.log"
116   fi
117
118   # set up the top-level for all build creations and logs and such.
119   if [ -z "$FEISTY_MEOW_GENERATED_STORE" ]; then
120     define_yeti_variable FEISTY_MEOW_GENERATED_STORE="$TMP/generated-feisty_meow"
121   fi
122   if [ ! -d "$FEISTY_MEOW_GENERATED_STORE" ]; then
123     mkdir -p "$FEISTY_MEOW_GENERATED_STORE"
124   fi
125   # set up our effluent outsourcing valves.
126   if [ -z "$TEMPORARIES_PILE" ]; then
127     define_yeti_variable TEMPORARIES_PILE="$FEISTY_MEOW_GENERATED_STORE/temporaries"
128   fi
129   if [ ! -d "$TEMPORARIES_PILE" ]; then
130     mkdir -p "$TEMPORARIES_PILE"
131   fi
132
133   # similarly, make sure we have someplace to look for our generated files, if
134   # we were not handed a value.
135   if [ -z "$FEISTY_MEOW_LOADING_DOCK" ]; then
136     # The generated scripts directory is where automatically generated files live.
137     # It is separate from the main body of the shell scripts in order to keep things from
138     # exploding.
139     define_yeti_variable FEISTY_MEOW_LOADING_DOCK=$HOME/.zz_feisty_loading
140   fi
141   
142   ##############
143
144   # set up the color_add variable which is a flag that lets ls know colors work.
145
146   # test if we can use color in ls...
147   ls --help 2>&1 | grep -i -q color
148   if [ $? -eq 0 ]; then
149     export color_add='--color=auto'
150   else
151     export color_add=
152   fi
153   unset test_ls_colors
154   
155   ##############
156
157   # umask sets a permission mask for all file creations.  we don't set this for the users any
158   # more; they should set it themselves.  this is just documentation.
159   # 
160   # this mask disallows writes by the "group" and disallows all permissions for "others".
161   #umask 027
162   # this mask disallows writes by "group" and "others".
163   #umask 022
164   # this mask allows writes by "group" but not by "others".
165   #umask 002
166
167   # ulimit sets user limits.  we set the maximum allowed core dump file size
168   # to zero, because it is obnoxious to see the core dumps from crashed
169   # programs lying around everywhere.
170   ulimit -c 0
171   
172   ##############
173   
174   # user variables, sort of...  if they haven't given themselves a name yet,
175   # then we will make one up for them.
176   
177   # define a default name, if one wasn't already set.
178   if [ -z "$NAME" ]; then
179     define_yeti_variable NAME='Unset Q. Namington, Fixley Your Name III'
180   fi
181   
182   ##############
183   
184   # variables for perl.
185   
186   define_yeti_variable PERLLIB+="/usr/lib/perl5"
187   if [ "$OS" == "Windows_NT" ]; then
188     define_yeti_variable PERLIO=:perlio
189       # choose perl's IO over the ms-windows version so we can handle file
190       # bytes properly.
191   fi
192
193   # iterate across our sub-directories and find the perl scripts.
194   # this currently only looks one level down.
195   for i in $FEISTY_MEOW_SCRIPTS/*; do
196     if [ -d "$i" ]; then
197       # check if there is a perl file present; add the folder to PERLLIB if so.
198       ls $i/*.pl &>/dev/null
199       if [ $? -eq 0 ]; then
200         PERLLIB+=":$(dos_to_unix_path $i)"
201       fi
202     fi
203   done
204   define_yeti_variable PERL5LIB=$PERLLIB
205   #echo PERLLIB is now $PERLLIB
206   
207   ##############
208   
209   # set this so nechung can find its data.
210   define_yeti_variable NECHUNG=$FEISTY_MEOW_APEX/infobase/fortunes.dat
211   
212 ##  # establish a pipe for less to see our beloved syntax highlighting.
213 ##  define_yeti_variable LESSOPEN="| source-highlight -f esc -o STDOUT -i %s"
214
215   # the base checkout list is just to update feisty_meow.  additional folder
216   # names can be added in your customized scripts.  the space at the end of
217   # this variable is important and allows users to extend the list like:
218   #    define_yeti_variable REPOSITORY_DIR+="muppets configs"
219   # see the customize/fred folder for a live example.
220   define_yeti_variable REPOSITORY_LIST="$FEISTY_MEOW_APEX "
221
222   # add in any active projects to the repository list.
223   if [ -d "$HOME/active" ]; then
224     REPOSITORY_LIST+="$(find "$HOME/active" -maxdepth 1 -mindepth 1 -type d) "
225   fi
226   # add in any site avenger applications that are in the applications folder.
227   if [ -d "$HOME/apps" ]; then
228     # general search for normal project folders in apps.
229     REPOSITORY_LIST+="$(find "$HOME/apps" -maxdepth 2 -mindepth 2 -iname ".git" -type d -exec dirname {} ';') "
230     REPOSITORY_LIST+="$(find "$HOME/apps" -maxdepth 2 -mindepth 2 -iname ".svn" -type d -exec dirname {} ';') "
231
232     # special search for site avenger directories; they have avenger5 as second level.
233     REPOSITORY_LIST+="$(find "$HOME/apps" -maxdepth 2 -mindepth 2 -iname "avenger5" -type d) "
234   fi
235   
236   # the archive list is a set of directories that are major repositories of
237   # data which can be synched to backup drives.
238   define_yeti_variable MAJOR_ARCHIVE_SOURCES=
239   # the source collections list is a set of directories that indicate they
240   # harbor a lot of source code underneath.
241   define_yeti_variable SOURCECODE_HIERARCHY_LIST=
242
243   # initializes the feisty meow build variables, if possible.
244   function initialize_build_variables()
245   {
246     found_build_vars=0
247     # we need to know the feisty meow directory, or we bail.
248     if [ -z "$FEISTY_MEOW_APEX" ]; then return; fi
249     # pick from our expected generator folder, but make sure it's there...
250     buildvars="$FEISTY_MEOW_SCRIPTS/generator/build_variables.sh"
251     if [ -f "$buildvars" ]; then
252       # yep, that one looks good, so pull in the build defs.
253       source "$buildvars" "$buildvars"
254       found_build_vars=1
255     fi
256     # now augment the environment if we found our build variables.
257     if [ $found_build_vars == 1 ]; then
258       # the binary directory contains our collection of handy programs.
259       define_yeti_variable FEISTY_MEOW_BINARIES=$TARGETS_STORE
260       # add binaries created within build to the path.
261       define_yeti_variable PATH="$(dos_to_unix_path $FEISTY_MEOW_BINARIES):$PATH"
262       # Shared libraries are located via this variable.
263       define_yeti_variable LD_LIBRARY_PATH="$(dos_to_unix_path $LD_LIBRARY_PATH):$(dos_to_unix_path $FEISTY_MEOW_BINARIES)"
264     fi
265   }
266   
267   ##############
268   
269   # windoze specific patching up missing things.
270   
271   if [ "$OS" == "Windows_NT" ]; then
272     define_yeti_variable HOSTNAME=$(echo $HOSTNAME | tr A-Z a-z)
273   fi
274   
275   ##############
276   
277   # load in the build environment.
278   initialize_build_variables
279   
280   ##############
281   
282   # add to the PATH variables used for locating applications.  this step is taken after any
283   # potential overrides from the user.
284   define_yeti_variable PATH="$(dos_to_unix_path $FEISTY_MEOW_LOADING_DOCK):$PATH:$(find /usr/local/games -maxdepth 1 -type d -exec echo -n {}: ';' 2>/dev/null)/sbin"
285   
286   ##############
287
288   # set the SHUNIT_PATH so our shunit tests can find the codebase.
289   define_yeti_variable SHUNIT_PATH="$FEISTY_MEOW_SCRIPTS/shunit"
290   
291   ##############
292
293   define_yeti_variable CORE_VARIABLES_LOADED=true
294   
295   if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then echo variables initialization ends....; fi
296 fi
297
298 ##############
299
300 # pull in the custom overrides for feisty_meow scripts.  this is done almost
301 # last, because we want to set everything up as expected, then let the user
302 # override individual variables and definitions.  we also don't guard this
303 # to avoid running it again, because we don't know what mix of functions and
304 # aliases they want to define in there.
305 for i in $FEISTY_MEOW_LOADING_DOCK/custom/*.sh; do
306   if [ ! -f "$i" ]; then
307     # skip it if it's not real.
308     continue;
309   fi
310   if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then
311     echo "loading customization: $(basename $(dirname $i))/$(basename $i)"
312   fi
313   source "$i"
314 done
315
316 ##############
317
318 # a late breaking action is to set the editor, if we can.
319 # we will fallback to whatever we can find on the host.
320 export EDITOR
321 # note: the editors for revision control must wait while the document is
322 # edited, so gvim and others that launch a separate x window are not
323 # going to work well unless they can be prevented from forking the process
324 # off.
325 if [ -z "$EDITOR" ]; then
326   EDITOR="$(which gvim)"
327   if [ ! -z "$EDITOR" ]; then
328     # if we found gvim, then add in the no forking flag.
329     EDITOR+=" --nofork"
330   fi
331 fi
332 if [ -z "$EDITOR" ]; then
333   EDITOR="$(which vim)"
334 fi
335 if [ -z "$EDITOR" ]; then
336   EDITOR="$(which vi)"
337 fi
338 if [ -z "$EDITOR" ]; then
339   EDITOR="$(which emacs)"
340 fi
341 ####
342 # out of ideas about editors at this point.
343 ####
344 # set the VISUAL and other variables from EDITOR if we found an editor to use.
345 if [ ! -z "$EDITOR" ]; then
346   VISUAL="$EDITOR"
347
348   export GIT_EDITOR="$EDITOR"
349   export SVN_EDITOR="$EDITOR"
350 fi
351
352 ##############
353