#!/usr/bin/perl

($username) = @ARGV;

open (NEW,">users/$username/threshold.txt") || die ("Unable to open file for writing");
open (NEW1,">users/$username/antithreshold.txt") || die ("Unable to open file for writing");

$find="-";

system ("HCopy -T 1 -C users/$username/config2 -S users/$username/threstr.scp > blah.txt");


################################################## SAMPLE 1 -> T0001.WAV ########################################################

system ("HVite -H users/$username/hmm9/macros -H users/$username/hmm9/hmmdefs -S users/$username/thres1.scp -i thres1.mlf -w wdnet1 -p 0.0 -s 5.0 users/$username/dict users/$username/monophones1");
system ("HVite -H users/anti/hmm9/macros -H users/anti/hmm9/hmmdefs -S users/$username/thres1.scp -i antithres1.mlf -w wdnet1 -p 0.0 -s 5.0 users/anti/dict users/anti/monophones1");
open (OLD,"thres1.mlf") || die ("Unable to open thres.mlf for reading");
open (ANTI1,"antithres1.mlf") || die ("Unable to open antithres.mlf for reading");


$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
close(OLD);

$total1=0;
while () {
	if(/$find/)	{
		$total1=$total1+(substr($_,  index($_,"-") , 29) );
	}
}
$diff= $total-$total1;
print (NEW $diff);
print (NEW "\n");

# Wav 1 against Anti - User 1
system ("HVite -H users/tunney/hmm9/macros -H users/tunney/hmm9/hmmdefs -S users/$username/thres1.scp -i thres1.mlf -w wdnet1 -p 0.0 -s 5.0 users/tunney/dict users/tunney/monophones1");
open (OLD,"thres1.mlf") || die ("Unable to open thres.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 1 against Anti - User 2
system ("HVite -H users/steve/hmm9/macros -H users/steve/hmm9/hmmdefs -S users/$username/thres1.scp -i thres1.mlf -w wdnet1 -p 0.0 -s 5.0 users/steve/dict users/steve/monophones1");
open (OLD,"thres1.mlf") || die ("Unable to open thres.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 1 against Anti - User 3
system ("HVite -H users/ronan/hmm9/macros -H users/ronan/hmm9/hmmdefs -S users/$username/thres1.scp -i thres1.mlf -w wdnet1 -p 0.0 -s 5.0 users/ronan/dict users/ronan/monophones1");
open (OLD,"thres1.mlf") || die ("Unable to open thres.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 1 against Anti - User 4
system ("HVite -H users/tom/hmm9/macros -H users/tom/hmm9/hmmdefs -S users/$username/thres1.scp -i thres1.mlf -w wdnet1 -p 0.0 -s 5.0 users/tom/dict users/tom/monophones1");
open (OLD,"thres1.mlf") || die ("Unable to open thres.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 1 against Anti - User 5
system ("HVite -H users/jd/hmm9/macros -H users/jd/hmm9/hmmdefs -S users/$username/thres1.scp -i thres1.mlf -w wdnet1 -p 0.0 -s 5.0 users/jd/dict users/jd/monophones1");
open (OLD,"thres1.mlf") || die ("Unable to open thres.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

################################################## SAMPLE 2 -> T0002.WAV ########################################################

system ("HVite -H users/$username/hmm9/macros -H users/$username/hmm9/hmmdefs -S users/$username/thres2.scp -i thres2.mlf -w wdnet2 -p 0.0 -s 5.0 users/$username/dict users/$username/monophones1");
system ("HVite -H users/anti/hmm9/macros -H users/anti/hmm9/hmmdefs -S users/$username/thres2.scp -i antithres2.mlf -w wdnet2 -p 0.0 -s 5.0 users/anti/dict users/anti/monophones1");
open (OLD,"thres2.mlf") || die ("Unable to open thres2.mlf for reading");
open (ANTI1,"antithres2.mlf") || die ("Unable to open antithres2.mlf for reading");


$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
close(OLD);

$total1=0;
while () {
	if(/$find/)	{
		$total1=$total1+(substr($_,  index($_,"-") , 29) );
	}
}
$diff= $total-$total1;
print (NEW $diff);
print (NEW "\n");

# Wav 2 against Anti - User 1
system ("HVite -H users/tunney/hmm9/macros -H users/tunney/hmm9/hmmdefs -S users/$username/thres2.scp -i thres2.mlf -w wdnet2 -p 0.0 -s 5.0 users/tunney/dict users/tunney/monophones1");
open (OLD,"thres2.mlf") || die ("Unable to open thres2.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 2 against Anti - User 2
system ("HVite -H users/steve/hmm9/macros -H users/steve/hmm9/hmmdefs -S users/$username/thres2.scp -i thres2.mlf -w wdnet2 -p 0.0 -s 5.0 users/steve/dict users/steve/monophones1");
open (OLD,"thres2.mlf") || die ("Unable to open thres2.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 2 against Anti - User 3
system ("HVite -H users/ronan/hmm9/macros -H users/ronan/hmm9/hmmdefs -S users/$username/thres2.scp -i thres2.mlf -w wdnet2 -p 0.0 -s 5.0 users/ronan/dict users/ronan/monophones1");
open (OLD,"thres2.mlf") || die ("Unable to open thres2.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 2 against Anti - User 4
system ("HVite -H users/tom/hmm9/macros -H users/tom/hmm9/hmmdefs -S users/$username/thres2.scp -i thres2.mlf -w wdnet2 -p 0.0 -s 5.0 users/tom/dict users/tom/monophones1");
open (OLD,"thres2.mlf") || die ("Unable to open thres2.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 2 against Anti - User 5
system ("HVite -H users/jd/hmm9/macros -H users/jd/hmm9/hmmdefs -S users/$username/thres2.scp -i thres2.mlf -w wdnet2 -p 0.0 -s 5.0 users/jd/dict users/jd/monophones1");
open (OLD,"thres2.mlf") || die ("Unable to open thres2.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);


################################################## SAMPLE 3 -> T0003.WAV ########################################################

system ("HVite -H users/$username/hmm9/macros -H users/$username/hmm9/hmmdefs -S users/$username/thres3.scp -i thres3.mlf -w wdnet3 -p 0.0 -s 5.0 users/$username/dict users/$username/monophones1");
system ("HVite -H users/anti/hmm9/macros -H users/anti/hmm9/hmmdefs -S users/$username/thres3.scp -i antithres3.mlf -w wdnet3 -p 0.0 -s 5.0 users/anti/dict users/anti/monophones1");
open (OLD,"thres3.mlf") || die ("Unable to open thres3.mlf for reading");
open (ANTI1,"antithres3.mlf") || die ("Unable to open antithres3.mlf for reading");


$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
close(OLD);

$total1=0;
while () {
	if(/$find/)	{
		$total1=$total1+(substr($_,  index($_,"-") , 29) );
	}
}
$diff= $total-$total1;
print (NEW $diff);
print (NEW "\n");

# Wav 3 against Anti - User 1
system ("HVite -H users/tunney/hmm9/macros -H users/tunney/hmm9/hmmdefs -S users/$username/thres3.scp -i thres3.mlf -w wdnet3 -p 0.0 -s 5.0 users/tunney/dict users/tunney/monophones1");
open (OLD,"thres3.mlf") || die ("Unable to open thres3.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 2 against Anti - User 2
system ("HVite -H users/steve/hmm9/macros -H users/steve/hmm9/hmmdefs -S users/$username/thres3.scp -i thres3.mlf -w wdnet3 -p 0.0 -s 5.0 users/steve/dict users/steve/monophones1");
open (OLD,"thres3.mlf") || die ("Unable to open thres3.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 3 against Anti - User 3
system ("HVite -H users/ronan/hmm9/macros -H users/ronan/hmm9/hmmdefs -S users/$username/thres3.scp -i thres3.mlf -w wdnet3 -p 0.0 -s 5.0 users/ronan/dict users/ronan/monophones1");
open (OLD,"thres3.mlf") || die ("Unable to open thres3.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 3 against Anti - User 4
system ("HVite -H users/tom/hmm9/macros -H users/tom/hmm9/hmmdefs -S users/$username/thres2.scp -i thres3.mlf -w wdnet3 -p 0.0 -s 5.0 users/tom/dict users/tom/monophones1");
open (OLD,"thres3.mlf") || die ("Unable to open thres3.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 3 against Anti - User 5
system ("HVite -H users/jd/hmm9/macros -H users/jd/hmm9/hmmdefs -S users/$username/thres3.scp -i thres3.mlf -w wdnet3 -p 0.0 -s 5.0 users/jd/dict users/jd/monophones1");
open (OLD,"thres3.mlf") || die ("Unable to open thres3.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

################################################## SAMPLE 4 -> T0004.WAV ########################################################

system ("HVite -H users/$username/hmm9/macros -H users/$username/hmm9/hmmdefs -S users/$username/thres4.scp -i thres4.mlf -w wdnet4 -p 0.0 -s 5.0 users/$username/dict users/$username/monophones1");
system ("HVite -H users/anti/hmm9/macros -H users/anti/hmm9/hmmdefs -S users/$username/thres4.scp -i antithres4.mlf -w wdnet4 -p 0.0 -s 5.0 users/anti/dict users/anti/monophones1");
open (OLD,"thres4.mlf") || die ("Unable to open thres4.mlf for reading");
open (ANTI1,"antithres4.mlf") || die ("Unable to open antithres4.mlf for reading");


$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
close(OLD);

$total1=0;
while () {
	if(/$find/)	{
		$total1=$total1+(substr($_,  index($_,"-") , 29) );
	}
}
$diff= $total-$total1;
print (NEW $diff);
print (NEW "\n");

# Wav 4 against Anti - User 1
system ("HVite -H users/tunney/hmm9/macros -H users/tunney/hmm9/hmmdefs -S users/$username/thres4.scp -i thres4.mlf -w wdnet4 -p 0.0 -s 5.0 users/tunney/dict users/tunney/monophones1");
open (OLD,"thres4.mlf") || die ("Unable to open thres4.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 4 against Anti - User 2
system ("HVite -H users/steve/hmm9/macros -H users/steve/hmm9/hmmdefs -S users/$username/thres4.scp -i thres4.mlf -w wdnet4 -p 0.0 -s 5.0 users/steve/dict users/steve/monophones1");
open (OLD,"thres4.mlf") || die ("Unable to open thres4.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 4 against Anti - User 4
system ("HVite -H users/ronan/hmm9/macros -H users/ronan/hmm9/hmmdefs -S users/$username/thres4.scp -i thres4.mlf -w wdnet4 -p 0.0 -s 5.0 users/ronan/dict users/ronan/monophones1");
open (OLD,"thres4.mlf") || die ("Unable to open thres4.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 4 against Anti - User 4
system ("HVite -H users/tom/hmm9/macros -H users/tom/hmm9/hmmdefs -S users/$username/thres4.scp -i thres4.mlf -w wdnet4 -p 0.0 -s 5.0 users/tom/dict users/tom/monophones1");
open (OLD,"thres4.mlf") || die ("Unable to open thres4.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 4 against Anti - User 5
system ("HVite -H users/jd/hmm9/macros -H users/jd/hmm9/hmmdefs -S users/$username/thres4.scp -i thres4.mlf -w wdnet4 -p 0.0 -s 5.0 users/jd/dict users/jd/monophones1");
open (OLD,"thres4.mlf") || die ("Unable to open thres4.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);


################################################## SAMPLE 5 -> T0005.WAV ########################################################

system ("HVite -H users/$username/hmm9/macros -H users/$username/hmm9/hmmdefs -S users/$username/thres5.scp -i thres5.mlf -w wdnet5 -p 0.0 -s 5.0 users/$username/dict users/$username/monophones1");
system ("HVite -H users/anti/hmm9/macros -H users/anti/hmm9/hmmdefs -S users/$username/thres5.scp -i antithres5.mlf -w wdnet5 -p 0.0 -s 5.0 users/anti/dict users/anti/monophones1");
open (OLD,"thres5.mlf") || die ("Unable to open thres5.mlf for reading");
open (ANTI1,"antithres5.mlf") || die ("Unable to open antithres5.mlf for reading");


$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
close(OLD);

$total1=0;
while () {
	if(/$find/)	{
		$total1=$total1+(substr($_,  index($_,"-") , 29) );
	}
}
$diff= $total-$total1;
print (NEW $diff);
print (NEW "\n");

# Wav 5 against Anti - User 1
system ("HVite -H users/tunney/hmm9/macros -H users/tunney/hmm9/hmmdefs -S users/$username/thres5.scp -i thres5.mlf -w wdnet5 -p 0.0 -s 5.0 users/tunney/dict users/tunney/monophones1");
open (OLD,"thres5.mlf") || die ("Unable to open thres5.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 5 against Anti - User 2
system ("HVite -H users/steve/hmm9/macros -H users/steve/hmm9/hmmdefs -S users/$username/thres5.scp -i thres5.mlf -w wdnet5 -p 0.0 -s 5.0 users/steve/dict users/steve/monophones1");
open (OLD,"thres5.mlf") || die ("Unable to open thres5.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 5 against Anti - User 3
system ("HVite -H users/ronan/hmm9/macros -H users/ronan/hmm9/hmmdefs -S users/$username/thres5.scp -i thres5.mlf -w wdnet5 -p 0.0 -s 5.0 users/ronan/dict users/ronan/monophones1");
open (OLD,"thres5.mlf") || die ("Unable to open thres5.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 5 against Anti - User 4
system ("HVite -H users/tom/hmm9/macros -H users/tom/hmm9/hmmdefs -S users/$username/thres5.scp -i thres5.mlf -w wdnet5 -p 0.0 -s 5.0 users/tom/dict users/tom/monophones1");
open (OLD,"thres5.mlf") || die ("Unable to open thres5.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);

# Wav 5 against Anti - User 5
system ("HVite -H users/jd/hmm9/macros -H users/jd/hmm9/hmmdefs -S users/$username/thres5.scp -i thres5.mlf -w wdnet5 -p 0.0 -s 5.0 users/jd/dict users/jd/monophones1");
open (OLD,"thres5.mlf") || die ("Unable to open thres5.mlf for reading");
$total=0;
while () {
	if(/$find/)	{
		$total=$total+(substr($_,  index($_,"-") , 29) );
	}
}
$diff1= $total-$total1;
print (NEW1 $diff1);
print (NEW1 "\n");
close(OLD);


#close files
close(NEW);
close(NEW1);
close(ANTI1);

print "DONE";