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