#!perl
#line 14
#
# This program changes the server name in the CVS/Root files.
#
# Need the full path from the root of this drive for the Entering directory
# messages.
use strict;

my $rootd=qx{cd};
chop($rootd);
$rootd =~ s/[a-zA-Z]://;
$rootd =~ s/\\/\//g;
my @dir_stack;
my @rdir_stack;
sub pushd {
    my($d) = @_;
    my($cwd) = qx{cd};
    chop($cwd);
    push(@dir_stack, $cwd);
    # Want a relative one as well
    push(@rdir_stack, $d);
    # This is for emacs compilation mode so it knows which directory files are
    # in.
    my $i;
#    print "\nchserver: Entering directory `" . $rootd . "/";
    for ($i=0; $i<=($#rdir_stack-1); $i++) {
	# Ignore path name . as it is superfluous.
#	print $rdir_stack[$i] . "/" if ($rdir_stack[$i] ne ".");
    }
#    print $rdir_stack[$#dir_stack] . "'\n";
    chdir($d) || die "Could not chdir($d)\n";
}

sub popd {
    my $d = pop(@dir_stack);
    pop(@rdir_stack);
    chdir($d) || die "Could not chdir($d)\n";
}

sub dodir {
    my ($dir, $level) = @_;
    local(*D);
    if (!opendir(D, $dir)) {
        print "Failed to open $dir\n";
        return
    }
    my(@allfiles)=sort grep(!/^\.\.?$/,readdir(D));
    closedir(D);
    foreach my $file (@allfiles) {
        if (-d "$dir/$file") {
            if (lc($file) eq "cvs") {
                print "$dir";
                local(*IN);
                local(*OUT);
                &pushd("$dir/$file");

                if (!-f "Root") {
                    print "WARNING: Didn't find $dir/$file/Root\n";
                } else {
                    my $haschanged = 0;
                    if (open(IN, "<Root")) {
                        if (open(OUT, ">tmp")) {
                            while (<IN>) {
                              if (/merlot/) {
                                s+/usr/local/repository+/cvs/controls+;
                                s+/usr/local/us_repository+/cvs/usa+;
                                s/\@merlot/\@cvs/;
                                $haschanged = 1;
                              }
                              print OUT;
                            }
                            close(OUT);
                        }
                        close(IN);
                        if ($haschanged) {
                          unlink "Root";
                          rename "tmp", "Root";
                          print " (Root)";
                        } else {
                          unlink "tmp";
                        }
                    }
                }

                # Make sure that the path in this file is relative to the
                # repository root.
                if (!-f "Repository") {
                    print "WARNING: Didn't find $dir/$file/Repository\n";
                } else {
                    my $haschanged = 0;
                    if (open(IN, "<Repository")) {
                        if (open(OUT, ">tmp")) {
                            while (<IN>) {
                              if (/^\/usr\/local\/repository\//) {
                                s+++;
                                $haschanged = 1;
                              }
                              if (/^\/usr\/local\/us_repository\//) {
                                s+++;
                                $haschanged = 1;
                              }
                              print OUT;
                            }
                            close(OUT);
                        }
                        close(IN);
                        if ($haschanged) {
                          unlink "Repository";
                          rename "tmp", "Repository";
                          print " (Repository)";
                        } else {
                          unlink "tmp";
                        }
                    }
                }

                &popd;
                print "\n";
            } else {
                &dodir("$dir/$file", $level + 1);
            }
        }
    }
}

&dodir(".", 1);
