tasty solution to spacemall weirdness
[feisty_meow.git] / scripts / files / spacem.sh
1 #!/bin/bash
2
3 function spacem_out()
4 {
5   while [ $# -gt 0 ]; do
6     arg="$1"; shift
7     if [ ! -f "$arg" -a ! -d "$arg" ]; then
8       echo "=> did not find a file or directory named '$arg'."
9       continue
10     fi
11
12     # we capture the output of the character replacement operation for reporting.
13     # this is done first since some filenames cannot be properly renamed in perl (e.g. if they
14     # have pipe characters apparently).
15     intermediate_name="$(bash "$FEISTY_MEOW_SCRIPTS/files/replace_spaces_with_underscores.sh" "$arg")"
16     local saw_intermediate_result=0
17     if [ -z "$intermediate_name" ]; then
18       # make sure we report something, if there are no further name changes.
19       intermediate_name="'$arg'"
20     else 
21       # now zap the first part of the name off (since original name is not needed).
22       intermediate_name="$(echo $intermediate_name | sed -e 's/.*=> //')"
23       saw_intermediate_result=1
24     fi
25
26     # here we rename the file to be lower case.
27     actual_file="$(echo $intermediate_name | sed -e "s/'\([^']*\)'/\1/")"
28     final_name="$(perl "$FEISTY_MEOW_SCRIPTS/files/renlower.pl" "$actual_file")"
29     local saw_final_result=0
30     if [ -z "$final_name" ]; then
31       final_name="$intermediate_name"
32     else
33       final_name="$(echo $final_name | sed -e 's/.*=> //' )"
34       saw_final_result=1
35     fi
36
37     if [[ $saw_intermediate_result != 0 || $saw_final_result != 0 ]]; then
38       # printout the combined operation results.
39       echo "'$arg' => $final_name"
40     fi
41   done
42 }
43
44 # this block should execute when the script is actually run, rather
45 # than when it is just being sourced.
46 if [[ $0 =~ .*spacem\.sh.* ]]; then
47   source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
48   exit_on_error "sourcing the feisty meow environment"
49   spacem_out "${@}"
50   exit_on_error "running spacem_out on a list: ${@}"
51 fi
52