#!/usr/bin/perl -w

#TODO: Individual Testing

###########################################################################
#
# test.pl -- for testing is built collection is consistent with model collection
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 New Zealand Digital Library Project
#
# 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.
#
# 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

package diffcol_mk2;

BEGIN {
	die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
	die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
	unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
	unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
}

use parsargv;
use util;
use FileUtils;
use logdiff;
use cfgdiff;
use gdbdiff;
use diffutil;
use Text::Diff;

#--Global Variables Declaration-----------
$gv_strModelColRoot = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"/model-collect");
$gv_strTestColRoot = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"/collect");

$gv_blnErrorStop = "false";
$gv_blnErrorShow = "false";
$gv_intVerbosity = 0;
$gv_strMode = "Full";
$strOutputFormat = "xml" unless defined $strOutputFormat; # global var with default

%gv_IndivList = ("archives" => 0,
		"etc" => 0,
		"images" => 0,
		"building" => 0,
		"import" => 0,
		"index" => 0,
		"log" => 0,
		"metadata" => 0,
		"perllib" => 0,
		"temp" => 0);
#----##

#--System Setup---------------------------
sub SetMode
{
	my ($strModeList) = @_;
	$strModeList =~ s/\|/ /g;
	my @Modes = split(" ",$strModeList);

	my $blnIndividual = "true";
	my $blnInitial = "false";
	my $blnFull = "false";


	foreach $strEachMode (@Modes)
	{
		if($strEachMode eq "all")
		{
			$blnFull = "true";
			$blnIndividual = "false";
		}
		elsif($strEachMode eq "init")
		{
			$blnInitial = "true";
			$blnIndividual = "false";
		}
		else
		{
			if(defined $gv_IndivList{$strEachMode})
			{
				$gv_IndivList{$strEachMode} = 1;
			}
			else
			{
				die Help("Error: used undefined mode");
			}
		}
	}

	if($blnFull eq "true") {return "Full";}
	elsif($blnInitial eq "true") {return "Initial";}
	elsif($blnIndividual eq "true") {return "Individual";}
	else {die "Error occured in function SetMode!!\n";}
}
#----##

#--System Process-------------------------
sub IndivTest
{
	my ($strModelCol,$strTestCol) = @_;
	my @Errors = ();
	my $intNumberOfErrors = 0;
	foreach $strEachFolder (keys %gv_IndivList)
	{
		if($gv_IndivList{$strEachFolder} == 1)
		{
			
			VobPrint("Start Comparing \"$strEachFolder\"\n",0);
			my $strModelFolder = &FileUtils::filenameConcatenate($strModelCol,$strEachFolder);
			my $strTestFolder = &FileUtils::filenameConcatenate($strTestCol,$strEachFolder);
			$intNumberOfErrors = scalar(@Errors);
			push(@Errors,TestEach($strModelFolder,$strTestFolder,0));
			$intNumberOfErrors = scalar(@Errors) - $intNumberOfErrors;
			VobPrint("End Comparing \"$strEachFolder\"\n",0);
			VobPrint("Difference Found: $intNumberOfErrors\n",0);
			VobPrint ("\n",0);
		}
	}
	return @Errors;
}

sub InitTest
{

	my ($strModelCol,$strTestCol,$strColName) = @_;
	my $intLevel = 1;
	my @Errors;
	
	# Testing Log files
#	my $strModelLog = &FileUtils::filenameConcatenate($strModelCol,"log");
#	my $strTestLog = &FileUtils::filenameConcatenate($strTestCol,"log");
#	
#	if(-e $strModelLog && -e $strTestLog)
#	{
#	my $strLogError = logdiff::test_log($strModelLog,$strTestLog);
#	if($strLogError ne "")
#	{
#		AlignPrint("Log Folder Comparison Result","Failed",$intLevel);
#		VobPrint ("$strLogError\n",$intLevel);
#
#		$strLogError = "$strLogError"; 
#		$strLogError = "Difference Found at Log Folder Testing\n".$strLogError."\n";
#		push(@Errors,$strLogError);
#	}
#	else
#	{
#		AlignPrint("Log Folder Comparison Result","Succeed",$intLevel);
#	}
#	}
#	else 
#	{ 
#	my $strErrorColName;
#	my $strLogError;
#	
#	if(!(-e $strModelLog)){ $strErrorColName = $strErrorColName."(Model Collection)";}
#	if(!(-e $strTestLog)){ $strErrorColName = $strErrorColName."(Test Collection)";}
#
#	AlignPrint("Log Folder Comparison Result","Failed",$intLevel);
#	$strLogError = "Difference Report: No Log Folder found in $strErrorColName"; 
#	VobPrint ("$strLogError\n",$intLevel);
#	$strLogError = "Difference Found at Log Folder Testing (Log folders are only created using GLI)\n".$strLogError."\n";
#
#	push(@Errors,$strLogError);
#	}
#	VobPrint ("\n",$intLevel);
	
	# Testing the build.cfg
	my $strModelBcfg =  &FileUtils::filenameConcatenate($strModelCol,"index","build.cfg");
	my $strTestBcfg =  &FileUtils::filenameConcatenate($strTestCol,"index","build.cfg");

	if(-e $strModelBcfg && -e $strTestBcfg)
	{
		my $strBcfgError = cfgdiff::test_cfg($strModelBcfg,$strTestBcfg,"build.cfg");
		if($strBcfgError ne "")
		{
			if( $strOutputFormat eq "xml" ) {
				print "<build-cfg succeeded=\"no\">\n<message>";
			} else {
				AlignPrint("Config File(build.cfg) Comparison Result","Failed",$intLevel);
			}

			VobPrint ("$strBcfgError",$intLevel);
			if( $strOutputFormat eq "xml" ) {
				print "</message></build-cfg>";
			}

			$strBcfgError = "$strBcfgError"; 
			$strBcfgError = "Difference Found at Config File(build.cfg) Comparison\n".$strBcfgError."\n";
			push(@Errors,$strBcfgError);
		}
		else
		{
			if( $strOutputFormat eq "xml" ) {
				print "<build-cfg succeeded=\"yes\"/>";
			} else {
				AlignPrint("Config File(build.cfg) Comparison Result","Succeed",$intLevel);
			}
		}
	}
	else 
	{
		my $strErrorColName;
		my $strBcfgError;
		
		if(!(-e $strModelBcfg)){ $strErrorColName = $strErrorColName."(Model Collection)";}
		if(!(-e $strTestBcfg)){ $strErrorColName = $strErrorColName."(Test Collection)";}

		AlignPrint("Config File(build.cfg) Comparison Result","Failed",$intLevel);
		$strBcfgError = "Difference Report: No Config files found in $strErrorColName";
		VobPrint ("$strBcfgError\n",$intLevel);
		$strBcfgError = "Difference Found at Config File(build.cfg) Comparison\n".$strBcfgError."\n";

		push(@Errors,$strBcfgError);
	}
	VobPrint ("\n",$intLevel);

	# Testing the collect.cfg

	my $strModelCcfg =  &FileUtils::filenameConcatenate($strModelCol,"etc","collect.cfg");
	my $strTestCcfg =  &FileUtils::filenameConcatenate($strTestCol,"etc","collect.cfg");
	
	if(-e $strModelCcfg && -e $strTestCcfg)
	{
		my $strCcfgError = cfgdiff::test_cfg($strModelCcfg,$strTestCcfg,"collect.cfg");
		if($strCcfgError ne "")
		{
			if( $strOutputFormat eq "xml" ) {
				print "<collect-cfg succeeded=\"no\"><message>";
			} else {
				AlignPrint("Config File(collect.cfg) Comparison Result","Failed",$intLevel);
			}

			VobPrint ("$strCcfgError",$intLevel);

			if( $strOutputFormat eq "xml" ) {
				print "</message></collect-cfg>";
			}

			$strCcfgError = "$strCcfgError"; 
			$strCcfgError = "Difference Found at Config File(collect.cfg) Comparison\n".$strCcfgError."\n";
			push(@Errors,$strCcfgError);
		}
		else
		{
			if( $strOutputFormat eq "xml" ) {
				print "<collect-cfg succeeded=\"yes\"/>";
			} else {
				AlignPrint("Config File(collect.cfg) Comparison Result","Succeed",$intLevel);
			}
		}
	}
	else 
	{
		my $strErrorColName;
		my $strCcfgError;

		if(!(-e $strModelCcfg)){ $strErrorColName = $strErrorColName."(Model Collection)";}
		if(!(-e $strTestCcfg)){ $strErrorColName = $strErrorColName."(Test Collection)";}

		AlignPrint("Config File(collect.cfg) Comparison Result","Failed",$intLevel);
		$strCcfgError = "Difference Report: No Config files found in $strErrorColName";
		VobPrint ("$strCcfgError\n",$intLevel);
		$strCcfgError = "Difference Found at Config File(collect.cfg) Comparison\n".$strCcfgError."\n";
	
		push(@Errors,$strCcfgError);
	}

	VobPrint ("\n",$intLevel);
	
	# Testing database

	my $strModelGdb =  &FileUtils::filenameConcatenate($strModelCol,"index","text","$strColName.ldb");
	my $strTestGdb =  &FileUtils::filenameConcatenate($strTestCol,"index","text","$strColName.ldb");
	
	if(-e $strModelGdb && -e $strTestGdb)
	{
		my $strGdbError = gdbdiff::test_gdb($strModelGdb, $strTestGdb);
		if($strGdbError ne "")
		{
			if( $strOutputFormat eq "xml" ) {
				print "<database succeeded=\"no\"><message>";
			} else {
				AlignPrint("Database Comparsion Result","Failed",$intLevel);
			}
			VobPrint ("$strGdbError\n",$intLevel);
	
			if( $strOutputFormat eq "xml" ) {
				print "</message></database>";
			}

			$strGdbError = "$strGdbError"; 
			$strGdbError = "Difference Found at Database Comparsion\n".$strGdbError."\n";
			push(@Errors,$strGdbError);
		}
		else
		{
			if( $strOutputFormat eq "xml" ) {
				print "<database succeeded=\"yes\"/>";
			} else {
				AlignPrint("Database Comparsion Result","Succeed",$intLevel);
			}
		}
	}
	else 
	{	
		my $strErrorColName;
		my $strGdbError;
		
		if(!(-e $strModelGdb)){ $strErrorColName = $strErrorColName."(Model Collection)";}
		if(!(-e $strTestGdb)){ $strErrorColName = $strErrorColName."(Test Collection)";}
	
		AlignPrint("Database Comparsion Result","Failed",$intLevel);
		
		$strGdbError = "Difference Report: No Database files found in $strErrorColName";
		VobPrint ("$strGdbError\n",$intLevel);
	
		$strGdbError = "Difference Found at Database Comparison\n".$strGdbError."\n";
	
		push(@Errors,$strGdbError);
	}

	VobPrint ("\n",$intLevel);

	return @Errors;
}

sub FullTest
{
	my ($strModelCol,$strTestCol,$strColName) = @_;
	my @Errors = ();
	my $intLevel = 0;
	my $intNumberDiffs = 0;
	
	# <Initial Test>
	if( $strOutputFormat eq "xml" ) {
		#print "<initial-test>";
	} else {
		VobPrint("Initial Testing Start\n",$intLevel);
	}

	@Errors = InitTest($strModelCol,$strTestCol,$strColName);
	$intNumberDiffs = scalar(@Errors);

	if( $strOutputFormat eq "xml" ) {
		#print "</initial-test>";
	} else {
		VobPrint("Initial Testing End\n",$intLevel);
		VobPrint("Difference Found in Initial Testing: $intNumberDiffs\n",$intLevel);
		VobPrint("\n",$intLevel);
	}
	# </Initial Test>

	# <Detailed Test>
	if( $strOutputFormat eq "xml" ) {
		#print "<detailed-test>";
	} else {
		VobPrint("Detail Testing Start\n",$intLevel);
	}
	push(@Errors,TestEach($strModelCol,$strTestCol,$intLevel));
	$intNumberDiffs = scalar(@Errors) - $intNumberDiffs;

	if( $strOutputFormat eq "xml" ) {
		#print "</detailed-test>";
	} else {
		VobPrint("Detail Testing End\n",$intLevel);
		VobPrint("Difference Found in Detail Testing: $intNumberDiffs\n",$intLevel);
	}
	# </Detailed Test>

	return @Errors;
}
#----##


#--Other System Utilities
sub PrintUsage
{
	my ($strProgName) = @_;
	if ( $strOutputFormat eq "xml" ) {
		print "<error>usage incorrect</error>\n";
	} else {
		print STDERR "Usage: $strProgName test-col [more-col] [-verbosity d] [-mode modes] [-eshow] [-estop]\n";
	}
	Help("Error: used incorrect parameters");
}

sub Help
{
	my ($strError) = @_;
	my $aryptHelps =
	[ { 'name' => "verbosity",
		'type' => "scale",
		'argu' => "a integer" ,
		'descrip' => "this parameter setup the verbosity of the testing result"},
	  { 'name' => "mode",
		'type' => "option",
		'argu' => "mode type \"[all|init|archives|building|etc|images|import|index|perllib|tmp]\" default to \"all\"" ,
		'descrip' => "setup testing mode: all-full testing, init-initial testing (include configuration file test,database testing and log testing), others-for individual folder testing"},
	  { 'name' => "estop",
		'type' => "flag",
		'argu' => "NULL" ,
		'descrip' => "Set then system will stop once it meet an error"},
	  { 'name' => "eshow",
		'type' => "flag",
		'argu' => "NULL" ,
		'descrip' => "Set then system will show the error summary"}
	  ];
	

	if ( $strOutputFormat ne "xml" ) {
		print "$strError\n";

		foreach my $hashOneArg (@{$aryptHelps})
		{
			print "\n----------------------------\n";
			print "Parameters: -".$hashOneArg->{"name"}."\n";
			print "Type: ".$hashOneArg->{"type"}."\n";
			print "Supply Argument: ".$hashOneArg->{"argu"}."\n";
			print "Description: ".$hashOneArg->{"descrip"}."\n";
			print "----------------------------\n";
		}
	}
}

sub OutputStart
{
	my ($strColName) = @_;
	my $intPadding = 17 - length($strColName);

	if ( $strOutputFormat eq "xml" ) {
		print "<diffcol>\n";
	} else {
		print "+---------------------------------------------------------+\n";
		print "|                                                         |\n";
		print "|              Start Testing Collection: $strColName"," " x $intPadding,"|\n";
		print "|                                                         |\n";
		print "+---------------------------------------------------------+\n\n";
	}
}

sub OutputEnd
{
	my ($strColName,$aryptErrors) = @_;
	my $intPadding = 12 - length($strColName);
	if ( $strOutputFormat eq "xml" ) {
		print "</diffcol>\n";
	} else {
		print "\n";
		print "+---------------------------------------------------------+\n";
		print "|                                                         |\n";
		print "|	        Result of Collection Testing: $strColName"," " x $intPadding,"|\n";
		print "|                                                         |\n";
		print "+---------------------------------------------------------+\n\n";
	}

	my $intTotalErrors = scalar(@{$aryptErrors});
	if ( $strOutputFormat ne "xml" ) {
		print "Checking completed, there is $intTotalErrors error(s) found.\n";
	}

	if($gv_blnErrorShow ne "off")
	{
		foreach my $strEachError (@{$aryptErrors})
		{
			if ( $strOutputFormat eq "xml" ) {
				print "<error>";
				print $strEachError;
				print "</error>\n";
			} else {
				print "+---------------------------------------------------------+\n";
				print "|                        Error                            |\n";
				print "+---------------------------------------------------------+\n\n";
				print "$strEachError\n\n";
			}
		}
	}
	else
	{
		if ( $strOutputFormat ne "xml" ) {
			print "Use -eshow to show the error detail\n\n";
		}
	}
}

sub AlignPrint
{
	my ($strMainString,$strStatus,$intLevel) = @_;
	my $intDot = 100 - length($strMainString) - length($strStatus);
	VobPrint ($strMainString."."x$intDot."$strStatus\n",$intLevel);
}

sub TestEach
{
	my ($strModel,$strTest,$intLevel) = @_;
	my @Errors = ();

	$intLevel++;
	if (-d $strModel && -d $strTest)
	{	
		my @aryInModel = &diffutil::files_in_dir($strModel);
		my @aryInTest = &diffutil::files_in_dir($strTest);
		my @aryTwoPointers = FolderTesting(\@aryInModel,\@aryInTest,$strModel,$strTest,$intLevel);
		my @aryCorrectFiles = @{$aryTwoPointers[1]};
		@Errors = @{$aryTwoPointers[0]};
		
		if(scalar(@Errors) == 0)
		{
			foreach my $strEachFile (@aryInModel)
			{
				my $strNewModel = &FileUtils::filenameConcatenate($strModel,$strEachFile);
				my $strNewTest = &FileUtils::filenameConcatenate($strTest,$strEachFile);
				if(!($strEachFile eq "log" || $strEachFile =~ m/\.cfg$/g || $strEachFile =~ m/\.(l|b)db$/g))
				{
					push(@Errors,TestEach($strNewModel,$strNewTest,$intLevel));
				}
				else
				{
					if ( $strOutputFormat eq "xml" ) {
						print "<file-comparision location=\"$strEachFile\" blocked=\"yes\" succeeded=\"yes\"/>";
					} else {
						VobPrint ("Blocked File Report: Test \"$strEachFile\" by using -mode \"init\"\n",$intLevel);
					}
				}
			} 
		}
		else
		{
			foreach my $strEachFile (@aryCorrectFiles)
			{
				my $strNewModel = &FileUtils::filenameConcatenate($strModel,$strEachFile);
				my $strNewTest = &FileUtils::filenameConcatenate($strTest,$strEachFile);
				if(!($strEachFile eq "log" || $strEachFile =~ m/\.cfg$/g || $strEachFile =~ m/\.(l|b)db$/g))
				{
					push(@Errors,TestEach($strNewModel,$strNewTest,$intLevel));
				}
			}
		}
		if($intLevel == $gv_intVerbosity)
		{
			if(scalar(@Errors) == 0){ AlignPrint("Contents Comparsion","Succeed",$intLevel);}
			else {  AlignPrint("Contents Comparsion","Failed",$intLevel);}
		}
	}
	else
	{
		my $strResult = diff $strModel, $strTest, { STYLE => "OldStyle" };
		$strResult = &diffutil::GenerateOutput($strResult,"^<Metadata name=\"lastmodified\">.*</Metadata>\$");
		#$strResult = GeneralOutput($strResult);
		if ( $strOutputFormat eq "xml" ) {
			#
		} else {
			VobPrint ("Comparing Files:\n\"$strModel\"\n\"$strTest\"\n",$intLevel);
		}
		if ($strResult eq "")
		{
			if ( $strOutputFormat eq "xml" ) {
				print "<file-comparison location=\"$strModel\" succeeded=\"yes\"/>\n";
			} else {
				AlignPrint("Comparing File","Succeed",$intLevel);
			}
		}
		else
		{
			my $strOutput = "Difference Report:\n$strResult\n";
			if ( $strOutputFormat eq "xml" ) {
				print "<file-comparison location=\"$strModel\" succeeded=\"no\"><message>";
			} else {
				AlignPrint("Comparing File","Failed",$intLevel);
			}

			$result=`file -b $strModel`;
			if ( "$result" =~  "data" ) {
			         VobPrint( "These binary files differ", $intLevel );
			} else {
			         VobPrint ( "$strOutput" , $intLevel);
			}


			if ( $strOutputFormat eq "xml" ) {
				print "</message></file-comparison>";
			}

			if($gv_blnErrorStop ne "off") { exit; }	
			push(@Errors,"File content comparison failed($strModel):\n$strOutput");
		}
	}

	return @Errors;
}


sub FolderTesting
{
	my ($aryptModel,$aryptTest,$strModelFolder,$strTestFolder,$intLevel) = @_;
	my %hashCount = ();
	my @Errors = ();
	my @CorrectFiles = ();
	my @TwoPointers = (\@Errors,\@CorrectFiles);

	if ( $strOutputFormat eq "xml" ) {
		#print "<folder-comparison location=\"$strModelFolder\">\n";		
	} else {
		VobPrint ("Comparing Folder contents at \"$strModelFolder\"\n",$intLevel);
	}

	foreach my $strEachItem (@$aryptModel) {$hashCount{$strEachItem} = 'M'}
	foreach my $strEachItem (@$aryptTest) 
	{
		if(defined $hashCount{$strEachItem} && $hashCount{$strEachItem} eq 'M') {$hashCount{$strEachItem} = 'B';}
		else {$hashCount{$strEachItem} = 'T';}
	}

	if( scalar(@$aryptModel)==scalar(@$aryptTest) && scalar(@$aryptModel)==scalar(keys %hashCount) )
	{
		if ( $strOutputFormat eq "xml" ) {
			print "<folder-comparison location=\"$strModelFolder\" succeeded=\"yes\"/>\n";
		} else {
			AlignPrint("Folder Comparsion","Succeed",$intLevel);
		}
		return @TwoPointers;
	}
	else
	{
		if ( $strOutputFormat eq "xml" ) {
			print "<folder-comparison location=\"$strModelFolder\" succeeded=\"no\"><message>\n";
		} else {
			AlignPrint("Folder Comparsion","Failed",$intLevel);
		}

		foreach my $strEachItem (keys %hashCount)
		{
			if($hashCount{$strEachItem} ne 'B')
			{ 
				my $strOutput = "";
				my $strReport = "";

				if($hashCount{$strEachItem} eq 'M')
				{
					$strOutput = "Difference Found at FolderTesting: \"$strEachItem\" is not found in the Test Collcetion";
					$strReport = "Difference Report: difference found at $strTestFolder";
				}
				elsif($hashCount{$strEachItem} eq 'T')
				{
					$strOutput = "Difference Found at FolderTesting: \"$strEachItem\" is not found in the Model Collcetion";
					$strReport = "Difference Report: difference found at $strModelFolder";
				}
				else {die "Error occours in diffcol_mk2::TestingFolder\n"}

				VobPrint ("$strOutput\n",$intLevel);
				$strOutput = $strOutput."\n\t".$strReport."\n";
				push(@Errors,$strOutput);
			}
			else {push(@CorrectFiles,$strEachItem);}
		}
		if( $strOutputFormat eq "xml" ) {
			print "</message></folder-comparison>";
		}

		return @TwoPointers;
	} 
}

sub VobPrint
{
	my ($strOutput, $intLevel) = @_;
	my $strTab = "";
	my $intTab = int($intLevel/2);
	if($intLevel <= $gv_intVerbosity)
	{
		if($intLevel >= 1) 
		{
			$strTab = "\t"x($intTab+1);
			$strOutput =~ s/\n$//;
			$strOutput =~ s/\n/\n$strTab/g;
			#$strTab =~ s/"\n"/"\n"."\t"x($intTab+1)/g;
		}

		if( $strOutputFormat eq "xml" ) {
			$strOutput =~ s/&/&amp;/g;
			$strOutput =~ s/</&lt;/g;
			$strOutput =~ s/>/&gt;/g;
		}

      if ( length( $strOutput ) > 1000 ) {
            $strOutput = substr( $strOutput, 0, 978) . "... (output truncated)";
      }


		print $strTab.$strOutput."\n";
	}
}
#----##


#--Main System----------------------------
#-----------------------------------------
# Name: main
# Perameters: arguments from command line
# Pre-condition: testing will start by calling this main function.
# Post-condition: output the test results for one or more collections.
#-----------------------------------------
sub main
{
	my ($intVerbosity,$strErrorStop,$strErrorShow,$strMode);
	my $strProgName = $0; 
	my $intArgc = scalar(@ARGV);
	
	#--System Arguments Setup
	if (!parsargv::parse(\@ARGV,
		'estop//off', \$strErrorStop, 
		'eshow//off', \$strErrorShow, 
		'verbosity/\d+/1', \$intVerbosity, 
		'mode/[\w\-]+/all', \$strMode,
		'output/[\w\-]+/text', \$strOutputFormat
		)) {
		PrintUsage($strProgName);
		die "\n";
	}

	if ($intArgc<1) {
		PrintUsage($strProgName);
		die "\n";
	}

	$gv_blnErrorStop = $strErrorStop;
	$gv_blnErrorShow = $strErrorShow;
	$gv_intVerbosity = $intVerbosity;
	$gv_strMode = SetMode($strMode);

	#----##

	#--Collection(s) Testing
	foreach $strColName (@ARGV)
	{
		my @ErrorsInEachCol;
		my $strModelCol = &FileUtils::filenameConcatenate($gv_strModelColRoot,$strColName);
		my $strTestCol = &FileUtils::filenameConcatenate($gv_strTestColRoot,$strColName);

		#--Output(Start)
		OutputStart($strColName);
		#----##
		
		if(-e $strModelCol && -e $strTestCol )
		{
			
			#--Individual Testing
			if ($gv_strMode eq "Individual")
			{
				@ErrorsInEachCol = IndivTest($strModelCol,$strTestCol);
			}
			#----##
			
			#--Initial Testing
			elsif	($gv_strMode eq "Initial")
			{
				@ErrorsInEachCol = InitTest($strModelCol,$strTestCol,$strColName);
			}
			#----##
			
			#--Full Testing
			elsif	($gv_strMode eq "Full")
			{
				@ErrorsInEachCol = FullTest($strModelCol,$strTestCol,$strColName);
			}
			#----##
			
			#--Error Checking
			else
			{
				if ( $strOutputFormat eq "xml" ) {
					die "<error>Error occoured in main function</error>\n";
				} else {
					die "Error occoured in main function.\n";
				}
			}
			#----##
			
		}
		else
		{
			if( $strOutputFormat eq "xml" ) {
				die "<error>Cannot find collection: $strColName</error>\n";
			} else {
				die "Error: cannot find collection: $strColName\n";
			}
		}
		#----##

		#--Output(Results and Errors)
		OutputEnd($strColName,\@ErrorsInEachCol);
		#----##

	}
}
#----##

&main();
