#! /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;

# Global Hawkeye data
my $Hawkeye;

Init();
RunIt();

sub Init {
    HawkeyeLib::DoConfig( );

    # Setup the hawkeye stuff
    $Hawkeye = HawkeyePublish->new;
    $Hawkeye->Quiet( 1 );
    $Hawkeye->AutoIndexSet( 0 );
}

sub RunIt {

    ###
    ### Get & parse the output of 'w'
    ###

    my $Hash = HawkeyeHash->new( \$Hawkeye, "" );
    my $Cmd = "w";

    # Run it, parse it
    Carp::confess( "Can't run w '$Cmd'" ) if ( ! open ( W, "$Cmd|" ) );
    my @UserList;
    my $NumUsers = 0;

    # Read & parse the summary line
    $_ = <W>;
    my %Uptime;
    HawkeyeLib::ParseUptime( $_, \%Uptime );
    foreach my $Attr ( keys %Uptime )
    {
	$Hash->Add( "Uptime" . $Attr,
		    HawkeyePublish::TypeAuto,
		    $Uptime{$Attr} );
    }

    # Read & parse the header line
    $_ = <W>;
    my %Fields;
    {
	my @Fields = split;
	my $Prev = "";
	foreach my $n ( 0 .. $#Fields )
	{
	    my $Field = $Fields[$n];
	    my $Offset= index( $_, $Field );
	    $Field =~ s/\W//g;
	    $Field = lc ($Field);
	    $Fields[$n] = $Field;
	    $Fields{$Field}{Offset} =
		( $Offset < 2 )  ?  0  :  ( $Offset - 2 );
	    my $Len = length( $Field );
	    $Fields{$Field}{Length} = $Len;
	    $Fields{$Field}{End} = $Offset + $Len - 1;
	    $Fields{$Field}{Next} =
		( $n == $#Fields )  ?  ""  :   $Fields[$n+1];
	    $Fields{$Field}{NextOff} = -1;
	    if ( $Prev ne "" )
	    {
		$Fields{$Prev}{NextOff} = $Fields{$Field}{Offset};
	    }
	    $Prev = $Field;
	}
	$Hash->Add( "FIELDS", HawkeyePublish::TypeString, join( " ", @Fields ) );
    }

    # Read the data lines
    my $Num = 0;
    while( <W> )
    {
	$Num++;
	my $Num_ = $Num . "_";
	chomp;

	foreach my $Field ( keys %Fields )
	{
	    my $f = $Fields{$Field};

	    # Find the start of the field
	    my $Start = -1;
	    foreach my $Offset ( $f->{Offset} .. $f->{End} )
	    {
		if ( substr( $_, $Offset, 1 ) ne " " )
		{
		    $Start = $Offset;
		    last;
		}
	    }

	    # Find it's end (if it has a start!)
	    my $End = -1;
	    if ( $Start >= 0 )
	    {
		my $Last =
		    ($f->{NextOff} >= 0)  ?  $f->{NextOff}  :  length( $_ );
		my $Offset;

		# Search backwards for non blank
		for ( $Offset = $Last ;  $Offset >= $Start;  $Offset-- )
		{
		    if ( substr( $_, $Offset, 1 ) ne " " )
		    {
			$End = $Offset;
			my $Len = $End - $Start + 1;
			my $Str = substr( $_, $Start, $Len );
			$Hash->Add( $Num_ . $Field,
				    HawkeyePublish::TypeString,
				    $Str );
			last;
		    }
		}
	    }
	}

	$Hawkeye->StoreIndex( $Num );
    }
    close( W );

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

    # Publish
    $Hawkeye->Publish( );
}
