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