#!/bin/bash

BUILD="obj"
DEFAULT_PYTHON="python-2.6.1"
PYTHON=/build/toolchain/lin32/${DEFAULT_PYTHON}/bin/python
GDB=/build/toolchain/lin64/gdb-7.5/bin/gdb

RUN_DEBUG=0
newCmdLine=""

VMW_PRODUCT="vcenter"; export VMW_PRODUCT

if [ ! "${VMW_ARCHITECTURE}" ]; then
   VMW_ARCHITECTURE="x64"; export VMW_ARCHITECTURE
fi

if [ ! "${VMW_SOURCE}" ]; then
   VMW_SOURCE="."; export VMW_SOURCE
fi

if [ ! "${VMW_BOD}" ]; then
   VMW_BOD="${VMW_SOURCE}/BOD"; export VMW_BOD
fi


for OPTION in "$@"
do
   case $OPTION in
      -c)
         echo "Compiling DRM apps... "
         cd ..
         bbq/bbq --buildtype ${BUILD} --scheduler NUMCPUS -- install.drmapps
         cd bora
         ;;
       --compile)
         echo "Compiling DRM apps... "
         cd ..
         bbq/bbq --buildtype ${BUILD} --scheduler NUMCPUS -- install.drmapps
         cd bora
         ;;
       -D)
         RUN_DEBUG=1
         ;;
       *)
         if [ "$newCmdLine" == "" ]; then
             newCmdLine="$OPTION"
         else
             newCmdLine="$newCmdLine $OPTION"
         fi
   esac
done

USE_STRIPPED=true

if [ ! "${DRMAPPS}" ]; then
   DRMAPPS_HASH=`ls -td ../BOD/vcenter-drmapps/x64/${BUILD}/linux/* | rev |\
                 cut -d'/' -f1 | rev | grep '^[0-9A-Fa-f]\+' | head -n1`
   DRMAPPS=${PWD}/../BOD/vcenter-drmapps/x64/${BUILD}/linux/${DRMAPPS_HASH}/
   DRMAPPS+=build/bora/build/drmapps/${BUILD}/drm-apps
   USE_STRIPPED=false
fi

REPLAY_BIN=${DRMAPPS}/bin/drmdumpprettyprinter
APPS_LIBS=${DRMAPPS}/lib

if [ ${RUN_DEBUG} -eq 1 ];then
    LD_LIBRARY_PATH=${APPS_LIBS} ${GDB} --args ${REPLAY_BIN} ${newCmdLine}
else
    LD_LIBRARY_PATH=${APPS_LIBS} ${REPLAY_BIN} ${newCmdLine}
fi
