#! /usr/bin/perl -w
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use strict;

# Update the module include path
BEGIN
{
    my $Dir = $0;
    if ( $Dir =~ /(.*)\/.*/ )
    {
	push @INC, "$1";
    }
}
use HawkeyePublish;
use HawkeyeLib;

# My Hawkeye object
my $Hawkeye;

main();

# Main
sub main
{
    $| = 1;
    Init();
    RunIt();
}

# Initialize
sub Init
{
    HawkeyeLib::DoConfig( );

    $Hawkeye = HawkeyePublish->new;
    $Hawkeye->Quiet( 1 );
    $Hawkeye->AutoIndexSet( 0 );
}

# ***********************************************
# Do the actual work
sub RunIt
{

    # Read the limits
    my $LimitMaxDirs = HawkeyeLib::ReadConfig(  "_MaxEntries", "20" );
    my $LimitMinSize = HawkeyeLib::ReadConfig(  "_MinSize", "100" );

    # Read the directory list (LIST)
    my $ConfigList = HawkeyeLib::ReadConfig(  "_list", "" );
    my @ConfigList = split( /[\s,]+/,
			    HawkeyeLib::ReadConfig(  "_list", "" ) );

    # Walk through them all
    my @OutList;
    foreach my $Entry ( @ConfigList )
    {
	my $Glob = HawkeyeLib::ReadConfig( "_".$Entry."_path", "" );
	if ( $Glob eq "" )
	{
	    print STDERR "No path for du list entry '$Entry'\n";
	    next;
	}
	my $LocalMaxDirs = HawkeyeLib::ReadConfig( "_".$Entry."_MaxEntries",
						   $LimitMaxDirs );
	my $LocalMinSize = HawkeyeLib::ReadConfig( "_".$Entry."_MinSize",
						   $LimitMinSize );

	# Build the local list
	my @LocalList;
	my @Glob = glob( $Glob );
	my $Total = 0;
	foreach my $Path ( @Glob )
	{
	    my $Cmd = "/usr/bin/du -s -k $Path";
	    if ( !open( DU, "$Cmd |" ) )
	    {
		print STDERR "Can't run du on '$Path'\n";
		next;
	    }
	    # All we care about is the last entry
	    my $Out = <DU>;
	    close( DU );
	    if ( !defined $Out )
	    {
		print STDERR "No output from du on '$Path'\n";
		next;
	    }
	    if ( $Out =~ /(\d+)\s+(.*)/ )
	    {
		my $Size = $1;
		my $r = ();
		$r->{Path} = $Path;
		$r->{Size} = $Size;
		$Total += $Size;
		if ( ( ! -d $Glob ) and ( $Size >= $LocalMinSize ) )
		{
		    $Path =~ s/.*\///g;
		    $Path =~ s/\W//g;
		    $Path =~ s/_//g;
		    $r->{Name} = $Entry . "_" .  $Path;
		    push( @LocalList, $r );
		}
	    }
	    else
	    {
		print STDERR "Error parsing output of du on '$Path'\n";
		next;
	    }
	}

	# Publish the total
	if ( $Total >= $LimitMinSize )
	{
	    my $r = ();
	    $r->{Path} = $Glob;
	    $r->{Name} = $Entry . "_TOTAL";
	    $r->{Size} = $Total;
	    push( @LocalList, $r );
	}

	my $NumEnt = 0;
	foreach my $r ( sort { $b->{Size} <=> $a->{Size} } @LocalList )
	{
	    push( @OutList, $r );
	    last if ( $NumEnt++ >= $LocalMaxDirs );
	}
    }

    # Now publish it all
    my $NumEnt = 0;
    foreach my $r ( sort { $b->{Size} <=> $a->{Size} } @OutList )
    {
	$Hawkeye->StoreValue( $r->{Name} . "_Kbytes",
			      $r->{Size},
			      HawkeyePublish::TypeNumber );
	$Hawkeye->StoreValue( $r->{Name} . "_Path",
			      $r->{Path},
			      HawkeyePublish::TypeString );
	last if ( $NumEnt++ >= $LimitMaxDirs );
    }

    $Hawkeye->Publish( );
}
