made var function support multiple names.
[feisty_meow.git] / scripts / core / functions.sh
index 1129d33669eb41800cb9609b64dce7bfbae39f1c..41d94b938e9144d29218e8fa31961b8a11f2a39f 100644 (file)
@@ -33,6 +33,35 @@ if [ -z "$skip_all" ]; then
     cd "$1"
   }
 
+  # displays the value of a variable in bash friendly format.
+  function var() {
+    while true; do
+      local varname="$1"; shift
+      if [ -z "$varname" ]; then
+        break
+      fi
+      if [ -z "${!varname}" ]; then
+        echo "$varname undefined"
+      else
+        echo "$varname=${!varname}"
+      fi
+    done
+  }
+
+  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 it failed,
   # then this complains and exits from bash.  the function parameters are
   # used as the message to print as a complaint.
@@ -40,6 +69,7 @@ if [ -z "$skip_all" ]; then
   {
     if [ $? -ne 0 ]; then
       echo -e "failed on: $*"
+      error_sound
       exit 1
     fi
   }
@@ -230,7 +260,7 @@ if [ -z "$skip_all" ]; then
   # label if they're doing an su with the sudo.
   function sudo() {
     local first_command="$1"
-    /usr/bin/sudo $*
+    /usr/bin/sudo "$@"
     if [ "$first_command" == "su" ]; then
       # yep, they were doing an su, but they're back now.
       bash $FEISTY_MEOW_SCRIPTS/tty/label_terminal_with_infos.sh
@@ -275,6 +305,20 @@ if [ -z "$skip_all" ]; then
     echo
   }
 
+  # a wrapper for the which command that finds items on the path.  some OSes
+  # do not provide which, so we want to not be spewing errors when that
+  # happens.
+  function whichable()
+  {
+    to_find="$1"; shift
+    which which &>/dev/null
+    if [ $? -ne 0 ]; then
+      # there is no which command here.  we produce nothing due to this.
+      echo
+    fi
+    echo $(which $to_find)
+  }
+
   # copies a set of custom scripts into the proper location for feisty meow
   # to merge their functions and aliases with the standard set.
   function recustomize()
@@ -291,11 +335,45 @@ if [ -z "$skip_all" ]; then
       return 1
     fi
     regenerate >/dev/null
+    pushd "$FEISTY_MEOW_GENERATED/custom" &>/dev/null
+    local incongruous_files="$(bash "$FEISTY_MEOW_SCRIPTS/files/list_non_dupes.sh" "$FEISTY_MEOW_DIR/customizing/$user" "$FEISTY_MEOW_GENERATED/custom")"
+    if [ ${#incongruous_files} -ge 1 ]; then
+      echo "cleaning unknown older overrides..."
+      perl "$FEISTY_MEOW_SCRIPTS/files/safedel.pl" $incongruous_files
+      echo
+    fi
+    popd &>/dev/null
     echo "copying custom overrides for $user"
+    mkdir "$FEISTY_MEOW_GENERATED/custom" 2>/dev/null
     perl "$FEISTY_MEOW_SCRIPTS/text/cpdiff.pl" "$FEISTY_MEOW_DIR/customizing/$user" "$FEISTY_MEOW_GENERATED/custom"
     regenerate
   }
 
+  function add_cygwin_drive_mounts() {
+    for i in c d e f g h q z ; do
+      ln -s /cygdrive/$i $i
+    done
+  }
+
+
+  # takes a file to modify, and then it will replace any occurrences of the
+  # pattern provided as the second parameter with the text in the third
+  # parameter.
+  function replace_pattern_in_file()
+  {
+    local file="$1"; shift
+    local pattern="$1"; shift
+    local replacement="$1"; shift
+    if [ -z "$file" -o -z "$pattern" -o -z "$replacement" ]; then
+      echo "replace_pattern_in_file: needs a filename, a pattern to replace, and the"
+      echo "text to replace that pattern with."
+      return 1
+    fi
+    sed -i -e "s%$pattern%$replacement%g" "$file"
+  }
+
+  ##############
+
   function function_sentinel() { return 0; }
   
   if [ ! -z "$SHELL_DEBUG" ]; then echo function definitions end....; fi