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