#!/usr/bin/perl -w

print "Please enter the name of raw data file: ";
$input=; chomp($input);

print "Please enter the name of output file\n";
print "WITHOUT a dot and a 3 letter extension: ";

$output=; chomp($output);

$new=$output.".dat";
$new1p=$output."1p.blg";
$new2p=$output."2p.blg";
$new3p=$output."3p.blg";
$kfn=$output.".kfn";
$batch1=$output."1p";
$batch2=$output."2p";
$batch3=$output."3p";

open (FILE1 ,"<$input");
open (FILE2 ,">$new");
open (FILE3 ,"$new1p");
open (FILE2P ,">$new2p");
open (FILE3P ,">$new3p");
open (KFN ,">$kfn");

#extract the key from the raw data
while(){

if($_ =~ /Key:/){
print KFN "        ";
print KFN "$'";
}

#extract the tester ID

 if($_ =~ /000....../){
  print FILE2 "$&";
 }

#extract the item answers

 if($_ =~ /en\d........./){
  print FILE2 "000$'";
 }

#extract the number of items

if($_ =~ /Number of Items:    /){
  $numitem="$'";
  chomp($numitem);
  print "The number of items is: ";
  print "$numitem";
  print "\n";
 }

}

#copy the bilog template and create programs for 1p, 2p, and 2p models

while(){
s/NPArm=/NPArm=1/;
s/AAA/$batch1/;
s/XXX/$output/;
s/YYY/$numitem/;
print FILE1P "$_";
s/NPArm=1/NPArm=2/;
s/$batch1/$batch2/;
print FILE2P "$_";
s/NPArm=2/NPArm=3/;
s/$batch2/$batch3/;
print FILE3P "$_";
}

close(FILE1);
close(FILE2);
close(FILE3);
close(FILE1P);
close(FILE2P);
close(FILE3P);
close(KFN);

print "Running Bilog in the batch mode\n";

system("blgwin $batch1 250000 20000 0");
system("blgwin $batch2 250000 20000 0");
system("blgwin $batch3 250000 20000 0");


#clean bilog program here

# Clean Phase1 files

for($i=1;$i<=3;$i++){


$file1=$output."$i"."p.PH1";
$file2=$output."$i"."cp1.dat";

open (FILE1 ,"<$file1");
open (FILE2 ,">$file2");

while(){

if($_ =~ /1000\.0/){
print FILE2 "$'";
} 
}

close(FILE1);
close(FILE2);


# Clean Phase2 files

$file1=$output."$i"."p.PH2";
$file2=$output."$i"."cp2";

$new1=$file2.".tmp";
$new2=$file2.".dat";

open (FILE1 ,"<$file1");
open (FILE2 ,">$new1");

while(){

if($_ =~ /\|/){
print FILE2 "$'";
} 
}
close(FILE1);
close(FILE2);

open(FILE3 ,"<$new1");
open(FILE4 ,">$new2");
while(){
if ($_ =~ /\.0/){
chomp;
}

s/\|/ /g;
s/\*/ /g;
s/\(/ /g;
s/\)/ /g;
print FILE4 "$_"; 
}

close(FILE3);
close(FILE4);
unlink("$new1");

# Clean Phase3 files

$file1=$output."$i"."p.PH3";
$file2=$output."$i"."cp3";

$new1=$file2.".tmp";
$new2=$file2.".dat";

open (FILE1 ,"<$file1");
open (FILE2 ,">$new1");

while(){
if($_ =~ /RANDOM/){
print FILE2 "$_";
} 
}
close(FILE1);
close(FILE2);

open(FILE3 ,"<$new1");
open(FILE4 ,">$new2");
while(){
if($_=~/\|.*\|/){
$test=$&;
$test =~ s/\|/ /g;
print FILE4 "$test";
print FILE4 "\n"; 
}
}

close(FILE3);
close(FILE4);
unlink($new1);

}

#append theta from phase 3 with the cleaned raw data

$finalout=$output.".theta";

$one=$output."1"."cp3.dat";
$two=$output."2"."cp3.dat";
$three=$output."3"."cp3.dat";
open (FILE ,"<$new");
open (FILE1 ,"<$one");
open (FILE2 ,"<$two");
open (FILE3 ,"<$three");
open (OUTPUT ,">>$finalout");

LOOP:while(){
chomp;
print OUTPUT "$_";
while(){
chomp;
print OUTPUT "$_";
while(){
chomp;
print OUTPUT "$_";
while(){
chomp;
print OUTPUT "$_";
print OUTPUT "\n";
next LOOP;
}
}
}
}

close(FILE);
close(FILE1);
close(FILE2);
close(FILE3);
close(OUTPUT);

#Copy the SAS template and create a SAS program

$sasout=$output.".sas";

open (SASFILE ,"$sasout");

open(OUTSAS ,">>$sasout");
while(){
s/AAAAA/$output/g;
s/GGGGG/$numitem/;
print OUTSAS "$_"; 
}

close(SASFILE);
close(OUTSAS);

print "Running SAS in the batch mode\n";

system("sas $sasout");

$test=$output."_final.txt";
$res=$output."_results";
$lst=$output.".lst";

#Insert headers as variable names in the final results
open (FILE2,">$res");
print FILE2 "id percent logit pearson biserial 1p_a 1p_b 1p_chisq 1p_df 1p_fit 1p_inter_se 1p_a_se 1p_b_se 1p_disper_se 1p_g_se 1p_prob 1p-5 1p-4 1p-3 1p-2 1p-1 1p0 1p+1 1p+2 1p+3 1p+4 1p+5 2p_a 2p_b 2p_chisq 2p_df 2p_fit 2p_inter_se 2p_a_se 2p_b_se 2p_disper_se 2p_g_se 2p_prob 2p-5 2p-4 2p-3 2p-2 2p-1 2p0 2p+1 2p+2 2p+3 2p+4 2p+5 3p_a 3p_b 3p_g 3p_chisq 3p_df 3p_fit 3p_inter_se 3p_a_se 3p_b_se 3p_disper_se 3p_g_se 3p_prob 3p-5 3p-4 3p-3 3p-2 3p-1 3p0 3p+1 3p+2 3p+3 3p+4 3p+5";
print FILE2 "\n";
close(FILE2);

open(FILE1,"<$test");
open(FILE2,">>$res");

while(){
print FILE2 "$_";
}

close(FILE1);
close(FILE2);

print "The final result is in the text file called $res ";
print "which can be opened in DataDesk\n";
print "The option X theta tables are in the $lst\n";