+
+ # a handy little method that can be used for date strings. it was getting
+ # really tiresome how many different ways the script did the date formatting.
+ function date_stringer() {
+ local sep="$1"; shift
+ if [ -z "$sep" ]; then sep='_'; fi
+ date +"%Y$sep%m$sep%d$sep%H%M$sep%S" | tr -d '/\n/'
+ }
+
+ # makes a directory of the name specified and then tries to change the
+ # current directory to that directory.
+ function mcd() {
+ if [ ! -d "$1" ]; then mkdir -p "$1"; fi
+ cd "$1"
+ }
+
+ function is_array() {
+ [[ "$(declare -p $1)" =~ "declare -a" ]]
+ }
+
+ function is_alias() {
+ alias $1 &>/dev/null
+ return $?
+ }
+
+ # displays the value of a variable in bash friendly format.
+ function var() {
+ HOLDIFS="$IFS"
+ IFS=""
+ while true; do
+ local varname="$1"; shift
+ if [ -z "$varname" ]; then
+ break
+ fi
+
+ if is_alias "$varname"; then
+#echo found $varname is alias
+ local tmpfile="$(mktemp $TMP/aliasout.XXXXXX)"
+ alias $varname | sed -e 's/.*=//' >$tmpfile
+ echo "alias $varname=$(cat $tmpfile)"
+ \rm $tmpfile
+ elif [ -z "${!varname}" ]; then
+ echo "$varname undefined"
+ else
+ if is_array "$varname"; then
+#echo found $varname is array var
+ local temparray
+ eval temparray="(\${$varname[@]})"
+ echo "$varname=(${temparray[@]})"
+#hmmm: would be nice to print above with elements enclosed in quotes, so that we can properly
+# see ones that have spaces in them.
+ else
+#echo found $varname is simple
+ echo "$varname=${!varname}"
+ fi
+ fi
+ done
+ IFS="$HOLDIFS"
+ }
+
+ # sets the variable in parameter 1 to the value in parameter 2, but only if
+ # that variable was undefined.
+ function set_var_if_undefined()
+ {
+ local var_name="$1"; shift
+ local var_value="$1"; shift
+ if [ -z "${!var_name}" ]; then
+ eval export $var_name="$var_value"
+ fi
+ }
+
+ function success_sound()
+ {
+ if [ ! -z "$CLAM_FINISH_SOUND" ]; then
+ bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_FINISH_SOUND"
+ fi
+ }
+
+ function error_sound()
+ {
+ if [ ! -z "$CLAM_ERROR_SOUND" ]; then
+ bash $FEISTY_MEOW_SCRIPTS/multimedia/sound_play.sh "$CLAM_ERROR_SOUND"
+ fi
+ }
+
+ # checks the result of the last command that was run, and if that failed,
+ # then this complains and exits from bash. the function parameters are
+ # used as the message to print as a complaint.
+ function check_result()
+ {
+ if [ $? -ne 0 ]; then
+ echo -e "failed on: $*"
+ error_sound
+ exit 1
+ fi
+ }
+
+ # locates a process given a search pattern to match in the process list.
+ # supports a single command line flag style parameter of "-u USERNAME";
+ # if the -u flag is found, a username is expected afterwards, and only the
+ # processes of that user are considered.
+ function psfind() {
+ local -a patterns=("${@}")
+#echo ====
+#echo patterns list is: "${patterns[@]}"
+#echo ====
+
+ local user_flag
+ if [ "${patterns[0]}" == "-u" ]; then
+ user_flag="-u ${patterns[1]}"
+#echo "found a -u parm and user=${patterns[1]}"
+ # void the two elements with that user flag so we don't use them as patterns.
+ unset patterns[0] patterns[1]=
+ else
+ # select all users.
+ user_flag="-e"
+ fi
+
+ local PID_DUMP="$(mktemp "$TMP/zz_pidlist.XXXXXX")"
+ local -a PIDS_SOUGHT
+
+ if [ "$OS" == "Windows_NT" ]; then
+ # gets cygwin's (god awful) ps to show windoze processes also.
+ local EXTRA_DOZER_FLAGS="-W"
+ # pattern to use for peeling off the process numbers.
+ local pid_finder_pattern='s/ *\([0-9][0-9]*\) *.*$/\1/p'
+
+ else
+ # flags which clean up the output on unixes, which apparently cygwin
+ # doesn't count as. their crappy specialized ps doesn't support this.
+ local EXTRA_UNIX_FLAGS="-o pid,args"
+ # pattern to use for peeling off the process numbers.
+ local pid_finder_pattern='s/^[[:space:]]*\([0-9][0-9]*\).*$/\1/p'
+ fi
+
+ /bin/ps $EXTRA_DOZER_FLAGS $EXTRA_UNIX_FLAGS $user_flag | tail -n +2 >$PID_DUMP
+#echo ====
+#echo got all this stuff in the pid dump file:
+#cat $PID_DUMP
+#echo ====
+
+ # search for the pattern the user wants to find, and just pluck the process
+ # ids out of the results.
+ local i
+ for i in "${patterns[@]}"; do
+ PIDS_SOUGHT+=($(cat $PID_DUMP \
+ | grep -i "$i" \
+ | sed -n -e "$pid_finder_pattern"))
+ done
+#echo ====
+#echo pids sought list became:
+#echo "${PIDS_SOUGHT[@]}"
+#echo ====
+
+ if [ ${#PIDS_SOUGHT[*]} -ne 0 ]; then
+ local PIDS_SOUGHT2=$(printf -- '%s\n' ${PIDS_SOUGHT[@]} | sort | uniq)
+ PIDS_SOUGHT=()
+ PIDS_SOUGHT=${PIDS_SOUGHT2[*]}
+ echo ${PIDS_SOUGHT[*]}
+ fi
+ /bin/rm $PID_DUMP
+ }
+
+ # finds all processes matching the pattern specified and shows their full
+ # process listing (whereas psfind just lists process ids).
+ function psa() {
+ if [ -z "$1" ]; then
+ echo "psa finds processes by pattern, but there was no pattern on the command line."
+ return 1
+ fi
+ local -a patterns=("${@}")
+ p=$(psfind "${patterns[@]}")
+ if [ -z "$p" ]; then
+ # no matches.
+ return 0
+ fi
+
+ if [ "${patterns[0]}" == "-u" ]; then
+ # void the two elements with that user flag so we don't use them as patterns.
+ unset patterns[0] patterns[1]=
+ fi
+