#!/usr/bin/perl 

$userName =  $ENV{'LOGNAME'};
print "Using '$userName' as the username\n";

$theName = $userName . "Assignments";

use File::Find;

sub eachFile {
  my $filename = $_;
  my $fullpath = $File::Find::name;

  if (-e $filename && (substr($_, -5) eq "gwsdl" || (substr($_, -3) eq "xml") ||
      substr($_, -4) eq "java" || substr($_, -4) eq "wsdd" ||
      $_ eq "build.xml" || $_ eq "namespace2package.mappings")) { 
    open (NAMESPACE, "$_");
    $content = "";
    while ($read  = <NAMESPACE>) {
      $content .= $read;
    }
    close (NAMESPACE);
    print "Editing $_ for content...";
    $content =~ s/assignments/$theName/g;
    open (NEWSPACE, ">$_");
    print NEWSPACE $content;
    close (NEWSPACE);
    print "\t[DONE]\n";
  } 
}

find (\&eachFile, "./");

print "\n\nRenaming directory assignments username to $userName" . "Assignments...";
if (rename ("assignments", $userName . "Assignments")) {
  print "\t[DONE]\n";
} else {
  print "\t[FAILED]\n";
  $failed = "true";
}

print "Renaming directory schema/username to schema/$userName...";
if (rename ("schema/assignments", "schema/$userName" . "Assignments")) {
  print "\t[DONE]\n";
} else {
  print "\t[FAILED]\n";
  $failed = "true";
}

if ($failed) {
  print "\n\nA Directory change failed during execution";
}

print "\nCOMPLETED";
