#!/bin/sh

echo "Configuring for a UNIX-alike platform (sub-architecture: \"$R_ARCH\")";

# Run the configure_script.R
if [ x"$R_HOME" != x ]; then
  $("$R_HOME/bin/Rscript" ./configure_script.R) 
fi;

# The location the Makevars will look for definitions
if [ x"$R_ARCH" != x ]; then
  mkdir -p ./src/setup$R_ARCH;
fi;

LOCALSYS_PATH=./src/setup$R_ARCH/Localsys.txt;
touch $LOCALSYS_PATH;

# Definitions should neither be empty nor default value 
ISDEF_PKG_MOSEKHOME='( [ x"$PKG_MOSEKHOME" != x ] && [ x"$PKG_MOSEKHOME" != x"[MOSEK_HOME_PATH]" ] )';
ISDEF_PKG_MOSEKLIB='( [ x"$PKG_MOSEKLIB" != x ] && [ x"$PKG_MOSEKLIB" != x"[MOSEK_LIB_FILE]" ] )';
newlineCR=$(printf "\r");


#
# ATTEMPT 1: READ CONFIGURATION FROM ENVIRONMENT
#
PKG_MOSEKHOME=$PKG_MOSEKHOME;
PKG_MOSEKLIB=$PKG_MOSEKLIB;

if (eval $ISDEF_PKG_MOSEKHOME) || (eval $ISDEF_PKG_MOSEKLIB); then
  echo "Found variable definitions in environment";
fi;


#
# ATTEMPT 2: READ CONFIGURATION FROM LOCALSYS FILE
#
if (eval ! $ISDEF_PKG_MOSEKHOME) && (eval ! $ISDEF_PKG_MOSEKLIB); then

  # (1) Go through all lines,
  # (2) From those that start with PKG_MOSEKHOME=
  # (3) extract the RHS
  # (4) remove the carriage returns
  # (5) remove trailing spaces

  PKG_MOSEKHOME=$(cat $LOCALSYS_PATH | grep "^PKG_MOSEKHOME" | sed 's/^PKG_MOSEKHOME=\(.*\)/\1/g' | sed 's/'$newlineCR'//g' | sed 's/[ ]*$//g' | sed 's/\"//g');
  PKG_MOSEKLIB=$(cat $LOCALSYS_PATH | grep "^PKG_MOSEKLIB" | sed 's/^PKG_MOSEKLIB=\(.*\)/\1/g' | sed 's/'$newlineCR'//g' | sed 's/[ ]*$//g' | sed 's/\"//g');

  if (eval $ISDEF_PKG_MOSEKHOME) || (eval $ISDEF_PKG_MOSEKLIB); then
    echo "Found variable definitions in $LOCALSYS_PATH file";
  fi;
fi;


#
# ATTEMPT 3: GUESS CONFIGURATION FROM "mosek" command
#
if (eval ! $ISDEF_PKG_MOSEKHOME) && (eval ! $ISDEF_PKG_MOSEKLIB); then

  # (1) Find the full path of command 'mosek'
  # (2) Resolve symbolic links if any
  # (3) Remove the latter part 'bin/mosek*'
  PKG_MOSEKEXE=$(which mosek);
  if [ -L $PKG_MOSEKEXE ]; then
    PKG_MOSEKHOME=$(readlink $PKG_MOSEKEXE | sed 's/bin\/mosek*$//');
  else
    PKG_MOSEKHOME=$(echo $PKG_MOSEKEXE | sed 's/bin\/mosek*$//');
  fi;

  if (eval $ISDEF_PKG_MOSEKHOME); then

    # (1) Go through all mosek library files
    # (2) From those whos name does not have additional specifiers (only integers/underscores allowed)
    #     and whos filetype is either .so (uname="Linux") or .dylib (uname="Darwin").
    # (3) Extract the integers/underscores if any, and add prefix 'mosek'
    PKG_MOSEKLIB=$(find $PKG_MOSEKHOME/bin -name 'libmosek*.*' | grep '\/libmosek\([0-9,_]*\).\([so,dylib]*\)*$' | sed 's/\(.*\)libmosek\([0-9,_]*\).\([so,dylib]*\)/mosek\2/g');

  fi;

  if (eval $ISDEF_PKG_MOSEKHOME) || (eval $ISDEF_PKG_MOSEKLIB); then
    echo "Guessed variable definitions from shell command 'mosek'";
  fi;
fi;


#
# EXIT IF A CONFIGURATION HAVE NOT BEEN READ
#
if (eval ! $ISDEF_PKG_MOSEKHOME) && (eval ! $ISDEF_PKG_MOSEKLIB); then
  echo "*** No variable 'PKG_MOSEKHOME' in environment - see configure-vars (configure.vars) argument ***";
  echo "*** No variable 'PKG_MOSEKHOME' in $LOCALSYS_PATH file ***";
  echo "*** Shell command 'mosek' was not recognized - please validate your installation of MOSEK ***";
  echo "***   >> PATH = $PATH  ***";
  exit 1;
fi;

if (eval ! $ISDEF_PKG_MOSEKHOME); then
  echo "*** Variable 'PKG_MOSEKHOME' was not found here ***";
  exit 1;
fi;

if (eval ! $ISDEF_PKG_MOSEKLIB); then
  echo "*** Variable 'PKG_MOSEKLIB' was not found here ***";
  echo "'PKG_MOSEKHOME':" $PKG_MOSEKHOME;
  exit 1;
fi;


#
# FORMAT INPUT CORRECTLY AND VERIFY
#

# Resolve symbolic links if any
if [ -L $PKG_MOSEKHOME ]; then
  PKG_MOSEKHOME=$(readlink $PKG_MOSEKHOME);
fi;

# Trim away traling slashes from path
REMOVESLASHES=${PKG_MOSEKHOME##*[!/]};
PKG_MOSEKHOME=${PKG_MOSEKHOME%$REMOVESLASHES};

if [ ! -d $PKG_MOSEKHOME ]; then
  echo "*** Variable 'PKG_MOSEKHOME' is not a directory: $PKG_MOSEKHOME ***"
  exit 1
fi;


#
# PERFORM CONFIGURATION
#
echo "Using MOSEK home directory:" $PKG_MOSEKHOME;
echo "Using MOSEK library:" $PKG_MOSEKLIB;

echo "
PKG_MOSEKHOME:=$PKG_MOSEKHOME
PKG_MOSEKLIB:=$PKG_MOSEKLIB
" > $LOCALSYS_PATH;

echo "Configuration done."
