#!/usr/bin/perl

# Please change the previous ligne with you perl location

######################################################################
# 
#      ManyPage : Moteur de Publication de site Web
#      ManyPage : Web site Management tools
#
#
# Auteur/Author       		: Pascal Vuylsteker 
# Collaborateur/Contributor	: Pierre Cordani
#
# Documentation : http://www.vrarchitect.net/ManyPage
# Question      : manypage@vuylsteker.net
#
# Creation      : 1/1/00
# Modification  : 1/12/00
# Version       : 0.9
# SoftVersion   : Perl 5
######################################################################
# 
# check_obj.pl  : litle independant script that check whether your
# .obj files are well constructed
#
# Use :   check_obj.pl .obj
# 
######################################################################
#
#  Copyright (C) 2000 Pascal Vuylsteker - Pierre Cordani
#
# 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 2
# of the License, or (at your option) any later version.
#
# Ce programme est un logiciel libre ; vous pouvez le redistribuer et/ou le modifier conformŽment
# aux dispositions de la Licence Publique GŽnŽrale GNU, telle que publiŽe par la Free Software
# Foundation ; version 2 de la licence, ou encore (ˆ votre choix) toute version ultŽrieure.
#
###################



#$FlagShowComment = 'ON';

######################
# TEST DES ARGUMENTS #
######################

	$argu=scalar(@ARGV);
	if ($argu != 1)
	{&modeEmploi;}

	if (@ARGV[0] =~ m/[h\-]/)
	{
		&modeEmploi;
	}
	else {
		$file = @ARGV[0];
	}


print "\nBegining of the check of file $file ... \n";


open (OBJ, $file) or die "\nError when trying to open the file $file :\n\t$!";
		

while (<OBJ>)	{
	$TOTAL_Ligne ++;
	
	next if (/^\s*$/);
	
	next if (/PM\.LANG/);
	
	if ($FlagDebComment eq 'ON')	{
		if ( $_ =~ s/(.*)-->//) {
			print "\nCOMMENT $TOTAL_Ligne\t: $1" if ($FlagShowComment);
			$FlagDebComment = '';
		}	else	{
			print "\nCOMMENT $TOTAL_Ligne\t: $_" if ($FlagShowComment);
			next;
		}
	}
		
	
	if ($_ =~ s/<!--(.*)-->//g)	{
		print "\nCOMMENT $TOTAL_Ligne\t: $1" if ($FlagShowComment);
		$comment++;
	}
	
	if ($_ =~ s/<!--(.*)//)	{
		print "\nCOMMENT $TOTAL_Ligne\t: $1" if ($FlagShowComment);
		$comment++;
		$FlagDebComment = 'ON';
		next;
	}

	next if (/^\s*$/);

#	if (($ObjectName) and not (/<\/(?:MP|PM)\.$ObjectName>/))	{
	if (($ObjectName) and not (/<\/(?:MP|PM)\.([\w.\-]+)>/))	{
		$ObjectValue = $_;
		$ObjectValue =~ s/^\s*//;
		$ObjectValue =~ s/\s*$//;
		unless ($AllObjects{$ObjectName}) {
			$AllObjects{$ObjectName} = $ObjectValue;
			print $ObjectValue;
		}
		next;
	}

	if (/^\s*<(?:MP|PM)\.([\w.\-]+)(\s+[^>]*)?>(.*)<\/(?:MP|PM)\.\1(\s+[^>]*)?>\s*$/)	{
		$ObjectName = $1;
		$ObjectValue = $2;
		$ObjectValue =~ s/^\s*//;
		$ObjectValue =~ s/\s*$//;
		if ($AllObjects{$ObjectName}) {
			print "\n\nATTENTION : This object has been defined at least twice ! :\nLigne $TOTAL_Ligne.\tObject : $ObjectName \t:Value : $ObjectValue\n";
		}	else	{
			$AllObjetcs{$ObjectName} = $ObjectValue;
			$AllObjetcsLigne{$ObjectName} = $TOTAL_Ligne;
			print "\nNew Object $TOTAL_Ligne\t: $ObjectName \tValue : $ObjectValue";
			$NumberObject++
		}
		$ObjectName = '';
		
	}	elsif (/^\s*<(?:MP|PM)\.([\w.\-]+)(\s+[^>]*)?>(.*)<\/(?:MP|PM)\.\1(\s+[^>]*)?>/)	{
		$ObjectName = $1;
		$ObjectValue = $2;
		$ObjectValue =~ s/^\s*//;
		$ObjectValue =~ s/\s*$//;
		if ($AllObjects{$ObjectName}) {
			print "\n\nATTENTION : This object has been defined at least twice ! :";
			print "\nLigne $TOTAL_Ligne.\tObject : $ObjectName \t:Value : $ObjectValue\n";
			print "\nFirst time definition : Ligne $AllObjetcsLigne{$ObjectName} \n";
		}	else	{
			$AllObjetcs{$ObjectName} = $ObjectValue;
			$AllObjetcsLigne{$ObjectName} = $TOTAL_Ligne;
			print "\nNew Object $TOTAL_Ligne\t: $ObjectName \tValue : $ObjectValue";
			$NumberObject++
		}
		print "\nATTENTION : No more than one object per ligne please. The end of the ligne is not toke into account\nLigne $TOTAL_Ligne. \tEnd of the ligne : $'\n";
		$ObjectName = '';
		
	}	elsif (/^\s*(.*)<\/(?:MP|PM)\.([\w.\-]+)>\s*$/)	{
		$ObjectLastName = $2;
		$ObjectValue = $1;
		$ObjectValue =~ s/^\s*//;
		$ObjectValue =~ s/\s*$//;
		if ($ObjectName eq $ObjectLastName)	{
			unless ($AllObjects{$ObjectName}) {
				$AllObjects{$ObjectName} = $ObjectValue;
				print $ObjectValue;
			}
			$ObjectName = '';
			$ObjectBeginLign = '';
		}	else	{
			print "\nATTENTION : Object Names DO NOT MATCH : ";
			print "\nLigne $ObjectBeginLign.\tObject : $ObjectName ::\tLigne $TOTAL_Ligne.\tObject : $ObjectLastName"; 
			print "\nA object is closed and none has been opened" unless ($ObjectBeginLign);
			print "\nThe checking has been stopped at Ligne $TOTAL_Ligne\n\n";
			exit();
		}		
	}	elsif (/^\s*<(?:MP|PM)\.([\w.\-]+)(\s+[^>]*)?>(.*)/)	{
		$ObjectName = $1;
		$ObjectValue = $2;
		$ObjectValue =~ s/^\s*//;
		$ObjectValue =~ s/\s*$//;
		if ($AllObjects{$ObjectName}) {
			print "\n\nATTENTION : This object has been defined at least twice ! :\nLigne $TOTAL_Ligne.\tObject : $ObjectName \t:Value : $ObjectValue\n";
		}	else	{
			$AllObjects{$ObjectName} = $ObjectValue;
			print "\nNew Object $TOTAL_Ligne\t: $ObjectName \tValue : $ObjectValue";
			$NumberObject++
		}
		
		$ObjectBeginLign = $TOTAL_Ligne
		
	}	else	{
		if ($ObjectName)	{
			$ObjectValue = $_;
			$ObjectValue =~ s/^\s*//;
			$ObjectValue =~ s/\s*$//;
			unless ($AllObjects{$ObjectName}) {
				$AllObjects{$ObjectName} = $ObjectValue;
				print $ObjectValue;
			}
		}	else	{
			print "\nATTENTION : Not commented ligne Outside Object definition : ";
			print "\nLigne $TOTAL_Ligne.\tContent : $ObjectValue"; 
		}
	}
}

print "\n\nCheck finished";

	
print "\n\nATTENTION : Object $ObjectName has been opened at line $ObjectBeginLign and has not been closed yet !"
	if ($ObjectName);

print "\n\n******************************\n\nResult of the chek : \n";
print "\nNumber of Objects : $NumberObject";
print "\nNumber of Ligne   : $TOTAL_Ligne";





print "\n";

######################################################################

sub modeEmploi {
	print "\nScript de test des fichiers .obj de ManyPage";
	print "\n\nUtilisation :\n\tcheck_obj.pl .obj";
	print "\n\n------\nScript to test ManyPage's .obj files";
	print "\n\Use :\n\tcheck_obj.pl .obj";
	print "\n\nCopyright (C) 2000 Pascal Vuylsteker - Pierre Cordani\nLicence GNU GPL";
	print "\n\n";
	exit(1);
}
