]> git.donarmstrong.com Git - bin.git/commitdiff
add sgf_rename
authorDon Armstrong <don@donarmstrong.com>
Wed, 3 Jan 2007 00:20:55 +0000 (00:20 +0000)
committerDon Armstrong <don@donarmstrong.com>
Wed, 3 Jan 2007 00:20:55 +0000 (00:20 +0000)
sgf_rename [new file with mode: 0755]

diff --git a/sgf_rename b/sgf_rename
new file mode 100755 (executable)
index 0000000..193a434
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+use IO::File;
+
+for my $sgf (@ARGV) {
+     my %sgf;
+     my $fh = new IO::File $sgf, 'r'
+         or warn "Unable to open $sgf for reading: $!"
+              and next;
+     while (<$fh>) {
+         chomp;
+         my ($field,$value) = $_ =~ /^([A-Z]{2})\[([^\]]+)\]/;
+         next unless defined $field;
+         if ($field =~ /^[WB]R$/) {
+              $value =~ s/\s+dan/d/;
+              $value =~ s/\s+pro/p/;
+              $value =~ s/\s+kyu/k/;
+         }
+         $sgf{$field} = $value;
+     }
+     undef $fh;
+     my $new_file = join('_',
+                        map {s/\s+/_/g;
+                             s/,//g;
+                             s/[_-]+/_/g;
+                             s/^_//;
+                             s/_$//;
+                             lc($_);
+                        }
+                        map{defined $_?($_):()}
+                        (@sgf{qw(EV RO PB BR)},'v',
+                         @sgf{qw(PW WR DT RE)}
+                        )
+                       ).'.sgf';
+     next if $sgf eq $new_file;
+     warn "$new_file already exists" and next if -e $new_file;
+     rename($sgf,$new_file);
+}