fixed modified path for x auth script
[feisty_meow.git] / scripts / core / functions.sh
1 #!/bin/bash
2
3 # This defines some general, useful functions.
4
5 # test whether we've been here before or not.
6 skip_all=
7 type function_sentinel &>/dev/null
8 if [ $? -eq 0 ]; then
9   # there was no error, so we can skip the inits.
10   if [ ! -z "$SHELL_DEBUG" ]; then
11     echo "skipping function definitions, because already defined."
12   fi
13   skip_all=yes
14 else
15   skip_all=
16 fi
17
18 if [ -z "$skip_all" ]; then
19
20   if [ ! -z "$SHELL_DEBUG" ]; then
21     echo "feisty meow function definitions beginning now..."
22   fi
23
24   # a handy little method that can be used for date strings.  it was getting
25   # really tiresome how many different ways the script did the date formatting.
26   function date_stringer() {
27     local sep="$1"; shift
28     if [ -z "$sep" ]; then sep='_'; fi
29     date +"%Y$sep%m$sep%d$sep%H%M$sep%S" | tr -d '/\n/'
30   }
31   
32   # makes a directory of the name specified and then tries to change the
33   # current directory to that directory.
34   function mcd() {
35     if [ ! -d "$1" ]; then mkdir -p "$1"; fi
36     cd "$1"
37   }
38
39   function is_array() {
40     [[ "$(declare -p $1)" =~ "declare -a" ]]
41   }
42
43   function is_alias() {
44     alias $1 &>/dev/null
45     return $?
46   }
47
48   # displays the value of a variable in bash friendly format.
49   function var() {
50     HOLDIFS="$IFS"
51     IFS=""
52     while true; do
53       local varname="$1"; shift
54       if [ -z "$varname" ]; then
55         break
56       fi
57
58       if is_alias "$varname"; then
59 #echo found $varname is alias
60         local tmpfile="$(mktemp $TMP/aliasout.XXXXXX)"
61         alias $varname | sed -e 's/.*=//' >$tmpfile
62         echo "alias $varname=$(cat $tmpfile)"
63         \rm $tmpfile
64       elif [ -z "${!varname}" ]; then
65         echo "$varname undefined"
66       else
67         if is_array "$varname"; then
68 #echo found $varname is array var 
69           local temparray
70           eval temparray="(\${$varname[@]})"
71           echo "$varname=(${temparray[@]})"
72 #hmmm: would be nice to print above with elements enclosed in quotes, so that we can properly
73 # see ones that have spaces in them.
74         else
75 #echo found $varname is simple
76           echo "$varname=${!varname}"
77         fi
78       fi
79     done | sort
80     IFS="$HOLDIFS"
81   }
82
83   # sets the variable in parameter 1 to the value in parameter 2, but only if
84   # that variable was undefined.
85   function set_var_if_undefined()
86   {
87     local var_name="$1"; shift
88     local var_value="$1"; shift
89     if [ -z "${!var_name}" ]; then
90       eval export $var_name="$var_value"
91     fi
92   }
93
94   function success_sound()
95   {
96     if [ ! -z "$CLAM_FINISH_SOUND" ]; then
97       bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_FINISH_SOUND"
98     fi
99   }
100
101   function error_sound()
102   {
103     if [ ! -z "$CLAM_ERROR_SOUND" ]; then
104       bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_ERROR_SOUND"
105     fi
106   }
107
108   # checks the result of the last command that was run, and if that failed,
109   # then this complains and exits from bash.  the function parameters are
110   # used as the message to print as a complaint.
111   function check_result()
112   {
113     if [ $? -ne 0 ]; then
114       echo -e "failed on: $*"
115       error_sound
116       exit 1
117     fi
118   }
119
120   # locates a process given a search pattern to match in the process list.
121   # supports a single command line flag style parameter of "-u USERNAME";
122   # if the -u flag is found, a username is expected afterwards, and only the
123   # processes of that user are considered.
124   function psfind() {
125     local -a patterns=("${@}")
126 #echo ====
127 #echo patterns list is: "${patterns[@]}"
128 #echo ====
129
130     local user_flag
131     if [ "${patterns[0]}" == "-u" ]; then
132       user_flag="-u ${patterns[1]}" 
133 #echo "found a -u parm and user=${patterns[1]}" 
134       # void the two elements with that user flag so we don't use them as patterns.
135       unset patterns[0] patterns[1]=
136     else
137       # select all users.
138       user_flag="-e"
139     fi
140
141     local PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")"
142     local -a PIDS_SOUGHT
143
144     if [ "$OS" == "Windows_NT" ]; then
145       # gets cygwin's (god awful) ps to show windoze processes also.
146       local EXTRA_DOZER_FLAGS="-W"
147       # pattern to use for peeling off the process numbers.
148       local pid_finder_pattern='s/ *\([0-9][0-9]*\) *.*$/\1/p'
149
150     else
151       # flags which clean up the process listing output on unixes.
152       # apparently cygwin doesn't count as a type of unix, because their
153       # crummy specialized ps command doesn't support normal ps flags.
154       local EXTRA_UNIX_FLAGS="-o pid,args"
155       # pattern to use for peeling off the process numbers.
156       local pid_finder_pattern='s/^[[:space:]]*\([0-9][0-9]*\).*$/\1/p'
157     fi
158
159     /bin/ps $EXTRA_DOZER_FLAGS $EXTRA_UNIX_FLAGS $user_flag | tail -n +2 >$PID_DUMP
160 #echo ====
161 #echo got all this stuff in the pid dump file:
162 #cat $PID_DUMP
163 #echo ====
164
165     # search for the pattern the user wants to find, and just pluck the process
166     # ids out of the results.
167     local i
168     for i in "${patterns[@]}"; do
169       PIDS_SOUGHT+=($(cat $PID_DUMP \
170         | grep -i "$i" \
171         | sed -n -e "$pid_finder_pattern"))
172     done
173 #echo ====
174 #echo pids sought list became:
175 #echo "${PIDS_SOUGHT[@]}"
176 #echo ====
177
178     if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
179       local PIDS_SOUGHT2=$(printf -- '%s\n' ${PIDS_SOUGHT[@]} | sort | uniq)
180       PIDS_SOUGHT=()
181       PIDS_SOUGHT=${PIDS_SOUGHT2[*]}
182       echo ${PIDS_SOUGHT[*]}
183     fi
184     /bin/rm $PID_DUMP
185   }
186   
187   # finds all processes matching the pattern specified and shows their full
188   # process listing (whereas psfind just lists process ids).
189   function psa() {
190     if [ -z "$1" ]; then
191       echo "psa finds processes by pattern, but there was no pattern on the command line."
192       return 1
193     fi
194     local -a patterns=("${@}")
195     p=$(psfind "${patterns[@]}")
196     if [ -z "$p" ]; then
197       # no matches.
198       return 0
199     fi
200
201     if [ "${patterns[0]}" == "-u" ]; then
202       # void the two elements with that user flag so we don't use them as patterns.
203       unset patterns[0] patterns[1]=
204     fi
205
206     echo ""
207     echo "Processes matching ${patterns[@]}..."
208     echo ""
209     if [ -n "$IS_DARWIN" ]; then
210       unset fuzil_sentinel
211       for i in $p; do
212         # only print the header the first time.
213         if [ -z "$fuzil_sentinel" ]; then
214           ps $i -w -u
215         else
216           ps $i -w -u | sed -e '1d'
217         fi
218         fuzil_sentinel=true
219       done
220     else 
221       # cases besides mac os x's darwin.
222       if [ "$OS" == "Windows_NT" ]; then
223         # special case for windows.
224         ps | head -1
225         for curr in $p; do
226           ps -W -p $curr | tail -n +2
227         done
228       else
229         # normal OSes can handle a nice simple query.
230         ps wu $p
231       fi
232     fi
233   }
234   
235   # an unfortunately similarly named function to the above 'ps' as in process
236   # methods, but this 'ps' stands for postscript.  this takes a postscript file
237   # and converts it into pcl3 printer language and then ships it to the printer.
238   # this mostly makes sense for an environment where one's default printer is
239   # pcl.  if the input postscript causes ghostscript to bomb out, there has been
240   # some good success running ps2ps on the input file and using the cleaned
241   # postscript file for printing.
242   function ps2pcl2lpr() {
243     for $i in $*; do
244       gs -sDEVICE=pcl3 -sOutputFile=- -sPAPERSIZE=letter "$i" | lpr -l 
245     done
246   }
247   
248 #  function fix_alsa() {
249 #    sudo /etc/init.d/alsasound restart
250 #  }
251
252   function screen() {
253     save_terminal_title
254 #hmmm: ugly absolute path here.
255     /usr/bin/screen $*
256     restore_terminal_title
257   }
258   
259   # switches from a /X/path form to an X:/ form.  this also processes cygwin paths.
260   function unix_to_dos_path() {
261     # we usually remove dos slashes in favor of forward slashes.
262     local DOSSYHOME
263     if [[ ! "$OS" =~ ^[Ww][iI][nN] ]]; then
264       # fake this value for non-windows (non-cygwin) platforms.
265       DOSSYHOME="$HOME"
266     else
267       # for cygwin, we must replace the /home/X path with an absolute one, since cygwin
268       # insists on the /home form instead of /c/cygwin/home being possible.  this is
269       # super frustrating and nightmarish.
270       DOSSYHOME="$(cygpath -am "$HOME")"
271     fi
272
273     if [ ! -z "$SERIOUS_SLASH_TREATMENT" ]; then
274       # unless this flag is set, in which case we force dos slashes.
275       echo "$1" | sed -e "s?^$HOME?$DOSSYHOME?g" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/' | sed -e 's/\//\\/g'
276     else
277       echo "$1" | sed -e "s?^$HOME?$DOSSYHOME?g" | sed -e 's/\\/\//g' | sed -e 's/\/cygdrive//' | sed -e 's/\/\([a-zA-Z]\)\/\(.*\)/\1:\/\2/'
278     fi
279   }
280   
281   # switches from an X:/ form to a /cygdrive/X/path form.  this is only useful
282   # for the cygwin environment currently.
283   function dos_to_unix_path() {
284     # we always remove dos slashes in favor of forward slashes.
285 #old:    echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/\1\/\2/'
286          echo "$1" | sed -e 's/\\/\//g' | sed -e 's/\([a-zA-Z]\):\/\(.*\)/\/cygdrive\/\1\/\2/'
287   }
288
289   # returns a successful value (0) if this system is debian or ubuntu.
290   function debian_like() {
291     # decide if we think this is debian or ubuntu or a variant.
292     DEBIAN_LIKE=$(if [ ! -z "$(grep -i debian /etc/issue)" \
293         -o ! -z "$(grep -i ubuntu /etc/issue)" ]; then echo 1; else echo 0; fi)
294     if [ $DEBIAN_LIKE -eq 1 ]; then
295       # success; this is debianish.
296       return 0
297     else
298       # this seems like some other OS.
299       return 1
300     fi
301   }
302   
303   # su function: makes su perform a login.
304   # for some OSes, this transfers the X authority information to the new login.
305   function su() {
306     if debian_like; then
307       # debian currently requires the full version which imports X authority
308       # information for su.
309   
310       # get the x authority info for our current user.
311       source "$FEISTY_MEOW_SCRIPTS/security/get_x_auth.sh"
312   
313       if [ -z "$X_auth_info" ]; then
314         # if there's no authentication info to pass along, we just do a normal su.
315         /bin/su -l $*
316       else
317         # under X, we update the new login's authority info with the previous
318         # user's info.
319         (unset XAUTHORITY; /bin/su -l $* -c "$X_auth_info ; export DISPLAY=$DISPLAY ; bash")
320       fi
321     else
322       # non-debian supposedly doesn't need the extra overhead any more.
323       # or at least suse doesn't, which is the other one we've tested on.
324       /bin/su -l $*
325     fi
326   }
327   
328   # sudo function wraps the normal sudo by ensuring we replace the terminal
329   # label if they're doing an su with the sudo.
330   function sudo() {
331 #    local first_command="$1"
332     save_terminal_title
333     /usr/bin/sudo "$@"
334     restore_terminal_title
335 #    if [ "$first_command" == "su" ]; then
336 #      # yep, they were doing an su, but they're back now.
337 #      label_terminal_with_info
338 #    fi
339   }
340   
341   # trashes the .#blah files that cvs and svn leave behind when finding conflicts.
342   # this kind of assumes you've already checked them for any salient facts.
343   function clean_cvs_junk() {
344     for i in $*; do
345       find $i -follow -type f -iname ".#*" -exec perl $FEISTY_MEOW_SCRIPTS/files/safedel.pl {} ";" 
346     done
347   }
348
349   # overlay for nechung binary so that we can complain less grossly about it when it's missing.
350   function nechung() {
351     local wheres_nechung=$(which nechung 2>/dev/null)
352     if [ -z "$wheres_nechung" ]; then
353       echo "The nechung oracle program cannot be found.  You may want to consider"
354       echo "rebuilding the feisty meow applications with this command:"
355       echo "bash $FEISTY_MEOW_SCRIPTS/generator/produce_feisty_meow.sh"
356     else
357       $wheres_nechung
358     fi
359   }
360   
361   # recreates all the generated files that the feisty meow scripts use.
362   function regenerate() {
363     # do the bootstrapping process again.
364     save_terminal_title
365     echo "regenerating feisty meow script environment."
366     bash $FEISTY_MEOW_SCRIPTS/core/reconfigure_feisty_meow.sh
367     echo
368     # force a full reload by turning off sentinel variables and methods.
369     unset -v CORE_VARIABLES_LOADED FEISTY_MEOW_LOADING_DOCK USER_CUSTOMIZATIONS_LOADED
370     unalias CORE_ALIASES_LOADED &>/dev/null
371     unset -f function_sentinel 
372     # reload feisty meow environment in current shell.
373     source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
374     # run nechung oracle to give user a new fortune.
375     nechung
376     restore_terminal_title
377   }
378
379   # copies a set of custom scripts into the proper location for feisty meow
380   # to merge their functions and aliases with the standard set.
381   function recustomize()
382   {
383     local custom_user="$1"; shift
384     if [ -z "$custom_user" ]; then
385       # use our default example user if there was no name provided.
386       custom_user=fred
387     fi
388
389     save_terminal_title
390
391     if [ ! -d "$FEISTY_MEOW_SCRIPTS/customize/$custom_user" ]; then
392       echo "The customization folder provided for $custom_user should be:"
393       echo "  '$FEISTY_MEOW_SCRIPTS/customize/$custom_user'"
394       echo "but that folder does not exist.  Skipping customization."
395       return 1
396     fi
397     regenerate >/dev/null
398     pushd "$FEISTY_MEOW_LOADING_DOCK/custom" &>/dev/null
399     incongruous_files="$(bash "$FEISTY_MEOW_SCRIPTS/files/list_non_dupes.sh" "$FEISTY_MEOW_SCRIPTS/customize/$custom_user" "$FEISTY_MEOW_LOADING_DOCK/custom")"
400     
401     #echo "the incongruous files list is: $incongruous_files"
402     # disallow a single character result, since we get "*" as result when nothing exists yet.
403     if [ ${#incongruous_files} -ge 2 ]; then
404       echo "cleaning unknown older overrides..."
405       perl "$FEISTY_MEOW_SCRIPTS/files/safedel.pl" $incongruous_files
406       echo
407     fi
408     popd &>/dev/null
409     echo "copying custom overrides for $custom_user"
410     mkdir -p "$FEISTY_MEOW_LOADING_DOCK/custom" 2>/dev/null
411     perl "$FEISTY_MEOW_SCRIPTS/text/cpdiff.pl" "$FEISTY_MEOW_SCRIPTS/customize/$custom_user" "$FEISTY_MEOW_LOADING_DOCK/custom"
412     if [ -d "$FEISTY_MEOW_SCRIPTS/customize/$custom_user/scripts" ]; then
413       echo "copying custom scripts for $custom_user"
414       \cp -R "$FEISTY_MEOW_SCRIPTS/customize/$custom_user/scripts" "$FEISTY_MEOW_LOADING_DOCK/custom/"
415     fi
416     echo
417     regenerate
418
419     restore_terminal_title
420   }
421
422   # generates a random password where the first parameter is the number of characters
423   # in the password (default 20) and the second parameter specifies whether to use
424   # special characters (1) or not (0).
425   # found function at http://legroom.net/2010/05/06/bash-random-password-generator
426   function random_password()
427   {
428     [ "$2" == "0" ] && CHAR="[:alnum:]" || CHAR="[:graph:]"
429     cat /dev/urandom | tr -cd "$CHAR" | head -c ${1:-32}
430     echo
431   }
432
433   # a wrapper for the which command that finds items on the path.  some OSes
434   # do not provide which, so we want to not be spewing errors when that
435   # happens.
436   function whichable()
437   {
438     to_find="$1"; shift
439     which which &>/dev/null
440     if [ $? -ne 0 ]; then
441       # there is no which command here.  we produce nothing due to this.
442       echo
443     fi
444     echo $(which $to_find)
445   }
446
447 #hmmm: improve this by not adding the link
448 # if already there, or if the drive is not valid.
449   function add_cygwin_drive_mounts() {
450     for i in c d e f g h q z ; do
451       ln -s /cygdrive/$i $i
452     done
453   }
454
455   ############################
456
457   # takes a file to modify, and then it will replace any occurrences of the
458   # pattern provided as the second parameter with the text in the third
459   # parameter.
460   function replace_pattern_in_file()
461   {
462     local file="$1"; shift
463     local pattern="$1"; shift
464     local replacement="$1"; shift
465     if [ -z "$file" -o -z "$pattern" -o -z "$replacement" ]; then
466       echo "replace_pattern_in_file: needs a filename, a pattern to replace, and the"
467       echo "text to replace that pattern with."
468       return 1
469     fi
470     sed -i -e "s%$pattern%$replacement%g" "$file"
471   }
472
473   # similar to replace_pattern_in_file, but also will add the new value
474   # when the old one did not already exist in the file.
475   function replace_if_exists_or_add()
476   {
477     local file="$1"; shift
478     local phrase="$1"; shift
479     local replacement="$1"; shift
480     if [ -z "$file" -o ! -f "$file" -o -z "$phrase" -o -z "$replacement" ]; then
481       echo "replace_if_exists_or_add: needs a filename, a phrase to replace, and the"
482       echo "text to replace that phrase with."
483       return 1
484     fi
485     grep "$phrase" "$file" >/dev/null
486     # replace if the phrase is there, otherwise add it.
487     if [ $? -eq 0 ]; then
488       replace_pattern_in_file "$file" "$phrase" "$replacement"
489     else
490       # this had better be the complete line.
491       echo "$replacement" >>"$file"
492     fi
493   }
494
495   ############################
496
497   # finds a variable (first parameter) in a particular property file
498   # (second parameter).  the expected format for the file is:
499   # varX=valueX
500   function seek_variable()
501   {
502     local find_var="$1"; shift
503     local file="$1"; shift
504     if [ -z "$find_var" -o -z "$file" -o ! -f "$file" ]; then
505       echo -e "seek_variable: needs two parameters, firstly a variable name, and\nsecondly a file where the variable's value will be sought." 1>&2
506       return 1
507     fi
508   
509     while read line; do
510       if [ ${#line} -eq 0 ]; then continue; fi
511       # split the line into the variable name and value.
512       IFS='=' read -a assignment <<< "$line"
513       local var="${assignment[0]}"
514       local value="${assignment[1]}"
515       if [ "${value:0:1}" == '"' ]; then
516         # assume the entry was in quotes and remove them.
517         value="${value:1:$((${#value} - 2))}"
518       fi
519       if [ "$find_var" == "$var" ]; then
520         echo "$value"
521       fi
522     done < "$file"
523   }
524   
525   # finds a variable (first parameter) in a particular XML format file
526   # (second parameter).  the expected format for the file is:
527   # ... name="varX" value="valueX" ...
528   function seek_variable_in_xml()
529   {
530     local find_var="$1"; shift
531     local file="$1"; shift
532     if [ -z "$find_var" -o -z "$file" -o ! -f "$file" ]; then
533       echo "seek_variable_in_xml: needs two parameters, firstly a variable name, and"
534       echo "secondly an XML file where the variable's value will be sought."
535       return 1
536     fi
537   
538     while read line; do
539       if [ ${#line} -eq 0 ]; then continue; fi
540       # process the line to make it more conventional looking.
541       line="$(echo "$line" | sed -e 's/.*name="\([^"]*\)" value="\([^"]*\)"/\1=\2/')"
542       # split the line into the variable name and value.
543       IFS='=' read -a assignment <<< "$line"
544       local var="${assignment[0]}"
545       local value="${assignment[1]}"
546       if [ "${value:0:1}" == '"' ]; then
547         # assume the entry was in quotes and remove them.
548         value="${value:1:$((${#value} - 2))}"
549       fi
550       if [ "$find_var" == "$var" ]; then
551         echo "$value"
552       fi
553     done < "$file"
554   }
555   
556   ############################
557
558   # goes to a particular directory passed as parameter 1, and then removes all
559   # the parameters after that from that directory.
560   function push_whack_pop()
561   {
562     local dir="$1"; shift
563     pushd "$dir" &>/dev/null
564     if [ $? -ne 0 ]; then echo failed to enter dir--quitting.; fi
565     rm -rf $* &>/dev/null
566     if [ $? -ne 0 ]; then echo received a failure code when removing.; fi
567     popd &>/dev/null
568   }
569
570   function spacem()
571   {
572     while [ $# -gt 0 ]; do
573       arg="$1"; shift
574       if [ ! -f "$arg" -a ! -d "$arg" ]; then
575         echo "failure to find a file or directory named '$arg'."
576         continue
577       fi
578
579       # first we will capture the output of the character replacement operation for reporting.
580       # this is done first since some filenames can't be properly renamed in perl (e.g. if they
581       # have pipe characters apparently).
582       intermediate_name="$(bash "$FEISTY_MEOW_SCRIPTS/files/replace_spaces_with_underscores.sh" "$arg")"
583       local saw_intermediate_result=0
584       if [ -z "$intermediate_name" ]; then
585         # make sure we report something, if there are no further name changes.
586         intermediate_name="'$arg'"
587       else 
588         # now zap the first part of the name off (since original name isn't needed).
589         intermediate_name="$(echo $intermediate_name | sed -e 's/.*=> //')"
590         saw_intermediate_result=1
591       fi
592
593       # first we rename the file to be lower case.
594       actual_file="$(echo $intermediate_name | sed -e "s/'\([^']*\)'/\1/")"
595       final_name="$(perl $FEISTY_MEOW_SCRIPTS/files/renlower.pl "$actual_file")"
596       local saw_final_result=0
597       if [ -z "$final_name" ]; then
598         final_name="$intermediate_name"
599       else
600         final_name="$(echo $final_name | sed -e 's/.*=> //')"
601         saw_final_result=1
602       fi
603 #echo intermed=$saw_intermediate_result 
604 #echo final=$saw_final_result 
605
606       if [[ $saw_intermediate_result != 0 || $saw_final_result != 0 ]]; then
607         # printout the combined operation results.
608         echo "'$arg' => $final_name"
609       fi
610     done
611   }
612
613   ##############
614
615 # new breed of definer functions goes here.  still in progress.
616
617   # defines an alias and remembers that this is a new or modified definition.
618   # if the feisty meow codebase is unloaded, then so are all the aliases that
619   # were defined.
620   function define_yeti_alias()
621   {
622 # if alias exists already, save old value for restore,
623 # otherwise save null value for restore,
624 # have to handle unaliasing if there was no prior value of one
625 # we newly defined.
626 # add alias name to a list of feisty defined aliases.
627
628 #hmmm: first implem, just do the alias and get that working...
629 alias "${@}"
630
631
632 return 0
633   }
634
635   # defines a variable within the feisty meow environment and remembers that
636   # this is a new or modified definition.  if the feisty meow codebase is
637   # unloaded, then so are all the variables that were defined.
638   # this function always exports the variables it defines.
639 #  function define_yeti_variable()
640 #  {
641 ## if variable exists already, save old value for restore,
642 ## otherwise save null value for restore,
643 ## have to handle unsetting if there was no prior value of one
644 ## we newly defined.
645 ## add variable name to a list of feisty defined variables.
646 #
647 ##hmmm: first implem just sets it up and exports the variable.
648 ##  i.e., this method always exports.
649 #export "${@}" 
650 #
651 #
652 #return 0
653 #  }
654
655   ##############
656
657 #hmmm: this points to an extended functions file being needed; not all of these are core.
658
659   # displays documentation in "md" formatted files.
660   function show_md()
661   {
662     local file="$1"; shift
663     pandoc "$file" | lynx -stdin
664   }
665
666   ##############
667
668   # just shows a separator line for an 80 column console, or uses the first
669   # parameter as the number of columns to expect.
670   function separator()
671   {
672     count=$1; shift
673     if [ -z "$count" ]; then
674       count=79
675     fi
676     echo
677     local i
678     for ((i=0; i < $count - 1; i++)); do
679       echo -n "="
680     done
681     echo
682     echo
683   }
684   # alias for separator.
685   function sep()
686   {
687     separator $*
688   }
689
690   ##############
691
692   function function_sentinel()
693   {
694     return 0; 
695   }
696   
697   if [ ! -z "$SHELL_DEBUG" ]; then echo "feisty meow function definitions done."; fi
698
699   ##############
700
701   # test code for set_var_if_undefined.
702   run_test=0
703   if [ $run_test != 0 ]; then
704     echo running tests on set_var_if_undefined.
705     flagrant=petunia
706     set_var_if_undefined flagrant forknordle
707     check_result "testing if defined variable would be whacked"
708     if [ $flagrant != petunia ]; then
709       echo set_var_if_undefined failed to leave the test variable alone
710       exit 1
711     fi
712     unset bobblehead_stomper
713     set_var_if_undefined bobblehead_stomper endurance
714     if [ $bobblehead_stomper != endurance ]; then
715       echo set_var_if_undefined failed to set a variable that was not defined yet
716       exit 1
717     fi
718   fi
719
720 fi
721