#!/bin/sh

TEXMACS_MAPLE_BIN=`which maple`
TEXMACS_MAPLE_BIN=`realpath "$TEXMACS_MAPLE_BIN"`
#if [ $? -ne 0 ]
#then
#    TEXMACS_MAPLE_BIN=`which maple`
#    TEXMACS_MAPLE_BIN=`realpath.py "$TEXMACS_MAPLE_BIN"`
#fi
if [ $? -ne 0 ]
then
    TEXMACS_MAPLE_BIN=`which maple`
fi
#TEXMACS_MAPLE_DIR=`echo -n "$TEXMACS_MAPLE_BIN" | sed -e 's%/bin/maple$%%'`
TEXMACS_MAPLE_DIR=`echo "$TEXMACS_MAPLE_BIN" | sed -e 's%/bin/maple$%%'`
export TEXMACS_MAPLE_BIN
export TEXMACS_MAPLE_DIR

MAPLE_CPPFLAGS="-I$TEXMACS_MAPLE_DIR/extern/include"
MAPLE_SYS_BIN=""
if test -e "$TEXMACS_MAPLE_DIR/bin/maple.system.type"; then
  MAPLE_SYS_BIN=$("$TEXMACS_MAPLE_DIR/bin/maple.system.type")
fi
MAPLE_LDFLAGS="-L$TEXMACS_MAPLE_DIR/$MAPLE_SYS_BIN -lmaplec"
if [ "$MAPLE_SYS_BIN" -eq "bin.IBM_INTEL_LINUX" ]; then
  MAPLE_LDFLAGS="-L$TEXMACS_MAPLE_DIR/$MAPLE_SYS_BIN -Wl,-rpath,$(TEXMACS_MAPLE_DIR)/$MAPLE_SYS_BIN -lmaplec"
fi
export MAPLE_CPPFLAGS
export MAPLE_LDFLAGS

if [ -d "$TEXMACS_MAPLE_DIR/extern" ]
then
    if [ -f "$TEXMACS_HOME_PATH/bin/tm_maple_9.sh" ]
    then
        DUMMY="dummy"
    else
        cd $TEXMACS_PATH/plugins/maple; make -f Makefile.9 TEXMACS_HOME_PATH="$TEXMACS_HOME_PATH"
    fi
    exec "${HOME}/.TeXmacs/bin/tm_maple_9.sh"
elif [ -f "$TEXMACS_MAPLE_DIR/mwshelp" ]
then
    exec tm_maple_5
fi
