#!/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{/}{}g; 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'; print "Renaming $sgf to $new_file\n"; next if $sgf eq $new_file; warn "$new_file already exists" and next if -e $new_file; rename($sgf,$new_file); my $psgz = $sgf; $psgz =~ s/\.sgf/.ps.gz/; if (-e $psgz) { my $new_psgz = $new_file; $new_psgz =~ s/\.sgf/.ps.gz/; rename($psgz,$new_psgz); } }