]> Sergey Matveev's repositories - torn.git/blobdiff - torn.pl
Explicitly mention that Perl version is not supported
[torn.git] / torn.pl
diff --git a/torn.pl b/torn.pl
new file mode 100755 (executable)
index 0000000..a029b56
--- /dev/null
+++ b/torn.pl
@@ -0,0 +1,132 @@
+#!/usr/bin/env perl
+# torn - Musical files renaming with russian language transliteration
+# Copyright (C) 2007-2021 Sergey Matveev (stargrave@stargrave.org)
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 3 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+=pod
+
+=head1 DESCRIPTION
+
+This Perl script takes directory file list and renames all files,
+making some substitution changes. It has some rules for musical
+files and rules to convert russian filenames to translit.
+
+This version is unsupported! Use zsh-based implementation instead.
+
+=head1 USAGE
+
+Simply run this script in directory where you want files and/or
+directories to be renamed. Directories are not transliterated by
+default, but you can use FORCE_DIR=1 environment variable.
+
+=head1 OVERVIEW
+
+=over 4
+
+=item * Transliterate everything
+
+=item * Replaces spaces with underscore
+
+=item * Downcase all extensions
+
+=item * "_-_" so good looking on the screen is replaced by single dash
+
+=item * Remove spaces before and after brackets
+
+=item * Remove "[]" brackets
+
+=item * Replace ampersand with "and" word
+
+=back
+
+=head1 AUTHOR
+
+Sergey Matveev L<mailto:stargrave@stargrave.org>
+
+=cut
+
+use strict;
+use utf8;
+use Encode;
+
+binmode STDOUT, ":utf8";
+
+my $VERSION = "0.10";
+
+my $src;
+my $dst;
+my $src_filename;
+
+if ($#ARGV >= 0) {
+    print "torn version $VERSION, Copyright (C) 2007-2023 Sergey Matveev
+torn comes with ABSOLUTELY NO WARRANTY.  This is free software,
+and you are welcome to redistribute it under certain conditions.\n
+Usage: just run inside the directory. Look for POD inside the script itself.\n";
+};
+
+opendir DIR, "." or die "Can not open directory\n";
+foreach (sort readdir DIR) {
+    # Skip directory itself
+    next if /^\.{1,2}$/;
+    next if -d and not $ENV{FORCE_DIR};
+
+    $src_filename = $_;
+    $src = decode "utf-8", $src_filename;
+    $dst = $src;
+
+    # Basic corrections for music files
+    $dst =~ s/ /_/g;
+    $dst =~ s/_-_/-/g;
+    $dst =~ s/_\(/\(/g;
+    $dst =~ s/-\(/-/g;
+    $dst =~ s/\)_/-/g;
+    $dst =~ s/\,_/\,/g;
+    $dst =~ s/\[//g;
+    $dst =~ s/\]//g;
+    $dst =~ s/\(_/\(/g;
+    $dst =~ s/_\)/\)/g;
+    $dst =~ s/\&/and/g;
+
+    # Make translit
+    $dst =~ y/абвгдеёзийклмнопрстуфхцьъыэ/abvgdeezijklmnoprstufhcjjye/;
+    $dst =~ y/АБВГДЕЁЗИЙКЛМНОПРСТУФХЦЬЪЫЭ/ABVGDEEZIJKLMNOPRSTUFHCJJYE/;
+    $dst =~ s/ж/zh/g;
+    $dst =~ s/ч/ch/g;
+    $dst =~ s/ш/sh/g;
+    $dst =~ s/щ/sch/g;
+    $dst =~ s/я/ja/g;
+    $dst =~ s/ю/ju/g;
+    $dst =~ s/Ж/Zh/g;
+    $dst =~ s/Ч/Ch/g;
+    $dst =~ s/Ш/Sh/g;
+    $dst =~ s/Щ/Sch/g;
+    $dst =~ s/Я/Ja/g;
+    $dst =~ s/Ю/Ju/g;
+
+    # Lowercase file extensions
+    if ($dst =~ /^(.*)\.([^\.]+)$/) {
+        $dst = $1 . "." . lc $2;
+    };
+
+    # Change looking of track numbers
+    if ($dst =~ /^(\d+)[-.]_*(.+)$/) {
+        $dst = "$1.$2";
+    };
+
+    next if ($src_filename eq $dst);
+    print "$src -> $dst\n";
+    die "\"$dst\" exists" if -e $dst;
+    rename $src_filename, $dst;
+};
+closedir DIR;