# Program to Parse the ISS Policy File and Create a Table of Policys
# Produces an output file in Comma Seperated Format
# The Output file will be used to input to a database or Excel Spreadsheet
# Marc Class 	MNET Australia Pty. Ltd.

# Initialise Variables
				
my $event; 			# The Event 
my $printevent="";
my $eventflag=0;		# Event Found Flag (For first time event)

my $enabled;		# Wether the event is Enabled 
my $enabledflag=0;	# enabled flag
my $printenabled="";

my $printdesc="";

my $priority;		# Event Priority
my $printpriority="";

my $logdb;			# Logdb Setting
my $logdbflag;		# Logdb Flag
my $printlogdb="";

my $display;		# Display Setting
my $dispflag=0;		# Display Flag
my $printdisplay="";

my $rskill;			# RSKILL Setting
my $printrskill="";

# Print Heading

print  "Event,Description,Enabled Flag,Priority,Display Setting,Log Setting, Kill Response\n";

while (<>)  
{
	chomp $_;	# Remove end of line Newlines

	if	(/\[\\template\\connections\\\];*/)	# Break out when we get to the Connections section 
	{
		$printevent=$event;
		printline ();		# Print last Line
		last;				# Exit While Loop
	}
	
	if 	(/\[\\template\\features\\\];*/)	# Skip the first occurence of [\template\features\];
	{
		next;
	}

	if	(/\[\\template\\features\\(\w+)\\\];*/)		# Contains \template\features\....
	{
		unless	(/Response/)
		{
			$printevent=$event;		# Save the previous Event for printing		
			$event=$1;				# Set the event to the new value
			
			if 	($eventflag eq 1)		# We have hit a New event
			{
				printline ();		# Print the previous events information
			}
			else					# Its a The FIRST event
			{
				$eventflag=1;
			}
			next;
		}
	}

	if	(/^Enabled\s+=B\s+(\w+);*/)		# Begins with Enabled = B and ends in ;
	{
		if 	($enabledflag eq 0) 		# Is it the first time Enabled is found for this Event ?
		{
			$enabled = $1;
			if	($enabled eq 1)
			{
				$printenabled = "Enabled";		
			}
			else
			{
				$printenabled = "Disabled";
			}
			$enabledflag = 1;
			next;
		}	
	}

	if	(/^CheckDescription\s+=S\s+/)	# Begins with CheckDescription =S
	{
		$printdesc = $';
		chop ($printdesc);		# Remove the ; at the end of the line 
		next;
	}

	if	(/^Priority\s+=L\s+(\w+);/)	# Begins with Priority = L and ends in ;
	{
		$priority = $1;			# 1 = High 2 = Medium 3 = Low

		if	($priority eq 1)
		{	
			$printpriority = "High";
		}
		elsif	($priority eq 2)
		{
			$printpriority = "Medium";
		}
		elsif	($priority eq 3)
		{
			$printpriority = "Low";
		}
		else
		{
			$printpriority = "???";
		}
		next;
	}

	if	(/^Choice\s+=S\s+(\w+);*/)	# Search for  	=S
	{
		if 	($dispflag eq 1)		# The last RESPONSE was a DISPLAY
		{
			$printdisp=$1;
			$dispflag=0;
		}
		elsif ($logdbflag eq 1)		# The last RESPONSE was a LOGDB
		{
			$printlogdb = $1;
			$logdbflag=0;
		}
	}
		
	if	(/RSKILL/)				# Contains the line RSKILL
	{	
		$z=(<>);				# Read in next Line	
		if	($z =~ /^Enabled\s+=B\s+(\w+);*/) 	# Search for Enabled	=B
		{
			$printrskill = $1;
			if	($printrskill eq "1")
			{	
				$printrskill = "Yes";
			}
			elsif	($printrskill eq "0")
			{	
				$printrskill = "No";
			}
			next;
		}
	}
	
	if	(/DISPLAY/)				# Contains the line Display
	{	
		$dispflag = 1;			# Tells the next pass we are looking for a Display Setting
	}
	
	if	(/LOGDB/)				# Contains the word LOGDB
	{
		$logdbflag=1;			# Tell the next pass that we are looking for a Log Setting
	}
}	

# End

sub printline
{	
	print	"$printevent,$printdesc,$printenabled,$printpriority,$printdisp,$printlogdb,$printrskill\n";		# print line	$printevent="";
	$printevent="";
	$printdesc="";
	$printenabled="";
	$printpriority="";
	$printdisp="";
	$printlogdb="";
	$printrskill="";
	$enabledflag = 0;
}	


