#!/usr/bin/perl -w

#
# this script does different things depending on how it was named (or
# a symlink) if the name includes:
# force   - the whole site is rebuilt
# pdf     - builds pdfs
# index   - builds the index
# verbose - turn the verbose mode on
#
# the easiest way is to use symlinks to the same script
#
# by default it only updates the changed files

use strict;

my $src = "/home/perlwww/apache.org/modperl-docs";
my $rel = "$src/dst_html";
my $dst = "/www/perl.apache.org";

my %fs = (
    'minotaur.apache.org' => '/x1',
    'daedalus.apache.org' => '/x2',
);

umask 0002;

my $HOME = $ENV{HOME};
$ENV{PATH} = "/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/bin:/usr/X11R6/bin:$HOME/bin";

$ENV{PERL5LIB} = "$HOME/lib/perl5/5.00503:$HOME/lib/perl5/site_perl/5.005:$HOME/lib/perl5/site_perl:$HOME/lib/perl5";

chdir $src;
# Do different things depending on our name
my($name) = $0 =~ m|([^/]+)$|;

update($name);

# we must not try to sync the files when the filesystem is full, or
# the whole online version may get rendered broken, since some files
# will be simply truncated to 0
die "online fs is full, cannot sync the online site" if fs_is_full();

online_sync();

sub update {
    my($name) = shift;

    my $reindex = $name =~ /index/ ? 1 : 0;

    my $flags = '';
    $flags .= 'f' if $name =~ /force/;
    $flags .= 'd' if $name =~ /pdf/;
    $flags .= 'v' if $name =~ /verbose/;
    $flags = $flags ? "-$flags" : "";

    system("cvs up -dP >/dev/null 2>&1");

    system("bin/build $flags");

    system("bin/makeindex") if $reindex;
}

sub online_sync {
    # for some reason we are having problems to preserve the timestamps :(
    # system("cp -pr $rel/ $dst/");
    system("cp -r $rel/ $dst/");
    system("find $dst -type d -exec chmod g+rwx {} \\; >/dev/null 2>&1");
    system("find $dst -type f -exec chmod g+wr {} \\;  >/dev/null 2>&1");
}

sub fs_is_full {

    require Sys::Hostname;
    my $hostname = Sys::Hostname::hostname();

    # don't check on local systems
    return 0 unless $hostname && $fs{$hostname};

    # get available disk space
    my $disk = qx{ df | grep $fs{$hostname} };
    my ($disk_avail) = ($disk =~ /(\d+)\s+\d+\%/); # avail is before capacity

    # get size of site
    my $site = qx{ du -c $rel | grep total };
    my ($site_size) = ($site =~ /^(\d+)/);

    # give us a margin of 50MB
    return 0 unless $site_size + 50*1024 > $disk_avail;

    warn "site_size = $site_size , disk_avail = $disk_avail\n";
    return 1;
}
