#!/usr/bin/perl -w
use strict;

# Copyright (C) 2011 Alois Schloegl, IST Austria, <alois.schloegl@ist.ac.at>

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.



my $n = 0;
my $flag_valid_cur_adr = 0; 
my $NumberOfRecords = 0; 

#############################################################################
#
#    parsing of OpenChange output 
#
#############################################################################
### record separater is the non-printable ASCII(30)=0x1e character 
#$/ = "\n\x1e";

## Check first line e.g. "MAILBOX (1579 messages)\n"
chomp($a = <>);
if ($a =~ /^MAILBOX\s*\((\d*)\s*messages\)\s*$/) {
        $NumberOfRecords = $1;
} else {
        print "Error OC2DB [221]: first line <$_> invalid\n";
        return; 
}        

while (<>) { # assigns each line in turn to $_
        chomp;

	### last line -> report last data set 
	if  (/^$/ || eof()) {
	        ### empty line indicates "end-of-record": process record 
	        #   process current data record
	};

	last if eof(); 

	### new entry 	
	if (/^\|==\s*([\x00-\xff]*)\s*==\|\s*:\s*([0-9A-F]*)\/([0-9A-F]*)\s*$/) {	
	        # note $1 may contain any ASCII character including <NEWLINE> and nonprintables
	        #   (.*) does not work because it does not include <NEWLINE> 
		# define next entry, clear $cur_adr	
		
		if ($flag_valid_cur_adr>0) { print "Error OC2DB [276] in line $.: start of record pattern <$_> ist not first line \n" };

		#$ocxid = hex('0x'.$3);
		$n++;
	}

        ### decode fields  
	elsif (/^([\w\s\-\/]*):\s*([\x00-\xff]*)$/) {
	}	
}

if ($n != $NumberOfRecords) {
        ### ocx.txt does not contain all records, be careful and do not delete anything
        print "Warning OC2DB: number of records do not match ( $n != $NumberOfRecords ) \n" ;
} 



