#!/usr/bin/perl

use strict;

my ($newfile, $origfile);
my ($dir, @files);

if (!defined ($ARGV[0])) {
  $dir = ".";
} else {
  $dir = $ARGV[0];
}

opendir (DIR, $dir) or die "Can't open $dir: $!";
@files = grep {/\.html/} readdir (DIR);
closedir (DIR);

foreach $origfile (@files) {
  $newfile = $origfile . ".new";

  open ORIGFILE, "<$dir/$origfile" or die "Can't open original file $origfile $!";
  open NEWFILE, ">$dir/$newfile" or die "Can't open new file $newfile $!";


  while (<ORIGFILE>) {
   s/\@/-at-/go;
   s/\.com/-dot-com/goi;
   s/\.org/-dot-org/goi;
   s/\.net/-dot-net/goi;
   print NEWFILE;
  }

  close NEWFILE;
  close ORIGFILE;

  rename ("$dir/$origfile", "${dir}/${origfile}.sav") or die "Can't rename old to new $!";
  rename ("$dir/$newfile", "$dir/$origfile") or die "Can't rename new to old $!";
}
