#!/usr/bin/perl

my $host = 'simba.cs.uct.ac.za';

# clean up files
sub checkdir
{
   my ($directory) = @_;

   opendir (my $dir, $directory);
   my @files = readdir ($dir);
   closedir ($dir);   
   
   foreach my $afile (grep { /[a-zA-Z0-9]/ } @files)
   {
      if (-d $directory.'/'.$afile)
      {
         checkdir ($directory.'/'.$afile);
      }
      else
      {
         print 'Checking: '.$directory.'/'.$afile."\n";
         
         my $res = `head -2 $directory/$afile | grep -ciw DOCTYPE`;
         if ($res > 0)
         {
            print "Found DOCTYPE ... removing\n";
            system ("mv $directory/$afile /tmp/check");
            system ("tail -n +3 /tmp/check > $directory/$afile");
            system ("rm /tmp/check");
         }
         
         $res = system ('xmllint --noout '.$directory.'/'.$afile);
         if ($res > 0)
         { print "ERROR!!!\n" }
      }
   }
}

checkdir ($host);
