# 現在のディレクトリにあるC#コードを読み込み、使用されていないMessage.IDを
# result.txtに出力する。サブディレクトリも検索。
$listname = "_";
while( -e $listname ){
	$listname = $listname . "_";
}

system "dir /s /b>" . $listname;

%hash;
%defined;

open( FILE, $listname );
while( $line = <FILE> ){
	chomp $line;
	# MessageIDの定義から，定義済みのenumを列挙
	if( index( $line, "MessageID.cs" ) >= 0 ){
		open( CS, $line );
		$mode = 0;
		while( $cs_line = <CS> ){
			if( $mode == 1 && index( $cs_line, "}" ) >= 0 ){
				$mode = 2;
				next;
			}
			if( $mode == 1 ){
				$line2 = "" . $cs_line;
				chomp $line2;
				$line2 =~ s/ //g;
				$line2 =~ s/,//g;
				$line3 = $line2;
				$defined{$line3} = 0;
			}
			if( $mode == 0 && index( $cs_line, "enum MessageID" ) >= 0 ){
				$mode = 1;
			}
		}
		close( CS );
		next;
	}else{
		open( CS, $line );
		while( $cs_line = <CS> ){
			$index = index( $cs_line, "MessageID." );
			while( $index >= 0 ){
				$spl = substr( $cs_line, $index + 10 );
				$index2 = index( $spl, ")" );
				$cs_line = substr( $spl, $index2 );
				$spl2 = substr( $spl, 0, $index2 );
				$spl2 =~ s/ //g;
				$hash{$spl2} = $hash{$spl2} + 1;
				$index = index( $cs_line, "MessageID." );
			}
		}
		close( CS );
	}
}
close( FILE );

@keys = keys( %hash );
foreach $entry( @keys ){
	$defined{$entry} = 1;
}
open( RESULT, ">result.txt" );
@keys2 = keys( %defined );
foreach $entry( @keys2 ){
	if( $defined{$entry} == 0 ){
		print RESULT $entry . "," . $defined{$entry} . "\n";
	}
}
close( RESULT );

system "del " . $listname;
