#!/usr/bin/env bash

VAST_HOME=$PWD

make

rm -rf test_data
mkdir test_data

# Test the standard set of CCD images
cd test_data
if [ -f ../../sample_data.tar.bz2 ];then
 cp ../../sample_data.tar.bz2 .
else
 wget -c ftp://scan.sai.msu.ru/pub/software/vast/sample_data.tar.bz2
fi
tar -xjf sample_data.tar.bz2
cd $VAST_HOME
cp default.sex.ccd_example default.sex
cp default.conv.backup default.conv
./vast -f test_data/sample_data/f_72-0*
# md5sum image000* out* vast_command_line.log vast_image_details.log vast_images_catalogs.log \
# vast_sigma_selection_curve.log vast_stars_with_large_sigma.log  vast_magnitude_calibration_details_log/* > lib/sample_data_test.md5
md5sum -c lib/sample_data_test.md5 &> vast_test01.log
if [ $? -ne 0 ];then
 cat vast_test01.log
 echo "TEST 1 failed!"
 exit 1
else
 echo "TEST 1 passed!"
fi

#exit # !!!


# Test the standard set of CCD images
cd test_data
if [ -f ../../transient_detection_test_Ceres.tar.bz2 ];then
 cp ../../transient_detection_test_Ceres.tar.bz2 .
else
 wget -c ftp://scan.sai.msu.ru/pub/software/vast/transient_detection_test_Ceres.tar.bz2
fi
# Copy local version of Tycho-2 if there is one
if [ ! -d lib/catalogs/tycho2 ];then
 if [ -d ../tycho2 ];then
  cp -r ../tycho2 lib/catalogs/tycho2
 fi
fi

tar -xjf transient_detection_test_Ceres.tar.bz2
cd $VAST_HOME
cp default.sex.telephoto_lens default.sex
cp default.conv.telephoto_lens default.conv
./vast -x99 -fuk test_data/transient_detection_test_Ceres/reference_images/* test_data/transient_detection_test_Ceres/second_epoch_images/* 
if [ $? -ne 0 ];then
 echo "TEST 2 failed!"
 exit 1
fi
util/transients/search_for_transients_single_field.sh
# Test individual objects
grep "Ceres" transient_report/index.html &>/dev/null
if [ $? -ne 0 ];then
 echo "TEST 2 failed!"
 exit 1
fi
grep "Lutetia" transient_report/index.html &>/dev/null
if [ $? -ne 0 ];then
 echo "TEST 2 failed!"
 exit 1
fi
grep "AW Tau" transient_report/index.html &>/dev/null
if [ $? -ne 0 ];then
 echo "TEST 2 failed!"
 exit 1
fi
grep "DO Gem" transient_report/index.html &>/dev/null
if [ $? -ne 0 ];then
 echo "TEST 2 failed!"
 exit 1
fi
grep "LP Gem" transient_report/index.html &>/dev/null
if [ $? -ne 0 ];then
 echo "TEST 2 failed!"
 exit 1
fi
grep "RR Tau" transient_report/index.html &>/dev/null
if [ $? -ne 0 ];then
 echo "TEST 2 failed!"
 exit 1
fi
grep "AU Tau" transient_report/index.html &>/dev/null
if [ $? -ne 0 ];then
 echo "TEST 2 failed!"
 exit 1
else
 echo "TEST 2 passed!"
fi


# !!!! The following tests consume a lot of traffic. They are disabled by default !!!!
exit

## Test the standard set of photographic images
cd test_data
if [ -f ../../test_data_photo.tar.bz2 ];then
 cp ../../test_data_photo.tar.bz2 .
else
 wget -c ftp://scan.sai.msu.ru/pub/software/vast/test_data_photo.tar.bz2
fi
tar -xjf test_data_photo.tar.bz2
cd $VAST_HOME
cp default.sex.beta_Cas_photoplates default.sex
./vast -f -p test_data/test_data_photo/*
# md5sum image0* out* vast_image_details.log vast_sigma_selection_curve.log vast_stars_with_large_sigma.log \
# vast_command_line.log vast_magnitude_calibration_details_log/* > lib/test_data_photo_test.md5
md5sum -c lib/test_data_photo_test.md5 &> vast_test03.log
if [ $? -ne 0 ];then
 cat vast_test03.log
 echo "TEST 3 failed!"
 exit 1
else
 echo "TEST 3 passed!"
fi

# test WCS calibration
for i in `cat vast_image_details.log | awk '{print $17}'` ; do util/wcs_image_calibration.sh $i ;done
NUMBER_OF_WCS_CALIBRATED_IMAGES=`ls wcs*fit |grep -c ""`
if [ $NUMBER_OF_WCS_CALIBRATED_IMAGES -ne 150 ];then
 echo "TEST 4 failed!"
 exit 1
else
 echo "TEST 4 passed!"
fi


# Test star idethification 
util/identify.sh out00422.dat
# Should be V834 Cas

rm -rf test_data

cp default.sex.ccd_example default.sex  

