#!/usr/bin/perl -w
use File::Copy;
# get rid of parens around sci. names (may have side effects)

# input record separator a tag not a newline

# program to search and replace strings in html files
#my $olddir = "./pictures";
#my $newdir = "./clnpics";
#my $olddir = "./gigs";
#my $newdir = "./clngigs";
#my $olddir = "./articles";
#my $newdir = "./clnarts";
#my $olddir = "./setlists";
#my $newdir = "./clnset";
#my $olddir = "./posters";
#my $newdir = "./clnpost";
my $olddir = "./combined";
my $newdir = "./clncombined";
opendir(DIR, "$olddir") || die("cannot open $olddir");
foreach $file (readdir(DIR)) {
my $new = $file;
  print STDOUT "1. $new\n";

$new =~ s/PNG/png/g;
$new =~ s/JPG/jpg/g;
$new =~ s/jpeg/jpg/g;
$new =~ s/GIF/gif/g;
$new =~ s/PDF/pdf/g;
$new =~ s/TXT/txt/g;
$new =~ s/htm/html/g;
$new =~ s/\s+/_/g;
$new =~ s/#//g;
$new =~ s/!//g;
$new =~ s/'//g;
$new =~ s/,/_/g;
# these are all the metachars to remove:
$new =~ s/\^/_/g;
$new =~ s/\$/_/g;
$new =~ s/\*/_/g;
#$new =~ s/\+/_/g;
$new =~ s/\?/_/g;
$new =~ s/\(/_/g;
$new =~ s/\)/_/g;
$new =~ s/\[/_/g;
$new =~ s/\]/_/g;
$new =~ s/\{/_/g;
$new =~ s/\}/_/g;
$new =~ s/\\/_/g;
$new =~ s/\|/_/g;
# other fixes
$new =~ s/\@/_at_/g;
$new =~ s/\&/_and_/g;
$new =~ s/___/_/g;
$new =~ s/__/_/g;
$new =~ s/(\D)\+(\D)/$1_and_$2/g;
$new =~ s/--/-/g;
$new =~ s/_-/_/g;
$new =~ s/-_/_/g;
$new =~ s/^A-Za-z0-9-_.//g;
if ($new  =~ /^(\d\d\d\d)-(\d\d)-(\d\d)(\D.*)(\.jpg|\.png)/) {
  print STDOUT "2a1. KEEP SAME: $new\n\n";
  #$new = $1 . "-" . $2 . "-" . $3 . $4;
  print STDOUT "2b1. KEPT SAME: $new\n\n";
}
elsif ($new  =~ /^(\d\d\d\d)-(\D.*)(\.jpg|\.png)/) {
  print STDOUT "2a. $new\n\n";
  $new = $1 . "-xx-xx-" . $2 . $3;
  print STDOUT "2b. $new\n\n";
}
elsif ($new  =~ /^(\d\d\d\d)-(\d\d)(\D.*)(\.jpg|\.png)/) {
  print STDOUT "2a1. $new\n\n";
  $new = $1 . "-" . $2 . "-" . $3 . $4;
  print STDOUT "2b1. $new\n\n";
}
elsif ($new  =~ /^(\d\d\d\d)(\D.*)(\.jpg|\.png)/) {
  print STDOUT "2a2. $new\n\n";
  $new = $1 . "-xx-xx-" . $2 . $3;
  print STDOUT "2b2. $new\n\n";
}
elsif ($new  =~ /^(\d\d)(\D.*)(\.jpg|\.png)/) {
  print STDOUT "2a3. $new\n\n";
  $new = "19" . $1 . "-xx-xx-" . $2 . $3;
  print STDOUT "2b3. $new\n\n";
}
#if ($new  =~ /(A-Za-z.*?)(-|_)(.*)(\.jpg|\.png|.gif|\.htm)/) {
elsif ($new  =~ /^(\D\D.*)/) {
  $new = "19xx-xx-xx_" . $1;
  #print STDOUT "2. $new\n\n";
}
$new =~ s/-xx-xx--xx-xx-/-xx-xx-/g;
$new =~ s/-xx-xx-xx-xx-/-xx-xx-/g;
$new =~ s/--/-/g;
$new =~ s/xx-_/xx-/g;
$new =~ s/xx_/xx-/g;
$new =~ s/__/_/g;
  print STDOUT "2. $new\n\n";

copy("$olddir/$file","$newdir/$new");
}
close (DIR);
#rename($old, "old.orig");
#rename($new, $old);
