#!/bin/bash

# If the bifold executable doesn't exist yet, make it.
# Also, make the EnergyPlot executable so the save file can be checked, if it
# doesn't exist yet.
echo '    Preparation of bifold tests started...'
echo '        Preparation in progress...'
if [[ ! -f ../exe/bifold ]]
then
cd ..; make bifold >& /dev/null; cd tests; 
fi

if [[ ! -f ../exe/EnergyPlot ]]
then
cd ..; make EnergyPlot >& /dev/null; cd tests;
fi
echo '    Preparation of bifold tests finished.'

# Test bifold_without_options.
echo '    bifold_without_options testing started...'
../exe/bifold $DOUBLESEQ bifold_without_options_test_output.ct 1>/dev/null 2>bifold_without_options_errors.txt
diff bifold_without_options_test_output.ct bifold/bifold_without_options_OK.ct >& bifold_without_options_diff_output.txt
checkErrors bifold_without_options bifold_without_options_errors.txt bifold_without_options_diff_output.txt
echo '    bifold_without_options testing finished.'

# Test bifold_dna_option.
echo '    bifold_dna_option testing started...'
../exe/bifold $DOUBLESEQ bifold_dna_option_test_output.ct -d 1>/dev/null 2>bifold_dna_option_errors.txt
diff bifold_dna_option_test_output.ct bifold/bifold_dna_option_OK.ct >& bifold_dna_option_diff_output.txt
checkErrors bifold_dna_option bifold_dna_option_errors.txt bifold_dna_option_diff_output.txt
echo '    bifold_dna_option testing finished.'

# Test bifold_intramolecular_option.
echo '    bifold_intramolecular_option testing started...'
../exe/bifold $DOUBLESEQ bifold_intramolecular_option_test_output.ct -i 1>/dev/null 2>bifold_intramolecular_option_errors.txt
diff bifold_intramolecular_option_test_output.ct bifold/bifold_intramolecular_option_OK.ct >& bifold_intramolecular_option_diff_output.txt
checkErrors bifold_intramolecular_option bifold_intramolecular_option_errors.txt bifold_intramolecular_option_diff_output.txt
echo '    bifold_intramolecular_option testing finished.'

# Test bifold_loop_option.
echo '    bifold_loop_option testing started...'
../exe/bifold $DOUBLESEQ bifold_loop_option_test_output.ct -l 5 1>/dev/null 2>bifold_loop_option_errors.txt
diff bifold_loop_option_test_output.ct bifold/bifold_loop_option_OK.ct >& bifold_loop_option_diff_output.txt
checkErrors bifold_loop_option bifold_loop_option_errors.txt bifold_loop_option_diff_output.txt
echo '    bifold_loop_option testing finished.'

# Test bifold_max_structures_option.
echo '    bifold_max_structures_option testing started...'
../exe/bifold $DOUBLESEQ bifold_max_structures_option_test_output.ct -m 2 1>/dev/null 2>bifold_max_structures_option_errors.txt
diff bifold_max_structures_option_test_output.ct bifold/bifold_max_structures_option_OK.ct >& bifold_max_structures_option_diff_output.txt
checkErrors bifold_max_structures_option bifold_max_structures_option_errors.txt bifold_max_structures_option_diff_output.txt
echo '    bifold_max_structures_option testing finished.'

# Test bifold_percent_difference_option.
echo '    bifold_percent_difference_option testing started...'
../exe/bifold $DOUBLESEQ bifold_percent_difference_option_test_output.ct -p 1 1>/dev/null 2>bifold_percent_difference_option_errors.txt
diff bifold_percent_difference_option_test_output.ct bifold/bifold_percent_difference_option_OK.ct >& bifold_percent_difference_option_diff_output.txt
checkErrors bifold_percent_difference_option bifold_percent_difference_option_errors.txt bifold_percent_difference_option_diff_output.txt
echo '    bifold_percent_difference_option testing finished.'

# Test bifold_save_file_option.
echo '    bifold_save_file_option testing started...'
../exe/bifold $DOUBLESEQ bifold_save_file_option_test_output.ct -s bifold_sav_test.sav 1>/dev/null 2>bifold_save_file_option_errors.txt
diff bifold_save_file_option_test_output.ct bifold/bifold_save_file_option_OK.ct >& bifold_save_file_option_diff_output.txt
checkErrors bifold_save_file_option bifold_save_file_option_errors.txt bifold_save_file_option_diff_output.txt
echo '    bifold_save_file_option_testing finished.'

# Test bifold_sav_plot (The save plot generated by the save file option).
echo '    bifold_sav_plot testing started...'
../exe/EnergyPlot bifold_sav_test.sav bifold_sav_plot_test_output.ps 1>/dev/null 2>bifold_sav_plot_errors.txt
diff bifold_sav_plot_test_output.ps bifold/bifold_sav_plot.ps >& bifold_sav_plot_diff_output.txt
checkErrors bifold_sav_plot bifold_sav_plot_errors.txt bifold_sav_plot_diff_output.txt
echo '    bifold_sav_plot testing finished.'

# Test bifold_temperature_option.
echo '    bifold_temperature_option testing started...'
../exe/bifold $DOUBLESEQ bifold_temperature_option_test_output.ct -t 250 1>/dev/null 2>bifold_temperature_option_errors.txt
diff bifold_temperature_option_test_output.ct bifold/bifold_temperature_option_OK.ct >& bifold_temperature_option_diff_output.txt
checkErrors bifold_temperature_option bifold_temperature_option_errors.txt bifold_temperature_option_diff_output.txt
echo '    bifold_temperature_option testing finished.'

# Test bifold_window_size_option.
echo '    bifold_window_size_option testing started...'
../exe/bifold $DOUBLESEQ bifold_window_size_option_test_output.ct -w 20 1>/dev/null 2>bifold_window_size_option_errors.txt
diff bifold_window_size_option_test_output.ct bifold/bifold_window_size_option_OK.ct >& bifold_window_size_option_diff_output.txt
checkErrors bifold_window_size_option bifold_window_size_option_errors.txt bifold_window_size_option_diff_output.txt
echo '    bifold_window_size_option_testing finished.'

# Clean up any extra files made over the course of the bifold tests.
echo '    Cleanup of bifold tests started...'
echo '        Cleanup in progress...'
rm -f bifold_without_options*
rm -f bifold_dna_option*
rm -f bifold_intramolecular_option*
rm -f bifold_loop_option*
rm -f bifold_max_structures_option*
rm -f bifold_percent_difference_option*
rm -f bifold_sav*
rm -f bifold_temperature_option*
rm -f bifold_window_size_option*
echo '    Cleanup of bifold tests completed.'