#!/usr/bin/perl

##	exdebug: A general purpose Extended Accounting debugging assistant.
##		Owner: Ben Rockwood <benr@cuddletech.com>
##		License: Simplified BSD (See opensource.org for definition)
##		Purpose: As a more useful replacement for exdump.c in 
##			 the building of ExAcct tools.
##
##	Refer to /usr/include/sys/exacct_catalog.h for catalog dereference


use strict;
use warnings;
use Sun::Solaris::Exacct qw(:EXACCT_ALL);

die("Usage is $0 <exacct file>\n") unless (@ARGV == 1);




# Open the exact file and display the header information.
my $ef = ea_new_file($ARGV[0], &O_RDONLY) || die(error_str());
printf("Creator:  %s\n", $ef->creator());
printf("Hostname: %s\n\n", $ef->hostname());
 



# obj_counter is a helpful counter of each loop iteration.
my $obj_counter = 0;




# Dump the file contents
while (my $obj = $ef->get()) {								## Get an object from the EA File and reposition to next.
	print("---------------- OBJECT $obj_counter -----------------------\n");
	$obj_counter++;

	my $objectType = $obj->type();							## Return the object type (group or item) 
	my $objectCatalogObj =  $obj->catalog(); 					## Return a catalog object for this object
	my ($a, $b, $c) = $objectCatalogObj->value();					## Breakout the catalog triplet
	
	printf("Object is: %s   -   Catalog: %s %s %s\n", $objectType, $a, $b, $c );	## Output the catalog and object type for the object at hand.

	if ( $objectType == EO_GROUP ) {						## If the object is a group...
		my @objectList = $obj->value();						## Return the sub-group which will contain actual items.

		foreach my $item (@objectList) {
			my $itemsType = $item->type();					## Get type for new object.. is it a EO_ITEM or EO_GROUP?
			
			if ($itemsType == EO_ITEM){
				my $itemCatalog = $item->catalog();				## Return the "Catalog Object" for this object.
				my ($itemType, $itemGroup, $itemId) = $itemCatalog->value();	## Breakout the catalog triple
				my $itemValue = $item->value();					## Get the item value itself.

				print("\t\tId: $itemId \tValue: $itemValue\n");			## Now print just the catalog item id and item value.
			} else {
				print("\t\tERROR: Expected Item, got a group instead.\n");
			}
		}
	} else {
		print("  !! WARNING: Was looking for a group, didn\'t get one.\n");
	}	
	
}


# Report any errors
if (ea_error() != EXR_OK && ea_error() != EXR_EOF)  {
        printf("\nERROR: %s\n", ea_error_str());
        exit(1);
}

