#!/bin/bash
#==========================================================================
#==========================================================================
#
#  The Sick LIDAR Matlab/C++ Toolbox
#
#  File: build_mex
#  Auth: Jason Derenick at Lehigh University
#  Cont: derenick(at)lehigh(dot)edu
#  Date: 10 March 2008
#
#  In:   BUILD_TYPE: 'ld' or 'lms' or 'both' (default: 'both') or 'clean'
#
#  Desc:
#        This script builds the mex interface for the Sick Toolbox.
#        If no arguments are supplied then it builds both the ld &
#        lms interfaces.
#
#        Us: build_mex [BUILD_TYPE=both]
#        Ex: build_mex
#        Ex: build_mex lms
#        Ex: build_mex clean
#
#==========================================================================

# Define the temp directories
BUILD_DIR=build;
CONF_DIR=.sickconf;

# Define LMS and LD src dir names
SICK_LD_SRC_DIR=sickld-1.0
SICK_LMS_SRC_DIR=sicklms-1.0

# Default build type
BUILD_TYPE='both';

# Check what to build
if [ -n "$1" ]; then

  # Do they want help?
  if [ $1 = "--help" ]; then  
    echo "usage: $0 [BUILD_TYPE=both]"
    echo "example: $0 lms"
    exit;
  fi

  # Assign the build type
  BUILD_TYPE=$1;

fi

echo
echo "The Sick LIDAR Matlab/C++ Toolbox"
echo "Build Script (Version 1.0)"
echo

# Make sure its a valid option
if [ $BUILD_TYPE = "--help" -o ! \
     $BUILD_TYPE = "ld" -a ! \
     $BUILD_TYPE = "lms" -a ! \
     $BUILD_TYPE = "both" -a ! \
     $BUILD_TYPE = "clean" ]; then

  echo "usage: bash $0 [BUILD_TYPE]"
  echo "example: bash $0 lms"

  exit;

fi

# Check if its a clean build
if [ $BUILD_TYPE = "clean" ]; then

  echo "Cleaning up..."
  if [ -e ./$BUILD_DIR ]; then
    rm -r ./$BUILD_DIR;
  fi
  echo "       Done cleaning!"

  echo
  echo "*** To BUILD re-run build_mex :o) ***";
  echo 

  exit;

fi

# Read in Matlab bin directory
BIN_DIR=`cat $CONF_DIR/.matbin 2>/dev/null`;
if [ ! $? -eq 0 ]; then

  echo "$0: Couldn't find config file! Please run config_mex."

  # If it exists then kill it
  if [ -e $CONF_DIR ]; then
    rm -r $CONF_DIR;
  fi

  exit;

fi

if [ -z $BIN_DIR ]; then

  echo "$0: Empty config file! Please (re)run config_mex."

  # If it exists then kill it
  if [ -e $CONF_DIR ]; then
    rm -r $CONF_DIR;
  fi

  exit;

fi

# Check whether to make temp build dir
if [ ! -e ./$BUILD_DIR ]; then

  mkdir ./$BUILD_DIR 2>/dev/null;

  if [ ! $? -eq 0 ]; then
    echo "$0: Couldn't make temp directory! Permissions problem?"
    exit;
  fi

fi

if [ $BUILD_TYPE = "both" -o $BUILD_TYPE = "ld" ]; then

  # Build the mex and place in temp dir
  echo "Building Sick LD mex..."

  $BIN_DIR/mex -g -output ./$BUILD_DIR/sickld \
                          interfaces/ld/ldmex.cc \
                          ../c++/drivers/ld/$SICK_LD_SRC_DIR/SickLDMessage.cc \
                          ../c++/drivers/ld/$SICK_LD_SRC_DIR/SickLDBufferMonitor.cc \
                          ../c++/drivers/ld/$SICK_LD_SRC_DIR/SickLD.cc \
                          -I../c++/drivers/ld/$SICK_LD_SRC_DIR \
                          -I../c++/drivers/base/src

  if [ ! $? -eq 0 ]; then
    echo "$0: Build failed!"
    exit;
  fi

  echo "       Build complete!"

  cp ./interfaces/ld/sickld.m ./$BUILD_DIR;

  echo

fi

if [ $BUILD_TYPE = "both" -o $BUILD_TYPE = "lms" ]; then

  echo "Building Sick LMS mex..."

  $BIN_DIR/mex -g -output ./$BUILD_DIR/sicklms \
                          interfaces/lms/lmsmex.cc \
                          ../c++/drivers/lms/$SICK_LMS_SRC_DIR/SickLMSMessage.cc \
                          ../c++/drivers/lms/$SICK_LMS_SRC_DIR/SickLMSBufferMonitor.cc \
                          ../c++/drivers/lms/$SICK_LMS_SRC_DIR/SickLMS.cc \
                          -I../c++/drivers/lms/$SICK_LMS_SRC_DIR/ \
                          -I../c++/drivers/base/src

  if [ ! $? -eq 0 ]; then
    echo "$0: Build failed!"
    exit;
  fi

  echo "       Build complete!"

  cp ./interfaces/lms/sicklms.m ./$BUILD_DIR;

  echo

fi

# Copy supporting file to temp dir
echo "Copying toolbox files..."
cp ./misc/*.xml ./$BUILD_DIR;
cp ./misc/*.gif ./$BUILD_DIR;
echo "       Files copied!"

echo

# Adjust permissions
echo "Adjusting permissions..."
chmod -R 755 ./$BUILD_DIR;
echo "       Permissions set!"

echo
echo "*** To INSTALL run install_mex :o) ***";
echo
