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

$source_dir = '/afs/sipb/project/www/root';
$dest_dir   = '/var/local/www';
$newdir     = '/var/local/www/.new';
$olddir     = '/var/local/www/root.old';

$rm_bin     = '/bin/rm';
$sync_bin   = '/afs/sipb/project/www/bin/serversync.pl';

if ( ((getpwuid($<))[0]) ne "wwwmaint") {
    print "This script should only be run as user 'wwwmaint'. Aborting.\n";
    exit(-1);
}

&main();

sub main {
    umask(002);

    &prepare_new_dir($newdir);

    system($sync_bin . " " . $newdir);

    #Make way... for prince ali...
    system($rm_bin . " -rf " . $olddir . " >/dev/null 2>&1");

    # Put the correct web.mit.edu homepage into the new directory
    system("/usr/local/bin/copy-from-web.pl .new");

    # Out with the old, in with the new...
    rename(($dest_dir . "/root"), $olddir);
    rename(($newdir . "/root"), ($dest_dir . "/root"));

}

sub prepare_new_dir {
    local($dirname) = @_;

    # There shouldn't be anything there at all
    system ($rm_bin . " -rf " . $dirname . " >/dev/null 2>&1");

    mkdir ($dirname, 0775);

    # This makes the new directory setgid and group-writeable.
    chmod (02775, $dirname);
    # The leading "0" is important -- it causes perl to interpret the
    # filemode as octal rather than decimal. Leaving out the "0" will
    # cause chmod to perform seemingly random chmod's, as it will interpret
    # a number as decimal which was intended as octal. e.g. "755" -->
    # "01363" ....

    # essentially "chgrp www $dirname"
    chown ($>, (getgrnam("www"))[2], $dirname);

}
