#!/bin/sh
set -o xtrace   # Write all commands first to stderr
set -o errexit  # Exit the script with error if any of the commands fail

# Supported/used environment variables:
#   CMAKE                      Path to cmake executable.
#   BSON_ONLY                  Whether to build only the BSON library.


DIR=$(dirname $0)
. $DIR/find-cmake.sh
. $DIR/check-symlink.sh

if command -v gtar 2>/dev/null; then
  TAR=gtar
else
  TAR=tar
fi

if [ "$BSON_ONLY" ]; then
  BUILD_DIR=$(pwd)/build-dir-bson
  INSTALL_PREFIX=$(pwd)/install-dir-bson
else
  BUILD_DIR=$(pwd)/build-dir-mongoc
  INSTALL_PREFIX=$(pwd)/install-dir-mongoc
fi

if [ "$DESTDIR" ]; then
   INSTALL_DIR="${DESTDIR}/${INSTALL_PREFIX}"
else
   INSTALL_DIR=$INSTALL_PREFIX
fi

rm -rf $BUILD_DIR
mkdir $BUILD_DIR

rm -rf $INSTALL_DIR
mkdir -p $INSTALL_DIR

cd $BUILD_DIR
$TAR xf ../../mongoc.tar.gz -C . --strip-components=1

if [ "$BSON_ONLY" ]; then
  BSON_ONLY_OPTION="-DENABLE_MONGOC=OFF"
else
  BSON_ONLY_OPTION="-DENABLE_MONGOC=ON"
fi


$CMAKE -DCMAKE_INSTALL_PREFIX=$INSTALL_PREFIX -DCMAKE_PREFIX_PATH=$INSTALL_DIR/lib/cmake $BSON_ONLY_OPTION .
make
if [ "$DESTDIR" ]; then
   make DESTDIR=$DESTDIR install
else
   make install
fi
touch $INSTALL_DIR/lib/canary.txt

ls -l $INSTALL_DIR/share/mongo-c-driver

make uninstall

set +o xtrace

if test -f $INSTALL_DIR/lib/pkgconfig/libbson-1.0.pc; then
  echo "libbson-1.0.pc found!"
  exit 1
else
  echo "libbson-1.0.pc check ok"
fi
if test -f $INSTALL_DIR/lib/cmake/libbson-1.0/libbson-1.0-config.cmake; then
  echo "libbson-1.0-config.cmake found!"
  exit 1
else
  echo "libbson-1.0-config.cmake check ok"
fi
if test -f $INSTALL_DIR/lib/cmake/libbson-1.0/libbson-1.0-config-version.cmake; then
  echo "libbson-1.0-config-version.cmake found!"
  exit 1
else
  echo "libbson-1.0-config-version.cmake check ok"
fi
if test ! -f $INSTALL_DIR/lib/canary.txt; then
  echo "canary.txt not found!"
  exit 1
else
  echo "canary.txt check ok"
fi
if test ! -d $INSTALL_DIR/lib; then
  echo "$INSTALL_DIR/lib not found!"
  exit 1
else
  echo "$INSTALL_DIR/lib check ok"
fi
if [ -z "$BSON_ONLY" ]; then
  if test -f $INSTALL_DIR/lib/pkgconfig/libmongoc-1.0.pc; then
    echo "libmongoc-1.0.pc found!"
    exit 1
  else
    echo "libmongoc-1.0.pc check ok"
  fi
  if test -f $INSTALL_DIR/lib/cmake/libmongoc-1.0/libmongoc-1.0-config.cmake; then
    echo "libmongoc-1.0-config.cmake found!"
    exit 1
  else
    echo "libmongoc-1.0-config.cmake check ok"
  fi
  if test -f $INSTALL_DIR/lib/cmake/libmongoc-1.0/libmongoc-1.0-config-version.cmake; then
    echo "libmongoc-1.0-config-version.cmake found!"
    exit 1
  else
    echo "libmongoc-1.0-config-version.cmake check ok"
  fi
fi
if test -f $INSTALL_DIR/include/libbson-1.0/bson/bson.h; then
  echo "bson/bson.h found!"
  exit 1
else
  echo "bson/bson.h check ok"
fi
if test -f $INSTALL_DIR/include/libbson-1.0/bson.h; then
  echo "bson.h found!"
  exit 1
else
  echo "bson.h check ok"
fi
if test -d $INSTALL_DIR/include/libbson-1.0; then
  echo "$INSTALL_DIR/include/libbson-1.0 found!"
  exit 1
else
  echo "$INSTALL_DIR/include/libbson-1.0 check ok"
fi
if [ -z "$BSON_ONLY" ]; then
  if test -f $INSTALL_DIR/include/libmongoc-1.0/mongoc/mongoc.h; then
    echo "mongoc/mongoc.h found!"
    exit 1
  else
    echo "mongoc/mongoc.h check ok"
  fi
  if test -f $INSTALL_DIR/include/libmongoc-1.0/mongoc.h; then
    echo "mongoc.h found!"
    exit 1
  else
    echo "mongoc.h check ok"
  fi
  if test -d $INSTALL_DIR/include/libmongoc-1.0; then
    echo "$INSTALL_DIR/include/libmongoc-1.0 found!"
    exit 1
  else
    echo "$INSTALL_DIR/include/libmongoc-1.0 check ok"
  fi
fi
if test -f $INSTALL_DIR/share/mongo-c-driver/uninstall-bson.sh; then
  echo "uninstall-bson.sh found!"
  exit 1
else
  echo "uninstall-bson.sh check ok"
fi
if test -f $INSTALL_DIR/share/mongo-c-driver/uninstall.sh; then
  echo "uninstall.sh found!"
  exit 1
else
  echo "uninstall.sh check ok"
fi
if test -d $INSTALL_DIR/share/mongo-c-driver; then
  echo "$INSTALL_DIR/share/mongo-c-driver found!"
  exit 1
else
  echo "$INSTALL_DIR/share/mongo-c-driver check ok"
fi

