#! /usr/bin/perl

$cpath=`pwd`;
chomp($cpath);
if ($cpath =~ m|/opc(-[\d.]+)?$|) {
    $err_h="./include/opcerr.h";
    $error_h="./libopc/tools/opcerror.h";
} elsif ($cpath =~ m|libopc$|) {
    $err_h="../include/opcerr.h";
    $error_h="tools/opcerror.h";
} elsif ($cpath =~ m|tools$|) {
    $err_h="../../include/opcerr.h";
    $error_h="opcerror.h";
} else {
    $err_h="../include/opcerr.h";
    $error_h="../libopc/tools/opcerror.h";
}

open INPUT, $error_h;
@OLD = <INPUT>;
close INPUT;

open OUTPUT, ">$error_h";
foreach $line(@OLD) {
    if ($flag) {
	if ($line =~ m/}\s*;/) {
	    $flag=0;
	    print OUTPUT $line;
	    print OUTPUT "#define opc_errors_number $number\n";
	} 
    } else {
        if ($line =~ m/#define\s+opc_errors_number/) {
	    ;
	} else {
	    print OUTPUT $line;
	}
	if ($line =~ m/static\s+const\s+struct\s+OPCErrorT\s+opc_errors\[\]\s*=\s*{/) {
	    $flag=1;
	    
	    open INPUT, $err_h;
	    $number = 0;
	    while (<INPUT>) {
		if ($_ =~ m/#define\s*((ERR|WRN)_OPC_\w+)\s+([\dxABCDEFabcdef]+)/) {
		    if ($number++) {
			print OUTPUT ",\n";
		    } 
		    print OUTPUT "\t{$3, lgs(\"$1\")}";
		}
	    }
	    print OUTPUT "\n";
	    close INPUT;
	}
    }
}
close OUTPUT;
