#!/usr/bin/perl -w
$uu=scalar(@ARGV);
if ($uu ne 2)
{
    print "WW3_switch_process [switch] [eFile]\n";
    print "with [switch] being the WW3 switch file\n";
    print "and [eFile] being the WW3 source code\n";
    die;
}

$switchFile=$ARGV[0];
open(IN, $switchFile) or die "impossible to open ".$switchFile;
@B=<IN>;
close(IN);
#
@Lswitch=split(" ", $B[0]);
$nbSwitch=scalar(@Lswitch);

#

$eFile=$ARGV[1];
open(IN, $eFile) or die "impossible to open ".$eFile;
@C=<IN>;
close(IN);
#
$nbLine=scalar(@C);
for ($iLine=1; $iLine<=$nbLine; $iLine++)
{
#    print "-------------------------\n";
    $_=$C[$iLine-1];
    s/\n//;
    $eLine=$_;
    @U=split("", $eLine);
    $nbChar=scalar(@U);
    $HasDefinition=0;
    if (scalar(@U) eq 0 || $U[0] ne "!")
    {
#	print "Case 1\n";
	$RetLine=$eLine;
    }
    else
    {
	if (scalar(@U) <= 1 || $U[1] ne "/")
	{
#	    print "Case 2\n";
	    $RetLine=$eLine;
	}
	else
	{
	    $HasDefinition=1;
	    $pos=0;
	    $posFirst=-400;
	    while(1)
	    {
		$eChar=$U[$pos];
		if ($eChar eq " ")
		{
		    $posFirst=$pos;
		    last;
		}
		if ($pos eq $nbChar-2 || $pos eq $nbChar-1)
		{
		    last;
		}
		$pos++;
	    }
#	    print "BigSwitch=".$BigSwitch;
	    if ($posFirst eq -400)
	    {
		$posFirst=$nbChar;
	    }
	    else
	    {
		$eChar1=$U[$posFirst-1];
		$eChar2=$U[$posFirst-2];
		if ($eChar1 eq "!" && $eChar2 eq "!")
		{
		    $posFirst=$posFirst-2;
		}
		if ($eChar1 ne "!" && $eChar2 eq "!")
		{
		    $posFirst=$posFirst-2;
		}
		if ($eChar1 eq "!" && $eChar2 ne "!")
		{
		    $posFirst=$posFirst-1;
		}
	    }
	    $BigSwitch="";
	    for ($pos=0; $pos<$posFirst; $pos++)
	    {
		$eChar=$U[$pos];
		$BigSwitch=$BigSwitch.$eChar;
	    }
	    #
	    $WeMatchAll=1;
	    @V=split("/", $BigSwitch);
	    if (scalar(@V) eq 0)
	    {
#		print "Case 4\n";
		$RetLine=$eLine;
	    }
	    else
	    {
		@V2=split("", $BigSwitch);
		$BigSwitchB="";
		for ($iChar=1; $iChar<=scalar(@V2); $iChar++)
		{
		    $eChar=$V2[$iChar-1];
		    if ($eChar eq "!" || $eChar eq "/")
		    {
			$eChar=" ";
		    }
		    $BigSwitchB=$BigSwitchB.$eChar;
		}
		@VB=split(" ", $BigSwitchB);
		$nbBigSwitch=scalar(@VB);

#		print "|V|=".scalar(@V)."\n";
		for ($iV=1; $iV<=$nbBigSwitch; $iV++)
		{
		    $singSwitch=$VB[$iV-1];
		    $WeMatch=0;
		    for ($iSwitch=1; $iSwitch<=$nbSwitch; $iSwitch++)
		    {
			if ($WeMatch eq 0)
			{
			    $fSwitch=$Lswitch[$iSwitch-1];
			    if ($fSwitch eq $singSwitch)
			    {
				$WeMatch=1;
			    }
			}
		    }
		    if ($WeMatch eq 0)
		    {
			$WeMatchAll=0;
		    }
		}
		if ($WeMatchAll == 1 && $nbBigSwitch > 0)
		{
		    $RetLine="";
		    for ($ipos=$posFirst; $ipos<$nbChar; $ipos++)
		    {
			$RetLine=$RetLine.$U[$ipos];
		    }
#		    print "Case 5\n";
		}
		else
		{
#		    print "Case 6\n";
		    $RetLine=$eLine;
		}
	    }
	}
    }
    @W=split("", $RetLine);
    $siz=scalar(@W);
    $lastPos=0;
    for ($iChar=1; $iChar<=$siz; $iChar++)
    {
	$jChar=$siz + 1 - $iChar;
	$eChar=$W[$jChar-1];
	if ($lastPos eq 0)
	{
	    if ($eChar ne " ")
	    {
		$lastPos=$jChar;
	    }
	}
    }
    $DoSomething=1;
    if ($HasDefinition eq 1)
    {
	if ($WeMatchAll eq 0)
	{
	    $DoSomething=0;
	}
    }
    $strRet="";
    for ($iChar=1; $iChar<=$lastPos; $iChar++)
    {
	$strRet=$strRet.$W[$iChar-1];
    }
    if ($strRet eq "")
    {
	$strRet=" ";
    }
    if ($iLine > 1)
    {
	if ($strRet eq "!" && $strRetPrev eq "!")
	{
	    $DoSomething=0;
	}
    }
#    $eStr="!";
#    print "eStr=".$eStr."\n";
#    $eStr="!/";
#    print "eStr=".$eStr."\n";
#    print "lastPos=".$lastPos."\n";
#    print "strRet=".$strRet."\n";
#    print "iLine=".$iLine."\n";
#    if ($strRet eq "!/")
#    {
#	print "     EQUALITY  !!!! \n";
#    }
#    if ($strRet eq "!\/" && $strRetPrev eq "!\/")
#    {
#	print "     EQUALITY B \n";
#	$DoSomething=0;
#    }
#    print "iLine=".$iLine."\n";
#    print "eLine=".$eLine."\n";
#    print "strRet=".$strRet."\n";
#    print "strRetPrev=".$strRetPrev."\n";
#    print "DoSomething=".$DoSomething."\n";
#    print "WeMatchAll=".$WeMatchAll."\n";
    if ($DoSomething eq 1)
    {
	print $strRet."\n";
	$strRetPrev=$strRet;
    }
}
