#!/bin/bash
#==========================================================================
#==========================================================================
#
#  The Sick LIDAR Matlab/C++ Toolbox
#
#  File: install_mex
#  Auth: Jason Derenick at Lehigh University
#  Cont: derenick(at)lehigh(dot)edu
#  Date: 10 March 2008
#
#  Desc:
#        This script installsthe mex interface for the Sick Toolbox.
#
#        Usage:   install_mex
#        Example: install_mex
#
#==========================================================================

# Define the temporary dir
BUILD_DIR=build;
CONF_DIR=.sickconf;

if [ -n "$1" ]; then

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

fi

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

# Read in Matlab toolbox directory
TOOLBOX_DIR=`cat $CONF_DIR/.matbox 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 $TOOLBOX_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 temp dir exists
if [ ! -e ./$BUILD_DIR ]; then
  echo "$0: No build directory found! Please run build_mex first."
  exit;
fi

# Make the installation directory
INSTALL_DIR=${TOOLBOX_DIR}/sick;

if [ -e $INSTALL_DIR ]; then

  # Prompt user to overwrite the directory
  echo "WARNING: Target installation directory exists!"
  echo "Should I still install files to this directory? [Y/N]:"
  read -e INSTALL_FILES;

  # Should we bail on the installation?
  if [ $INSTALL_FILES = 'N' -o $INSTALL_FILES = 'n' ]; then
    echo "Quitting installation :o("
    exit;
  fi

  # Finish the installation!
  if [ $INSTALL_FILES = 'Y' -o $INSTALL_FILES = 'y' ]; then

    rm -r "$INSTALL_DIR";
    if [ ! $? -eq 0 ]; then
      echo "$0: Failed to remove old directory! Possibly a permissions problem."
      exit;
    fi

  else
    echo "Invalid reply! Quitting Installation :o("
    exit;
  fi

  echo

fi

# Move files into the target directory
echo "Installing files to target directory..."

mv ./$BUILD_DIR $INSTALL_DIR 2>/dev/null;
if [ ! $? -eq 0 ]; then
  echo "$0: Failed to install files! Possibly a permissions problem."
  exit;
fi

echo "       Files installed!"
echo

# Check if path already exists
SEARCH=`grep '/toolbox/sick:' $TOOLBOX_DIR/local/pathdef.m`;
if [ ! -n "$SEARCH" ]; then

  # Backup pathdef.m
  echo "Backing up pathdef.m..."
  DATE_SECS=`date +%s`;
  if [ ! $? -eq 0 ]; then
    echo "$0: Failed to backup pathdef.m (date failed)!"
    exit;
  fi
  
  cat $TOOLBOX_DIR/local/pathdef.m > $TOOLBOX_DIR/local/pathdef.m_bu_$DATE_SECS;
  if [ ! $? -eq 0 ]; then
    echo "$0: Failed to backup pathdef.m (write failed)!"
    exit;
  fi

  echo "       Backup complete!"

  echo

  echo "Updating pathdef.m..."

  # Update pathdef.m
  sed -f ./.sed/pathdef.sed $TOOLBOX_DIR/local/pathdef.m > $TOOLBOX_DIR/local/pathdef.tmp 2>/dev/null;
  if [ ! $? -eq 0 ]; then
    echo "$0: Failed to update pathdef.m (sed failed)!"
    exit;
  fi

  # Make sure file was copied over
  mv $TOOLBOX_DIR/local/pathdef.tmp $TOOLBOX_DIR/local/pathdef.m 2>/dev/null;
  if [ ! $? -eq 0 ]; then
    echo "$0: Failed to update pathdef.m (mv failed)!"
    exit;
  fi

  echo "       pathdef.m updated!";
  echo

fi

echo "*** Installation successful! ***"
echo
echo "Thanks for installing the Sick LIDAR Matlab/C++ Toolbox!"
echo "Bye Bye :o)"
