#!/afs/athena/contrib/perl/p

require "find.pl";

# -r causes make to forget about its built-in rules.
# 
$source_dir  = '/afs/sipb/project/www/bin/from';
$dest_dir    = '/afs/sipb/project/www/bin/to';

$make_bin    = '/mit/gnu/bin/gmake -r' ;
# $make_bin    = '/mit/gnu/bin/gmake -r -j10' ;
$cp_bin      = '/mit/gnu/bin/gcp';
$mkdir_bin   = '/mit/gnu/bin/gmkdir';
$chmod_bin   = '/bin/chmod';

@Wdirs  = ();
@Wfiles = ();
@Wlinks = ();

&initialize();

chdir $source_dir;
&find("./");
chdir $dest_dir;

# No need to worry about RCS directories or their contents.
@Wdirs  = grep(!/\/RCS\/?$/, @Wdirs );
@Wfiles = grep(!/\/RCS\//  , @Wfiles);

# No need to worry about emacs backup files.
@Wfiles = grep(!/\~$/      , @Wfiles);

open (MF, ">GNUMakefile");

&print_mf_header(MF);

print MF "WLINKS=" . join(" ", @Wlinks) . "\n\n" ;
print MF "WFILES=" . join(" ", @Wfiles) . "\n\n" ;
# print MF "WDIRS="  . join(" ", @Wdirs)  . "\n\n" ;

print MF "\n";

&print_mf_filerules(MF);
close(MF);

system($make_bin . " -f GNUMakefile files");


# This is a magic subroutine used by find.pl to do things with the files it
# looks at. To expand this, consider looking at the output of find2perl.
# Basically, it tests the file's type, and pushes it into an array depending on
# what it is.
sub wanted {
    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))
	&& (( -d _ && push(@Wdirs,  $name)) ||
	    ( -f _ && push(@Wfiles, $name)) ||
	    ( -l _ && push(@Wlinks, $name)));
}

# Attach gnu locker. Needed for gmake.
sub initialize {
    system('/bin/athena/attach -q -n -h gnu') &&
	die("Error -- could not attach gnu locker. Server update script " .
	    "aborting.\n");

}

sub format_time {
    local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	grep(&padzeros($_), localtime());
    return($hour . ":" . $min . ":" . $sec . " " .
	   $mon . "/" . $mday . "/" . $year);
}

# if a number is less than 10, add a zero to the front. More or less. :-)
sub padzeros {
    $_ = ($_ < 10)?("0" . $_):$_;
}

sub print_mf_header {
    local ($mfl) = @_;
    print $mfl "# This Makefile was created by an automated script, written\n";
    print $mfl "# by nocturne@mit.edu, for use in updating local files on\n";
    print $mfl "# www.mit.edu from an AFS directory tree.\n#\n";
    print $mfl "# Makefile generated: " . &format_time() . "\n#\n";
    print $mfl "# This Makefile may only be usable by gmake. Deal. :-)\n#\n";
    print $mfl "# (In general practice, this file should only be used from\n";
    print $mfl "#  within the server update script. (i.e., If you can read\n";
    print $mfl "#  this, you're too close. ;-) ))\n#\n\n";
}

sub print_mf_filerules {
    local($mfl) = @_;

    print $mfl "files: \$(WFILES)\n";

    print $mfl "% : " . $source_dir . "/%\n";
    print $mfl "\t" . $cp_bin . " -p --no-dereference \$\< \$\@\n";
    print $mfl "\t" . $chmod_bin . " a+r \$\@\n";
}

sub print_mf_dirrules {
    local($mfl) = @_;

    print $mfl "dirs: $(WDIRS)\n";
    print $mfl "\t" . $mkdir_bin . " -p \n";
    print $mfl "\t" . $chmod_bin . "";
}
