#!/usr/local/bin/perl5
$|=1;

### university required copyright stuff
##
##      Buildindex.pl, for Accheck v1.1
##      Mark Notarus, notarus@uiuc.edu
##      University of Illinois Computing and Communication Services
##
##      Copyright (c) 1998 by Mark Notarus, 
##       and the University of Illinois Board of Trustees.
##
##  Redistribution and use in source and binary forms, with or without
##  modification, are permitted provided that the following conditions
##  are met:
##
##  1.  Redistributions of source code must retain the above copyright
##      notice, this list of conditions and the following disclaimer.
##
##  2.  Redistributions in binary form must reproduce the above copyright
##      notice, this list of conditions and the following disclaimer in the
##      documentation and/or other materials provided with the distribution.
##
##  3.  All advertising materials mentioning features or use of this
##      software must display the following ack
##
##  THIS SOFTWARE IS PROVIDED BY THE TRUSTEES AND CONTRIBUTORS "AS IS"
##  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
##  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
##  PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE TRUSTEES
##  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
##  OF SUCH DAMAGE.
##


###############################################
######                                   ######
######                                   ######
###### Customizing for your site!        ######
######                                   ######
######                                   ######
###############################################

###
### Where are our accounting files stored?
### Note that this NEEDS the trailing /
$ACCTDIR="/areas/termserv/logs/accounting/";
###
### What's the prefix we tag on each file (eg, ACCOUNTING.Sep.25.gz)
$FILEPREFIX="ACCOUNTING";

### Where is the index file which will tell us which files encompass
### what times?
$INDEXFILE="/areas/termserv/logs/accounting/.index";   

### Where does the magic file program live on your system?
$BINFILE="/bin/file";
###############################################
######                                   ######
######                                   ######
######  End Site Customization           ######
######                                   ######
######                                   ######
###############################################



push(@INC,"/usr/local/lib/perl5");

require 5;
use Time::Local;
require "ctime.pl";

open (TEST,">/tmp/.buildindex");
print TEST &ctime(time);
close TEST;

#### 
#### Months
%months=(
    Jan => 0, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6,
    Feb => 1, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11,
    jan => 0, mar => 2, apr => 3, may => 4, jun => 5, jul => 6,
    feb => 1, aug => 7, sep => 8, oct => 9, nov => 10, dec => 11,
	 );



opendir(THEDIR,$ACCTDIR);
@FILES=grep /^$FILEPREFIX/,readdir(THEDIR);
close(THEDIR);

if (!-e $INDEXFILE){
    &BuildIndex;
}
open (INDEX,">>$INDEXFILE") || print "Error opening $INDEXFILE\n";
chdir $ACCTDIR;
while ($file=shift @FILES){
#    print "Parsing $file\n";
    # First, see if we already have this file logged.
    next if &HasBeenParsed($file);
    if (&IsGzipped($file)){
	open(THEFILE,"gunzip -c $file |") || print "Error opening $file\n";
    }
    else {
	open(THEFILE,"< $file")|| print "Error opening $file\n";
    }
    if (eof(THEFILE)){
	next;
    }
    undef $b;
    while (!$b){
	$a=<THEFILE>;
	chop;
	$b=$a;
	$b=~s/\s+//g;
    }
    #Sun Apr 12 23:50:20 1998        
    ($wday,$mon,$day,$hms,$year,@other)=split(/\s+/,$a);
    ($hour,$min,$sec)=split(/:/,$hms);
    $starttime=timelocal($sec,$min,$hour,$day,$months{$mon},$year);
    undef $a; undef $b;
    #scream through the file. Hold onto lines w/ content.
    while(<THEFILE>){
	chop;
	$line=$_;
	$b=$line;
	$b=~s/\s+//g;
	$a=$line if $b;
    }
    ($wday,$mon,$day,$hms,$year,@other)=split(/\s+/,$a);
    ($hour,$min,$sec)=split(/:/,$hms);
    $endtime=timelocal($sec,$min,$hour,$day,$months{$mon},$year);

    # output!
    $a=&ctime($starttime);
    $b=&ctime($endtime);
    $a=~s/US\/Central//;
    $b=~s/US\/Central//;
    chop $a;
    chop $b;
    print INDEX "$starttime\t$endtime\t$file\t$a\t$b\n";



}
chmod 777,$INDEX;
close INDEX;

	

    
	




### be smarter than just checking for .gz!
sub IsGzipped{
    local($THEFILE)=@_;
### Use /bin/file to see if this is a gzipped file.
    open (FILECHECK,"$BINFILE $THEFILE |");
    while(<FILECHECK>){
	return 1 if /gzip/;
    }
    ### Silly check for any host who's /bin/file is too old to recognize gzip
    if ($THEFILE =~/\.gz$/){
	return 1;
    }
    return 0;
}


sub BuildIndex{
    # just write a header to the index file.
    open(INDEX,">$INDEXFILE");
    print INDEX "#start epoch\tend epoch\tfile\tstart time\tend time\n";
    close INDEX;
}

sub GetParsedList{
    local($st,$et,$file,$st2,$et2);
    # this function just goes through the index file and makes sure we don't waste time
    # scanning a file that's already been indexed.
    open (CHECKINDEX,"<$INDEXFILE");
    while(<CHECKINDEX>){
	next if /^#/;
	next if !/\t/; # skip if there's no tabs!
	($st,$et,$file,$st2,$et2)=split(/\t/,$_);
	$filelist{$file}=1;
    }
}

sub HasBeenParsed{
    local($theFile)=@_;
   if (!defined(%filelist)){
	&GetParsedList;
    }
    return $filelist{$theFile};
}
