#!/usr/bin/perl -w
#
# Copyright (c) Members of the EGEE Collaboration. 2004-2010.
# See http://www.eu-egee.org/partners/ for details on the copyright holders.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

my $lines = $ENV{AT3_LINES};
my ($prefix, $perlinstalldir);

BEGIN {
	$prefix = '/usr';
	$perlinstalldir = '/usr/share/perl5/vendor_perl';
}

use lib "$perlinstalldir";

use gLite::LB::MultiStruct;
require "$prefix/share/glite-lb/at3/types.T";

my $eventsn = "$prefix/share/glite-lb/at3/events.T";
my $statusn = "$prefix/share/glite-lb/at3/status.T";

my $indent = '';

$event = new gLite::LB::MultiStruct;
$status = new gLite::LB::MultiStruct;

sub gen {
	local $_ = shift;

	s/^\n!//;
	s/\n!/\n/g;
	print $_;
}


open EVENTS,$eventsn or die "$eventsn: $!\n";
$event->load(\*EVENTS);
close EVENTS;

open STATUS,$statusn or die "$statusn: $!\n";
$status->load(\*STATUS);
close STATUS;

my $code;
my $startcode;
while (<>) {
	chomp;
	if (/^\@\@\@LANG: (\S+)$/) {
		$gLite::LB::StructField::lang = $1;
		next;
	}

	if ($code) {
		if (/^\@\@\@}$/) {
			$code .= "1;\n";
			print "#line $startcode \"$ARGV\"\n/* begin */\n" if $lines;
			eval $code or die "eval: $@ at $ARGV:$.\n";
			my $nxtline = $.+1;
			print "/* end */\n#line $nxtline \"$ARGV\"\n" if $lines;
			undef $code;
		}
		else { $code .= $_."\n"; }
	}
	else {
		if (/^\@\@\@{$/) {
			$startcode = $.;
			$code = "\n";
		}
		elsif (/^\@\@\@AUTO$/) {
			print qq{
  !! Automatically generated file
  !! Do not edit, your changes will be discarded upon build
  !! Change the corresponding template file $ARGV

};
			print "#line $. \"$ARGV\"\n" if $lines;
		}
		else {
			print "$_\n";
		}
	}
}

# print $event_common{prog}->copy('bla','hu');
