]> git.donarmstrong.com Git - bin.git/blobdiff - sanitize_filename
* fix the renaming of / in long files to _
[bin.git] / sanitize_filename
index 3e6d70557ec24dacc5010ba00530549b5b409e8a..fce84936dad1496422fa4b64cf304e8d6b3bcc65 100755 (executable)
@@ -1,13 +1,15 @@
-#! /bin/sh
+#! /bin/bash
 # replace spaces with underscores, lowercase everything, ditch multiple underscores, and ditch _.foo.
 # should probably try to avoid overwriting existing files, but c'est la vie!
-set - $(getopt -u --longoptions 'anime' -- '' "$@")
+
+TEMP="$(getopt --longoptions 'anime' -- '' "$@")"
+eval set -- "$TEMP"
 set -e
 
 ANIME_CUT=0
 
 
-while [ $1 != '--' ]; do
+while [ $1 != "--" ]; do
     case $1 in 
        --anime)
            ANIME_CUT=1
@@ -23,7 +25,7 @@ while [ $1 != '--' ]; do
 done;
 shift;
 if [ "$ANIME_CUT" == 0 ]; then
-    rename 's/\s+/\_/g; $_ = lc($_); s/\_+/\_/g; s/\_\././' "$@"
+    rename 's/\s+/\_/g; s{[^\w\d.\/]}{\_}g; s/_$//g; $_ = lc($_); s/\_+/\_/g; s/\_\././; s/^_+//g;' -- "$@"
 else
-    rename 's/\[[^\]]+\]//g; s/^_+//; s/\s+/\_/g; $_ = lc($_); s/[_-]+/\_/g; s/\_\././' "$@"
+    rename 's{\[[^\]\/]+\]}{}g; s{\([^\)\/]+\)}{}g; s/\s+/\_/g; $_ = lc($_); s/[_-]+/\_/g; s/\_\././; s/^_+//;' -- "$@"
 fi;
\ No newline at end of file