#!/usr/bin/perl

open (STDERR, ">&STDOUT");
select ((select(STDOUT), $| = 1)[0]); # Synchronize STDOUT
select ((select(STDERR), $| = 1)[0]); # with STDERR

($dns_lib = $0) =~ s:(^|/)[^/]*$::;
($script=$0) =~ s:^.*/([^/]*)$:$1:;	# Get the simple name of this script.

push (@INC, $dns_lib);

require 5.0;
require 'dns_lib.perl';

umask 077;

open (UNAME, "$uname |");
$nodename = <UNAME>;
chomp $nodename;

foreach $server (sort (keys (%downstream))) {

    if ($downstream{$server}->{'data_from'} eq $nodename) {

	push (@downstream, $server);

    }

}

if (-e "$restarted") {

    print "Restart file found.\n";
    print "Building .netrc...\n";

    foreach $site (@downstream) {

	open (NETRC, ">$dns_home/.netrc") || die "Cannot create $dns_home/.netrc: $!\n";

	print NETRC "machine $site\n";
	print NETRC "login $user\n";
	print NETRC "password $pass\n";
	print NETRC "macdef init\n";
	print NETRC "cd $dns_logs\n";
	print NETRC "put $restarted .reboot\n";
	print NETRC "bye\n";
	print NETRC "\n";

	close NETRC;
    
	print "FTPing...\n\n";
	$status = system "$ftp", "$site";
	unlink "$dns_home/.netrc";

    }

    unlink "$restarted";

}

exit;

