simplified sudo function
[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   define_yeti_variable TEMPORARIES_PILE="$FEISTY_MEOW_GENERATED_STORE/temporaries"
136   if [ ! -d "$TEMPORARIES_PILE" ]; then
137     mkdir -p "$TEMPORARIES_PILE"
138   fi
139
140   # similarly, make sure we have someplace to look for our generated files, if
141   # we were not handed a value.
142   if [ -z "$FEISTY_MEOW_LOADING_DOCK" ]; then
143     # The generated scripts directory is where automatically generated files live.
144     # It is separate from the main body of the shell scripts in order to keep things from
145     # exploding.
146     define_yeti_variable FEISTY_MEOW_LOADING_DOCK=$HOME/.zz_feisty_loading
147   fi
148   
149   ##############
150
151   # set up the color_add variable which is a flag that lets ls know colors work.
152
153   # test if we can use color in ls...
154   ls --help 2>&1 | grep -i -q color
155   if [ $? -eq 0 ]; then
156     export color_add='--color=auto'
157   else
158     export color_add=
159   fi
160   unset test_ls_colors
161   
162   ##############
163
164   # umask sets a permission mask for all file creations.  we don't set this for the users any
165   # more; they should set it themselves.  this is just documentation.
166   # 
167   # this mask disallows writes by the "group" and disallows all permissions for "others".
168   #umask 027
169   # this mask disallows writes by "group" and "others".
170   #umask 022
171   # this mask allows writes by "group" but not by "others".
172   #umask 002
173
174   # ulimit sets user limits.  we set the maximum allowed core dump file size
175   # to zero, because it is obnoxious to see the core dumps from crashed
176   # programs lying around everywhere.
177   ulimit -c 0
178   
179   ##############
180   
181   # user variables, sort of...  if they haven't given themselves a name yet,
182   # then we will make one up for them.
183   
184   # define a default name, if one wasn't already set.
185   if [ -z "$NAME" ]; then
186     define_yeti_variable NAME='Unset Q. Namington, Fixley Your Name III'
187   fi
188   
189   ##############
190   
191   # variables for perl.
192   
193   define_yeti_variable PERLLIB+="/usr/lib/perl5"
194   if [ "$OS" == "Windows_NT" ]; then
195     define_yeti_variable PERLIO=:perlio
196       # choose perl's IO over the ms-windows version so we can handle file
197       # bytes properly.
198   fi
199
200   # iterate across our sub-directories and find the perl scripts.
201   # this currently only looks one level down.
202   for i in $FEISTY_MEOW_SCRIPTS/*; do
203     if [ -d "$i" ]; then
204       # check if there is a perl file present; add the folder to PERLLIB if so.
205       ls $i/*.pl &>/dev/null
206       if [ $? -eq 0 ]; then
207         PERLLIB+=":$(dos_to_unix_path $i)"
208       fi
209     fi
210   done
211   define_yeti_variable PERL5LIB=$PERLLIB
212   #echo PERLLIB is now $PERLLIB
213   
214   ##############
215   
216   # set this so nechung can find its data.
217   define_yeti_variable NECHUNG=$FEISTY_MEOW_APEX/infobase/fortunes.dat
218   
219 ##  # establish a pipe for less to see our beloved syntax highlighting.
220 ##  define_yeti_variable LESSOPEN="| source-highlight -f esc -o STDOUT -i %s"
221
222   # the base checkout list is just to update feisty_meow.  additional folder
223   # names can be added in your customized scripts.  the space at the end of
224   # this variable is important and allows users to extend the list like:
225   #    define_yeti_variable REPOSITORY_DIR+="muppets configs"
226   # see the customize/fred folder for a live example.
227   define_yeti_variable REPOSITORY_LIST="$FEISTY_MEOW_APEX "
228
229   # add in any active projects to the repository list.
230   if [ -d "$HOME/active" ]; then
231     REPOSITORY_LIST+="$(find "$HOME/active" -maxdepth 1 -mindepth 1 -type d) "
232   fi
233   # add in any site avenger applications that are in the applications folder.
234   if [ -d "$HOME/apps" ]; then
235     # general search for normal project folders in apps.
236     REPOSITORY_LIST+="$(find "$HOME/apps" -maxdepth 2 -mindepth 2 -iname ".git" -type d -exec dirname {} ';') "
237     REPOSITORY_LIST+="$(find "$HOME/apps" -maxdepth 2 -mindepth 2 -iname ".svn" -type d -exec dirname {} ';') "
238
239     # special search for site avenger directories; they have avenger5 as second level.
240     REPOSITORY_LIST+="$(find "$HOME/apps" -maxdepth 2 -mindepth 2 -iname "avenger5" -type d) "
241   fi
242   
243   # the archive list is a set of directories that are major repositories of
244   # data which can be synched to backup drives.
245   define_yeti_variable MAJOR_ARCHIVE_SOURCES=
246   # the source collections list is a set of directories that indicate they
247   # harbor a lot of source code underneath.
248   define_yeti_variable SOURCECODE_HIERARCHY_LIST=
249
250   # initializes the feisty meow build variables, if possible.
251   function initialize_build_variables()
252   {
253     found_build_vars=0
254     # we need to know the feisty meow directory, or we bail.
255     if [ -z "$FEISTY_MEOW_APEX" ]; then return; fi
256     # pick from our expected generator folder, but make sure it's there...
257     buildvars="$FEISTY_MEOW_SCRIPTS/generator/build_variables.sh"
258     if [ -f "$buildvars" ]; then
259       # yep, that one looks good, so pull in the build defs.
260       source "$buildvars" "$buildvars"
261       found_build_vars=1
262     fi
263     # now augment the environment if we found our build variables.
264     if [ $found_build_vars == 1 ]; then
265       # the binary directory contains our collection of handy programs.
266       define_yeti_variable FEISTY_MEOW_BINARIES=$TARGETS_STORE
267       # add binaries created within build to the path.
268       define_yeti_variable PATH="$(dos_to_unix_path $FEISTY_MEOW_BINARIES):$PATH"
269       # Shared libraries are located via this variable.
270       define_yeti_variable LD_LIBRARY_PATH="$(dos_to_unix_path $LD_LIBRARY_PATH):$(dos_to_unix_path $FEISTY_MEOW_BINARIES)"
271     fi
272   }
273   
274   ##############
275   
276   # windoze specific patching up missing things.
277   
278   if [ "$OS" == "Windows_NT" ]; then
279     define_yeti_variable HOSTNAME=$(echo $HOSTNAME | tr A-Z a-z)
280   fi
281   
282   ##############
283   
284   # load in the build environment.
285   initialize_build_variables
286   
287   ##############
288   
289   # add to the PATH variables used for locating applications.  this step is taken after any
290   # potential overrides from the user.
291 #old:  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"
292   define_yeti_variable PATH="$PATH:$(find /usr/local/games -maxdepth 1 -type d -exec echo -n {}: ';' 2>/dev/null)/sbin"
293   
294   ##############
295
296   # set the SHUNIT_PATH so our shunit tests can find the codebase.
297   define_yeti_variable SHUNIT_PATH="$FEISTY_MEOW_SCRIPTS/shunit"
298   
299   ##############
300
301   define_yeti_variable CORE_VARIABLES_LOADED=true
302   
303   if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then echo variables initialization ends....; fi
304 fi
305
306 ##############
307
308 # pull in the custom overrides for feisty_meow scripts.  this is done almost
309 # last, because we want to set everything up as expected, then let the user
310 # override individual variables and definitions.  we also don't guard this
311 # to avoid running it again, because we don't know what mix of functions and
312 # aliases they want to define in there.
313 for i in $FEISTY_MEOW_LOADING_DOCK/custom/*.sh; do
314   if [ ! -f "$i" ]; then
315     # skip it if it's not real.
316     continue;
317   fi
318   if [ ! -z "$DEBUG_FEISTY_MEOW" ]; then
319     echo "loading customization: $(basename $(dirname $i))/$(basename $i)"
320   fi
321   source "$i"
322 done
323
324 ##############
325
326 # a late breaking action is to set the editor, if we can.
327 # we will fallback to whatever we can find on the host.
328 export EDITOR
329 # note: the editors for revision control must wait while the document is
330 # edited, so gvim and others that launch a separate x window are not
331 # going to work well unless they can be prevented from forking the process
332 # off.
333 if [ -z "$EDITOR" ]; then
334   EDITOR="$(which gvim)"
335   if [ ! -z "$EDITOR" ]; then
336     # if we found gvim, then add in the no forking flag.
337     EDITOR+=" --nofork"
338   fi
339 fi
340 if [ -z "$EDITOR" ]; then
341   EDITOR="$(which vim)"
342 fi
343 if [ -z "$EDITOR" ]; then
344   EDITOR="$(which vi)"
345 fi
346 if [ -z "$EDITOR" ]; then
347   EDITOR="$(which emacs)"
348 fi
349 ####
350 # out of ideas about editors at this point.
351 ####
352 # set the VISUAL and other variables from EDITOR if we found an editor to use.
353 if [ ! -z "$EDITOR" ]; then
354   VISUAL="$EDITOR"
355
356   export GIT_EDITOR="$EDITOR"
357   export SVN_EDITOR="$EDITOR"
358 fi
359
360 ##############
361