# Set flag dump_file to 1 in order to regenerate script files with actual data
# used as reference. In this mode all tests intentionaly report failure. 
set dump_file 0
########################################################################
set mist 0;
# Dimensions
set nbDim_First 0; set nbDim_Second 0;
set nbDimSize_First 0; set nbDimSize_Second 0;
set nbDimLoc_First 0; set nbDimLoc_Second 0;
set nbAng_First 0; set nbAng_Second 0;
set nbDimWthPath_First 0; set nbDimWthPath_Second 0;
set nbCommon_First 0; set nbCommon_Second 0;
# Tolerances
set nbTol_First 0; set nbTol_Second 0;
set nbTolWthMod_First 0; set nbTolWthMod_Second 0;
set nbTolWthMax_First 0; set nbTolWthMax_Second 0;
set nbTolWthDat_First 0; set nbTolWthDat_Second 0;
# Datums
set nbDat_First 0;
set nbDatT_First 0;
set nbAttDat_First 0;
###################################################################
set ref_Compare 0
set todo_msg ""
set todo_mask "puts \"TODO CR26859 ALL: "
set end_line "\" \n"
##################################################################

# Read original file
if { [string length $filename] > 1} {
  set path_file [locate_data_file $filename]
  if { [catch { ReadStep D_First $path_file } catch_result] } {
    set err_msg "Error: First - file was not read - exception "
    puts $err_msg
    append todo_msg $todo_mask $err_msg $end_line
    set mist 1
  }
} else {
  set mist 1
}

# Get information about translation
if { $mist < 1} {           
  puts ""
  set xst [ XDumpNbDGTs D_First f] 

  if { [llength $xst] > 0 } {
    regexp {NbOfDimensions +: +([-0-9.+eE]+)} $xst full nbDim_First
    regexp {NbOfDimensionalSize +: +([-0-9.+eE]+)} $xst full nbDimSize_First
    regexp {NbOfDimensionalLocation +: +([-0-9.+eE]+)} $xst full nbDimLoc_First
    regexp {NbOfAngular +: +([-0-9.+eE]+)} $xst full nbAng_First
    regexp {NbOfWithPath +: +([-0-9.+eE]+)} $xst full nbWthPath_First
    regexp {NbOfCommonLabels +: +([-0-9.+eE]+)} $xst full nbCommon_First
    regexp {NbOfTolerances +: +([-0-9.+eE]+)} $xst full nbTol_First
    regexp {NbOfGTWithModifiers +: +([-0-9.+eE]+)} $xst full nbTolWthMod_First
    regexp {NbOfGTWithMaxTolerance +: +([-0-9.+eE]+)} $xst full nbTolWthMax_First
    regexp {NbOfGTWithDatums +: +([-0-9.+eE]+)} $xst full nbTolWthDat_First
    regexp {NbOfDatumFeature +: +([-0-9.+eE]+)} $xst full nbDat_First
    regexp {NbOfAttachedDatum +: +([-0-9.+eE]+)} $xst full nbAttDat_First
    regexp {NbOfDatumTarget +: +([-0-9.+eE]+)} $xst full nbDatT_First
  } else {
      puts " GDT information was NOT provided"
    }
}

if { $mist != 1 } {  
  puts "" 
  set result ""
  append result [format $xst]
}

# Writing file
if { $mist < 1} {
  puts " "
  puts "-----------------------------WRITING FILE ------------------------------"
  if { [catch { WriteStep D_First $imagedir/${casename}_D_First.stp } catch_result] } {
    set err_msg "Error: First - file was not written - exception"
    puts $err_msg
    append todo_msg $todo_mask $err_msg $end_line
    set mist 1
  }
  if { $mist < 1 } {
    if { [catch { ReadStep D_Second $imagedir/${casename}_D_First.stp } catch_result] } {
      set err_msg "Error: Second - file was not read - exception"
      puts $err_msg
      append todo_msg $todo_mask $err_msg $end_line
      set mist 1
    }
  }
}

catch {[file delete $imagedir/${casename}_D_First.stp]}
if { [catch { Close D_First } catch_result] } {
  set err_msg "Error : cannot close a document D_First - exception"
  puts $err_msg
}

# Get information about translation
if { $mist < 1} {           
  puts ""
  set xst2 [ XDumpNbDGTs D_Second f] 

  if { [llength $xst2] > 0 } {
    regexp {NbOfDimensions +: +([-0-9.+eE]+)} $xst2 full nbDim_Second
    regexp {NbOfDimensionalSize +: +([-0-9.+eE]+)} $xst2 full nbDimSize_Second
    regexp {NbOfDimensionalLocation +: +([-0-9.+eE]+)} $xst2 full nbDimLoc_Second
    regexp {NbOfAngular +: +([-0-9.+eE]+)} $xst2 full nbAng_Second
    regexp {NbOfWithPath +: +([-0-9.+eE]+)} $xst2 full nbWthPath_Second
    regexp {NbOfCommonLabels +: +([-0-9.+eE]+)} $xst2 full nbCommon_Second
    regexp {NbOfTolerances +: +([-0-9.+eE]+)} $xst2 full nbTol_Second
    regexp {NbOfGTWithModifiers +: +([-0-9.+eE]+)} $xst2 full nbTolWthMod_Second
    regexp {NbOfGTWithMaxTolerance +: +([-0-9.+eE]+)} $xst2 full nbTolWthMax_Second
    regexp {NbOfGTWithDatums +: +([-0-9.+eE]+)} $xst2 full nbTolWthDat_Second
    regexp {NbOfDatumFeature +: +([-0-9.+eE]+)} $xst2 full nbDat_Second
    regexp {NbOfAttachedDatum +: +([-0-9.+eE]+)} $xst2 full nbAttDat_Second
    regexp {NbOfDatumTarget +: +([-0-9.+eE]+)} $xst2 full nbDatT_Second
  } else {
      puts " GDT information was NOT provided"
    }
  if { [catch { Close D_Second } catch_result] } {
    set err_msg "Error : cannot close a document D_Second - exception"
    puts $err_msg
  }
}

if { $mist != 1 } {  
  puts "" 
  set result2 ""
  append result2 [format $xst2]
}

set err_compare_ref ""
# Put reference data to the test script file if option "dump" is set
if { $dump_file == 1 } { 
  set fd_stream  [open $dirname/$groupname/$gridname/$casename w] 
  puts $fd_stream "# !!!! This file is generated automatically, do not edit manually! See end script"
  puts $fd_stream "set filename $filename"
  if { $mist != 1 } {
    puts $fd_stream "" 
    puts $fd_stream "set ref_data \{"
    puts $fd_stream $result
    puts $fd_stream "\}"
  }
  close $fd_stream 
} elseif { $mist != 1 } {
  puts "========================== Comparision with reference data ========"
  puts ""
  # Comparision of reference data with obtained result
  set ref_list [split $ref_data \n]
  set cur_list [split $result \n]
  set cur2_list [split $result2 \n]
  set nb_ref [llength $ref_list]
  for { set i 0 } { $i < $nb_ref } { incr i } {
    set j [expr $i +1]
    set refstr [lindex $ref_list $j]
    set curstr [lindex $cur_list $i]
    set cur2str [lindex $cur2_list $i]
    set isOK 1; 

    if {[string equal $refstr $curstr] == 0} {
      set isOK 0 
    }
    
    if {[string equal $curstr $cur2str] == 0} {
      set isOK 0 
    } 

    if { $isOK == 0}  {
      incr ref_Compare
      append err_compare_ref " Reference data             - $refstr\n"
      append err_compare_ref " Current data               - $curstr\n"
      append err_compare_ref " Current data after writing - $cur2str\n"
      append err_compare_ref "--------------------------------------------------------------------\n"
    }
  }
}

if { $dump_file != 0 } {
    puts "Error : Running in regeneration mode, comparision was not performed!"
    if { $mist != 1 } {
      	 puts "Generation of test file $groupname/$gridname/$casename successful"
    } else {
      	 puts "Generation of reference data failed"
    }
} else {
    if { $ref_Compare > 0} {
        puts "Error : $ref_Compare differences with reference data found :\n$err_compare_ref"
    } else {
        puts "Comparision of current result with reference data - OK\n"
    }    
}

puts "--------------------------------------------------------------------"
puts ""

puts "TEST COMPLETED"