#! /usr/bin/env perl
##**************************************************************
##
## 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;
use warnings;


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

# Setup the hawkeye stuff
my $Hawkeye;

# Do it
Init();
RunIt();

# Initization logic
sub Init {
    HawkeyeLib::DoConfig( );

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

# Run time logic
sub RunIt {
    my $Hash = HawkeyeHash->new( \$Hawkeye, "" );

    my $Tmp = HawkeyeLib::ReadConfig( "_repositories", "" );
    my @Repositories = split( /,/, $Tmp );
    die "No repositories specified" if ( $#Repositories < 0 );

    # Lock fields that we publish
    my %Fields = (
		  Full => HawkeyePublish::TypeString,
		  Dir => HawkeyePublish::TypeString,
		  Type => HawkeyePublish::TypeString,
		  Host => HawkeyePublish::TypeString,
		  Pid => HawkeyePublish::TypeNumber,
		  Uid => HawkeyePublish::TypeNumber,
		  User => HawkeyePublish::TypeString,
		  Age => HawkeyePublish::TypeNumber,
		);

    # Store off the fields
    $Hash->Add( "FIELDS", HawkeyePublish::TypeString, join( " ", keys %Fields ) );

    # List of valid repositories
    my @CheckedRepositories;

    # Walk through the repositories
    foreach my $Repository ( @Repositories )
    {
	my ( $RepoName, $RepoDir ) = split( /:/, $Repository );
	if ( ! defined $RepoDir )
	{
	    print STDERR "Can't parse repository '$Repository'\n";
	    next;
	}

	# Step 1: Change to the directory...
	if ( ! chdir( $RepoDir ) )
	{
	    print STDERR "Can't chdir to $RepoDir\n";
	    next;
	}

	# Find locks
	my $Now = time( );
	my $Find = "find . -name '#cvs*' -print";
	if ( ! open( FIND, "$Find|" ) )
	{
	    print STDERR "Error running '$Find' in '$RepoDir'\n";
	    next;
	}

	# Add it to the list of checked repositories
	push( @CheckedRepositories, $RepoName );

	# Look through it...
	my @Locks;
	while( <FIND> )
	{
	    # Skip empty lines (shouldn't be any, but prudent)
	    chomp;
	    next if ( ! /\S/ );

	    # Parse it
	    if ( /(.*)\/\#cvs\.(\w+)\.(.*)\.(\d+)$/ )
	    {
		my @Stat = stat( $_ );
		next if ( $#Stat < 9 );

		my $Lock = ();
		$Lock->{Dir} = $1;
		$Lock->{Type} = $2;
		$Lock->{Host} = $3;
		$Lock->{Pid} = $4;
		$Lock->{Uid} = $Stat[4];
		$Lock->{Age} = ($Now - $Stat[9]);
		$Lock->{User} = getpwuid( $Stat[4] );
		$Lock->{Full} = $_;
		push( @Locks, $Lock );
		$Lock = ();
	    }
	    else
	    {
		print STDERR "Failed to parse '$_'\n";
		next;
	    }
	}
	close( FIND );

	# Publish bas info
	$Hash->Add( $RepoName . "_locks",
		    HawkeyePublish::TypeNumber,
		    $#Locks + 1 );
	$Hash->Add( $RepoName . "_directory",
		    HawkeyePublish::TypeString,
		    $RepoDir );

	# Publish lock info
	for my $LockNo ( 0 .. $#Locks )
	{
	    $Hawkeye->StoreIndex( $LockNo );
	    my $LockName = $RepoName . "_" . $LockNo. "_";
	    my $Lock = $Locks[$LockNo];
	    foreach my $Key ( keys %{$Lock} )
	    {
		my $Type = HawkeyePublish::TypeString;
		if ( ! exists $Fields{$Key} )
		{
		    print STDERR "Warning: Lock field '$Key' unknown\n";
		}
		else
		{
		    $Type = $Fields{$Key};
		}
		$Hash->Add( $LockName . $Key, $Type, $Lock->{$Key} );
	    }
	}

    }

    $Hash->Add( "REPOSITORIES",
		HawkeyePublish::TypeString,
		join( " ", @CheckedRepositories ) );

    # Ok, summary is done...
    $Hash->Store( );
    $Hawkeye->Publish( );
}
