#!/bin/bash

package=OpenCV
version=-2.4.9

progname=$0

source ../cascade-make/lib/cascade-lib.bash GEXT_OPENCV ../.. $*

prefix=$GEXTOPENCV_INSTALLED

#export CFLAGS="$CFLAGS -I$GEXTAUDIODB_INSTALLED/include"
#export CPPFLAGS="$CPPFLAGS -I$GEXTAUDIODB_INSTALLED/include"
#export CXXFLAGS="$CXXFLAGS -I$GEXTAUDIODB_INSTALLED/include"
#export LDFLAGS="$LDFLAGS -L$GEXTAUDIODB_INSTALLED/lib"

#echo GSDLOS = $GSDLOS

opt_run_untar $force_untar $auto_untar $package $version

# Whether force_config is set for this package depends on CMake test
# to see if 'release' directory has been created or no
if [ $auto_config = "1" ] ; then
  if [ ! -d $package$version/release ] ; then
    force_config=1
  fi
fi

if type -p opencv_createsamples; then
  echo "found opencv in PATH"
  _OPENCV=opencv_createsamples
elif [[ -x "$GEXTOPENCV_INSTALLED/linux/bin/opencv_createsamples" ]]; then
  echo "found opencv in openCV/linux/bin"
  _OPENCV="$GEXTOPENCV_INSTALLED/linux/bin/opencv_createsamples"
fi

if [ $force_config = "1" ] ; then
  echo "[pushd $package$version]"
  ( cd $package$version ; \
    if [ ! -d release ] ; then \
      echo "Creating 'release' directory" ; \
      mkdir release ; \
    fi ; \
    cd release ; \

    if [ ! -f Makefile ] ; then \
      cmake -D CMAKE_BUILD_TYPE=RELEASE \
            -D CMAKE_INSTALL_PREFIX=$prefix ..;
    fi ; \
    PKG_CONFIG_PATH=$prefix/lib/pkgconfig:${PKG_CONFIG_PATH}; \
    export PKG_CONFIG_PATH; \
   )
  if [ $? != 0 ] ; then
      echo "      Error encountered running *configure* stage of $progname"
      exit 1
  fi
  echo "[popd]"
  opt_run_cmake $compile $package $version "release"
  opt_run_cmake $install $package $version "release" "install"
else
  if [ $auto_config = "1" ] ; then
    echo "Found top-level for ${progname%.*} => no need to run configure/initialize CMake"
  fi
fi

opt_run_cmake $clean $package $version "release" "clean"
opt_run_cmake $distclean $package $version "release" "distclean"

opt_run_tarclean $tarclean $package $version

