#!/bin/bash

. $TEST_ROOT_DIR/run-test-common

# specific TIMIT variables
. $TEST_DIR/../run-timit-test-common

# Train:
cntkrun TIMIT_TrainAutoEncoder_ndl_deprecated.cntk "$CntkArguments TIMIT_TrainAutoEncoder=[reader=[useMersenneTwisterRand=true]]" || exit $?

# Copy the test data to the test run directory, so that we do not damage anything
DataDir=$TEST_RUN_DIR/TestData
mkdir $DataDir
cp -R $DataSourceDir/* $DataDir || exit $?

# Write:
cntkrun TIMIT_WriteBottleneck.cntk "$CntkArguments TIMIT_WriteBottleneck=[reader=[useMersenneTwisterRand=true]]"
ExitCode=$?

if [ $ExitCode == 0 ]; then
  echo Write succeeded.

  # Restore DataDir prior to modification through cntkrun (on Cygwin)
  DataDir=$TEST_RUN_DIR/TestData

  # Compare the output files
  OutputDir=$DataDir/bottleneck

  if [ "$OS" == "Windows_NT" ]; then
      ExpectedOutputFile=$TEST_DIR/expected_output_md5sum.windows.txt
  else
      ExpectedOutputFile=$TEST_DIR/expected_output_md5sum.linux.txt
  fi
  
  # TODO need to watch this for a while. If this turns out to be
  # non-deterministic we need to disable this / find a different output
  # validation.
  cd $OutputDir && md5sum --quiet -c $ExpectedOutputFile
  ExitCode=$?
  if [ $ExitCode == 0 ]; then
    echo MD5 check succeeded.
  else
    # Using explicit path to find to disambiguate from find.exe on Windows
    /usr/bin/find $OutputDir -type f -print0 | xargs -0 md5sum
    echo Error: Output files are different. More info above.
  fi
fi

# Delete the test data
rm -rf $DataDir

exit $ExitCode
