#!/usr/local/bin/perl -w
use strict;
# By: Ventz Petkov
# Date: 7-13-05
##################################################

use File::Find::Rule;
#use Text::Diff;
my $LIVE_SERVER = "/webvols/webs/live-htdocs";
my $DEV_SERVER = "/webvols/webs/dev-htdocs";
our (@livestorage, @devstorage, @missinginlive, @missingindev, @difffiles);

##################################################

sub linebreak() {
    my $title = shift;
    print "\n\n\n";
    print "#" x 25;
    print " $title ";
    print "#" x 25;
    print "\n";
}

##################################################

sub listdirs() {
    my %args = @_;
    my @arrayofdirs = @{$args{ "dirs" }};
    my $identity = $args { "ident" };

    foreach my $dir (@arrayofdirs) {
        print "$dir\n";
        opendir(DIR, $dir) or die "Can't Open $dir: $!";
        while( defined(my $file = readdir(DIR)) ) {
            unless ($file eq "." || $file eq "..") {
                my $filename = $dir."/".$file;
                print "\t$filename\n";
                if ($identity eq "live") { push(@livestorage, $filename); }
                else { push(@devstorage, $filename); }
            }   
        }   
    }
}

##################################################

sub diffdirs() {
    my %args = @_;
    my @arraystorage = @{$args{ "filesdirs" }};
    my $identity = $args{ "ident" };
   
    foreach my $linef1 (@arraystorage) {
        my $linef2 = $linef1;
        if ($identity eq "live") {
            $linef2 =~ s/live-htdocs/dev-htdocs/;
            if (-e $linef2) {
                chomp($linef1); chomp($linef2);
                my $size1 = (stat($linef1))[7]; my $size2 = (stat($linef2))[7];
                if ($size1 != $size2) { push(@difffiles, $linef1); }
            }
            else { push(@missingindev, $linef1); }
        }
        else {
            $linef2 =~ s/dev-htdocs/live-htdocs/; unless (-e $linef2 ) { push(@missinginlive, $linef1); }
        }

    }
}

##################################################

sub filestats() {
    my $line = shift;
    my ($owner, $ownerid, $size, $date, $temp);
    $owner = "N/A"; $ownerid = "N/A"; $size = "N/A"; $date = "N/A"; $temp = "N/A";
    unless (-d $line) {
        chomp($line); $line =~ s/\"//g;
        $ownerid = (stat($line))[4];
        unless(getpwuid($ownerid)) { $owner = $ownerid; }
        else { $owner = getpwuid($ownerid); }
        $size = (stat($line))[7];
        $date = localtime((stat($line))[9]);
        print "$line \t\t [ Owner: $owner || Size: $size B || $date ]\n";
        #return "$line \t\t [ Owner: $owner || Size: $size B || $date ]\n";
    }
}

##################################################

sub printinfo() {
    my %args = @_;
    my @missing = @{$args{ "missing" }};
    my $identity = $args{ "ident" };
    my ($line, $owner, $size, $date);   
    print "Things Only in [ $identity-htdocs ]:\n";
    print "-------------------------------\n";
    foreach $line (@missing) { &filestats($line); } print "\n\n";
}    

##################################################

sub printdiffs() {
    foreach my $missing (@difffiles) {
        my $missing2 = $missing; $missing2 =~ s/live-htdocs/dev-htdocs/;
        $missing = &filestats($missing); $missing2 = &filestats($missing2);
        print "\n\n";
    }
}

##################################################

# START
my @livedirs = File::Find::Rule->directory->in( $LIVE_SERVER );
my @devdirs = File::Find::Rule->directory->in( $DEV_SERVER );

my %livestuff = ("dirs" => \@livedirs, "ident" => "live");
my %devstuff = ("dirs" => \@devdirs, "ident" => "dev");
&linebreak("All Things in live-htdocs"); &listdirs(%livestuff);
&linebreak("All Things in dev-htdocs"); &listdirs(%devstuff);

%livestuff = ("filesdirs" => \@livestorage, "ident" => "live");
%devstuff = ("filesdirs" => \@devstorage, "ident" => "dev");
&diffdirs(%livestuff); &diffdirs(%devstuff);

%livestuff = ("missing" => \@missingindev, "ident" => "live");
%devstuff = ("missing" => \@missinginlive, "ident" => "dev");
&linebreak("FILES ONLY IN ONE OF THE TWO PLACES");
&printinfo(%livestuff); &printinfo(%devstuff);

&linebreak(" FILES THAT DIFFER IN CONTENT");
printdiffs();
&linebreak(" END OF WEB_DIFF.PL REPORT");
