#!/bin/sh

# This script will check out all of the pieces of FeatureLearning and CERT that are needed for
# exporting CERT for use by NRL or someone else outside the MPLab.
# Needed data files will be generated and anything that's not needed will be removed
# Subversion Revision: $LastChangedRevision$

if [ ! $# -eq 2 ]; then
   echo "$0 <MPT Revision> <CERT Revision>"
   echo "  Usage: You must supply a Subversion revision number for MPT and CERT. Checking for current revision..."
   CURMPT=`/sw/bin/svnversion -n $MPT`
   CURCERT=`/sw/bin/svnversion -n $MPT/../../CERT`
   echo "  Current revisions are:  ${CURMPT}  and  ${CURCERT}"
   exit
fi

# If any output directories already exist svn will complain, so tell the user then exit
if [ -d mptslidingwindow -o -d eyefinder -o -d CERTWrapper -o -d Documentation -o -d Misc -o -d aligneyes -o -d mp_auCoder ]; then
    echo "Some output directories already exist, you will need to clear out the destination directory before exporting CERT 2.0"
    exit
fi

STARTDATE=`date`
DOXYGEN=/Users/Shared/Applications/Doxygen.app/Contents/Resources/doxygen
QUIET=-q

MPTREV=$1
CERTREV=$2

# 1) checkout all the different pieces
echo "Exporting FeatureLearning source..."
svn export ${QUIET} -r ${MPTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/FeatureLearning/trunk/Runtime/Libraries/mptslidingwindow/src mptslidingwindow
svn export ${QUIET} -r ${MPTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/FeatureLearning/trunk/Runtime/Libraries/eyefinder/src eyefinder
svn export ${QUIET} -r ${MPTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/FeatureLearning/trunk/Runtime/Libraries/smiledetector/src smiledetector
svn export ${QUIET} -r ${MPTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/FeatureLearning/trunk/Runtime/Build Build
echo "Exporting CERT source..."
svn export ${QUIET} -r ${CERTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/CERT/trunk/Build/aligneyes aligneyes
svn export ${QUIET} -r ${CERTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/CERT/trunk/Build/mp_auCoder mp_auCoder
svn export ${QUIET} -r ${CERTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/CERT/trunk/Build/Misc Misc
svn export ${QUIET} -r ${CERTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/CERT/trunk/Build/CERTWrapper CERTWrapper
svn export ${QUIET} -r ${CERTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/CERT/trunk/Build/CERT2_VS2005 CERT2_VS2005
#svn export ${QUIET} -r ${CERTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/CERT/trunk/Build/ExpressionFilter ExpressionFilter
svn export ${QUIET} -r ${CERTREV} svn+ssh://mplab.ucsd.edu/usr/local/svnroot/CERT/trunk/Documentation Documentation

# 2) Generate data files
echo "Generating Gabor Kernels..."
make -s -C mp_auCoder/KernelExport CERT=$MPT/../../CERT IS_SILENT=1
echo "Generating SVM weight files..."
make -s -C mp_auCoder/SVMWeights CERT=$MPT/../../CERT IS_SILENT=1

# 2) Now do some moving around of files and cleaning up
find . -name UnitTests -type d | xargs rm -rf
find . -name 'Makefile*' -type f | xargs rm -rf
find . -name 'CFLAGS-*.txt' -type f | xargs rm -rf
find . -name 'Jamfile' -type f | xargs rm -rf   

# Set the revision numbers
sed -e "s/#define SVN_REV \"[^\"]*\"/#define SVN_REV \"${MPTREV}\"/" Build/mptVersion.c > Misc/mptVersion.c
mv Build/mptVersion.h Misc/
rm -rf Build
sed -e "s/#define SVN_REV \"[^\"]*\"/#define SVN_REV \"${CERTREV}\"/" Misc/certVersion.c > Misc/certVersion.c.tmp
mv Misc/certVersion.c.tmp Misc/certVersion.c
rm -rf Misc/makeCERT2.sh
rm -rf Misc/makeCERT2.mk

rm -rf CERTWrapper/mp_CERTMex.cpp

rm -rf aligneyes/BoostTests
rm -rf aligneyes/TestSerialization
rm -rf aligneyes/mexopts*.sh
rm -rf aligneyes/mp_alignEyes.*
rm -rf aligneyes/rimage_matlab.*

#rm -rf eyefinder/EyeWeightsFiles/config.h                          

rm -rf mptslidingwindow/perftest*

sed -e "s/#define  *II_DO_LOG  *(1)/#define II_DO_LOG (0)/" smiledetector/interpolate.h > aligneyes/interpolate.h
rm -rf smiledetector

mv mp_auCoder/KernelExport/kernels-96x96 mp_auCoder
rm -rf mp_auCoder/KernelExport
rm -rf mp_auCoder/kernels-96x96/Data

mv mp_auCoder/SVMWeights/SVMWeights mp_auCoder/SVMWeights.tmp
rm -rf mp_auCoder/SVMWeights
mv mp_auCoder/SVMWeights.tmp mp_auCoder/SVMWeights

rm -f mp_auCoder/mp_auCoder.cpp mp_auCoder/mp_auCoder.h

# Generate Doxygen output
echo "Generating source documentation..."
${DOXYGEN} Documentation/Doxyfile 
rm -rf Documentation/Doxyfile
rm -rf "Documentation/CERT Flow Chart.oo3"

# Generate a test app, run it, and compare to expected values
echo
echo "Generating a test app"
cd ../CERT2_0_TEST
make -s clean
ln -s ../CERT2_0/mp_auCoder/SVMWeights
make -s

echo
echo "Running test app"
AUS=`./CERT 1005.rimage`
# Without expression filtering, SVMWeights as of 7/31/2006, Plus Gwens extra step
GOOD_AUS="-0.28297521, -0.071829496, 1.027907, -1.6110861, -0.62560298, -1.2335333, -1.8351188, -1.05746"
# Without expression filtering, SVMWeights as of 7/31/2006
#GOOD_AUS="7.6072709, 0.336321, 1.6415271, -6.7245087, -3.0305008, -0.53009088, -11.736489, -6.7066323"
# Without expression filtering
#GOOD_AUS="0.904266, -0.684095, 1.13765, -1.43378, -0.0796643, -0.818025, -1.10107, 0.0245506"
# With expression filtering
#GOOD_AUS="0.853643, 0.592146, 0.853898, 0.410414, 1.08169e-05, 0.535135, 0.784918, 0.595064"

if [ "${AUS}" == "${GOOD_AUS}" ]; then
    echo "Test appears to have run correctly"
else
    echo "Uh, oh! you got trouble! Correct results should be:"
    echo "${GOOD_AUS}"
    echo "But you got:"
    echo "${AUS}"
fi

echo
echo "Exporting started: ${STARTDATE} and ended `date`"
