#!/bin/sh
## @file configure
##
## PANDORE
##
## @author  Jalal Fadili  - 2007-12-13
## based on Regis Clouard - 2005-09-27
##
## GREYC - IMAGE
## 6, boulevard Marechal Juin 14050 CAEN.
## email : Jalal.Fadili@greyc.ensicaen.fr





#
# If needed configure the following parameters
#


# 1. C++ compiler and linker additional flags
CXX=${CXX-g++}
CFLAGS="-O2 --fast-math"
LDFLAGS=
MEX=${MEX-mex}
MEXFLAGS=""




# 2. OS commands.
MAKE='make'
RM='rm -f'
AR='ar'
ARFLAGS='lrcv'
RANLIB='ranlib'
LN_S='ln -sf'

















##
## No changes need to be made after these lines
## DO NOT EDIT BY HAND
##

boxname=`pwd`
boxname=`basename $boxname`

bindir='./bin'
libdir='./lib'


##
## 1. ARGS
## Reading configure arguments
##
while :
do
    case $# in
    0)
	break
	;;
    esac

    option=$1
    shift

    case $option in
    --*=*)
	optarg=`echo $option | sed -e 's/^[^=]*=//'`
	arguments="$arguments $option"
	;;
    esac
    case $option in
    --mex-pathname*)
	MEX=$optarg/mex
    ;;
    -help | --help | --hel | --he)
    # Omit some internal or obsolete options to make the list less imposing.
    # This message is too long to be a string in the A/UX 3.1 sh.
    cat << EOF
Usage: configure [options]
Options: [defaults in brackets after descriptions]
Configuration:
  --help                       print this message
Matlab mex compiler:
  --mex-pathname=DIR           use DIR as the mex path.
EOF
    exit 0;
    ;;

    *)
    echo "${option}: invalid command-line switch"
    exit 1
    ;;
    esac
done


##
## 2. ECHO
##

# Find flavor for echoing a string without a NEWLINE
# (ew_c: echo with \c, ew_n: echo with -n, ew_t: tabulation)
if (echo "example\c"; echo 1,2,3) | grep c >/dev/null; then 
  if (echo -n example; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then 
    ew_n= ew_c=' 
' ew_t='        ' 
  else
    ew_n=-n ew_c= ew_t=
  fi 
else 
  ew_n= ew_c='\c' ew_t=
fi

##
## 3. OS
##

# Try to figure the system we have
echo $ew_n "checking target system type""... $ew_c"
# determine the kind of the machine which is running this script
os=`uname -s`
version=`uname -r`
machine=`uname -m`

MACHINE=$os-$version-$machine

case $MACHINE in
  SunOS-4*)  OS=SUNOS4;;
  SunOS-5*)  OS=SUNOS5;;
  CYGWIN*|MSYS*|MINGW*) OS=WIN32;;
  Linux*)    OS=LINUX_ELF;;
  Darwin*)   OS=MACOSX;;

  OSF1*)     OS=OSF1;;
  ULTRIX*)   OS=ULTRIX;;
  HP*)	     OS=HPUX; DFLGS="$DFLGS -Dhpux";;
  NetBSD-1*) OS=NETBSD1;;
  FreeBSD*)  OS=FREEBSD;;
  IRIX-5*)   OS=IRIX5;;
  IRIX*-6*)  OS=IRIX5;;
  *)         { echo "configure: error: this operating system is not supported" 1>&2; exit 1; } ;;
esac
echo "$ew_t""$MACHINE"

##
## 4. CXX
##

# Configure the dynamic loading options for CXX
OBJLIB="OBJ=";
OBJ_FLAGS="";
SHARED_LIB_FLAGS="";
LIB_FLAGS="";
LD="";
LIB_NAME="pandore";
LIB_PREFIX="lib";
DYN_LIB_SUFFIX="";
STATIC_LIB_SUFFIX="";
STKLDFLAGS=""
LIBPATH=LD_LIBRARY_PATH

echo $ew_n "checking dynamic loading options for $OS""... $ew_c"

case $OS in 
  SUNOS4)
     OBJ_FLAGS="-fpic"
     CFLAGS="$CFLAGS -s"
     SHARED_LIB_FLAGS="-assert pure-text"
     LD="$CXX"
     STATIC_LIB_SUFFIX='a'
     DYN_LIB_SUFFIX='so' ;;
   SUNOS5)
     OBJ_FLAGS="-fpic"
     CFLAGS="$CFLAGS -D__C99FEATURES__ -s "
     SHARED_LIB_FLAGS='-G -z nodefs'
     STATIC_LIB_SUFFIX='a'
     DYN_LIB_SUFFIX='so';;
   LINUX_ELF)
     OBJ_FLAGS='-fpic'
     CFLAGS="$CFLAGS -s"
     SHARED_LIB_FLAGS='-shared'
     LD="$CXX"
     STATIC_LIB_SUFFIX='a'
     DYN_LIB_SUFFIX='so'
     # Under ELF, use the -rdynamic option at ld phase
     STKLDFLAGS=-rdynamic;;
   WIN32)
     OBJ_FLAGS=''
     LD="$CXX"
     SHARED_LIB_FLAGS='-shared -Wl,--enable-auto-import'
     LIBPATH=PATH
     LIB_FLAGS='-Wl,--enable-auto-import'
     LIB_PREFIX=""
     STATIC_LIB_SUFFIX='lib'
     DYN_LIB_SUFFIX='dll' ;;
   MACOSX)
    case "$MACHINE" in
      darwin1.[[012]]*)
        OBJ_FLAGS="-undefined suppress" ;;
      *)
	OBJ_FLAGS="-fno-common -flat_namespace -undefined suppress" ;;
    esac
     SHARED_LIB_FLAGS="-dynamiclib -dynamic -fno-common -flat_namespace -undefined suppress"
     LIBPATH=DYLD_LIBRARY_PATH
     LD="$CXX"
     STATIC_LIB_SUFFIX='a'
     DYN_LIB_SUFFIX="dylib" ;;
   OSF1)
     OBJ_FLAGS="-fpic"
     SHARED_LIB_FLAGS="-shared"
     LD="$CXX"
     LIBPATH=LD_LIBRARY_PATH
     STATIC_LIB_SUFFIX='a'
     DYN_LIB_SUFFIX='so' ;;
   NETBSD1) 
     OBJ_FLAGS="-fpic"
     SHARED_LIB_FLAGS="-Bshareable"
     LD="$CXX"
     LIBPATH=LD_LIBRARY_PATH
     STATIC_LIB_SUFFIX='a'
     DYN_LIB_SUFFIX='so';;
   FREEBSD)
     OBJ_FLAGS="-fpic"
     SHARED_LIB_FLAGS="-shared"
     LD="$CXX"
     STKLDFLAGS=-rdynamic
     LIBPATH=LD_LIBRARY_PATH
     STATIC_LIB_SUFFIX='a'
     DYN_LIB_SUFFIX='so';;
   IRIX5)
     if test "$CXX"="g++"
     then 
       OBJ_FLAGS="-fpic"
     else
       OBJ_FLAGS="-KPIC"
     fi
     SHARED_LIB_FLAGS="-shared"
     LD="$CXX"
     LIBPATH=LD_LIBRARY_PATH
     STATIC_LIB_SUFFIX='a'
     DYN_LIB_SUFFIX='so' ;;
   *) { echo "configure: error: don't know how to build shared libraries" 1>&2; exit 1; } ;;
esac

echo "$ew_t""yes"

# Check CXX facilities
echo $ew_n "checking C++ compiler""... $ew_c"
cat > tmp.cpp <<EOF
template<typename T> struct foo {
template<typename U> static bool bar(U u) { return true; }
};
int main() { return foo<int>::bar(12); }
EOF

if $CXX -c -o tmp.o tmp.cpp 2>/dev/null
then
    echo "$ew_t""$CXX"
else
    echo "$ew_t""no."
    echo "*** Error: MCALabC++ won't compile, please set CXX variable with the C++ compiler path (export CXX=<path>) before executing configure." 1>&2; exit 1;
fi

echo $ew_n "checking linker""... $ew_c"
if $LD -o tmp.exe tmp.o 2>/dev/null
then
    echo "$ew_t""$LD"
else
    echo "$ew_t""no."
   echo "*** Error: MCALabC++ won't compile, please set LD variable with the linker path (export LD=<path>) before executing configure." 1>&2; exit 1;
fi

$RM tmp.o tmp.exe
echo $ew_n "checking $MAKE""... $ew_c"
cat > tmp.mak <<EOF
tmp.exe:  tmp.cpp
	$CXX -o tmp.exe tmp.cpp 2>/dev/null
EOF

if $MAKE -f tmp.mak 2>/dev/null 1>&2
then
    echo "$ew_t""$MAKE"
else
    echo "$ew_t""no."
    echo "*** Error: MCALabC++ won't compile, please set MAKE variable with the make path (export MAKE=<path>) before executing configure." 1>&2; exit 1;
fi

$RM tmp.cpp tmp.mak tmp.exe

## set compiler options.
CXXFLAGS="$CFLAGS -Isrc/utils -Iinclude"
CXXFLAGSOP=" -Isrc -DMAIN" 
MEXFLAGS="$MEXFLAGS -Isrc/utils -Iinclude -Isrc -Imatlab -DMAIN"

DYN_LIB_NAME=${LIB_PREFIX}${LIB_NAME}.${DYN_LIB_SUFFIX}
STATIC_LIB_NAME=${LIB_PREFIX}${LIB_NAME}.${STATIC_LIB_SUFFIX}
LDFLAGSLIB="$SHARED_LIB_FLAGS"
LDFLAGSOP="-L$libdir -lpandore $LIB_FLAGS"

##
## 5. OPERS and OBJECTS
##
TARGETS="TARGETS="
MEXTARGETS="MEXTARGETS="
CLEAN="clean:
	-\$(RM) \$(OBJ)"
DISTCLEAN="distclean: clean
	\$(RM) \$(TARGETS)
	-\$(RM) -r $bindir
	-\$(RM) \$(MEXTARGETS)
	-\$(RM) \$(DYN_LIB)
	-\$(RM) \$(STATIC_LIB)
	-\$(RM) -r $libdir
	-\$(RM) \$(OBJ)"
DEPENDENCIES=

echo $ew_n "generating Makefile""... $ew_c"
echo ""

echo $ew_n "generating dependencies for objects files... ""$ew_c"
for i in src/objects/*.cpp 
do
    if [ -r $i ]
    then
	OBJLIB=$OBJLIB" \\
	    src/objects/`basename $i .cpp`.o"
    fi
done
echo "... done."

echo $ew_n "generating dependencies for operators files... ""$ew_c"
for i in src/*.cpp
do
    if [ -r $i ]
    then
	TARGETS=$TARGETS" \\
	    ${bindir}/`basename $i .cpp`"
    fi
done

for i in src/*.cc
do
    if [ -r $i ]
    then
    TARGETS=$TARGETS" \\
	${bindir}/`basename $i .cc`"
    fi
done
echo "... done."

echo $ew_n "generating dependencies for mex files... ""$ew_c"
for i in matlab/*_mex.cpp
do
    if [ -r $i ]
    then
	MEXTARGETS=$MEXTARGETS" \\
	    matlab/`basename $i .cpp`"
    fi
done

for i in matlab/*_mex.cc
do
    if [ -r $i ]
    then
	MEXTARGETS=$MEXTARGETS" \\
	    matlab/`basename $i .cpp`"
    fi
done
echo "... done."


##
## 7. Makefile
##
cat > Makefile <<EOF
## Makefile MCALabC++
##  
##-----------------------------------------------------------
## DO NOT EDIT BY HAND
## Autogenerated by ./configure.
## If something is wrong, fix ./configure and run it again. 
##-----------------------------------------------------------
##
## GREYC - IMAGE
## 6, boulevard Maréchal Juin 14050 CAEN.
## email : Jalal.Fadili@greyc.ensicaen.fr
##

RM          = $RM
AR          = $AR 
ARFLAGS     = $ARFLAGS
CXX         = $CXX
CXXFLAGS    = $CXXFLAGS
CXXFLAGSOP  = \$(CXXFLAGS) $CXXFLAGSOP
MEX         = $MEX
MEXFLAGS    = $MEXFLAGS
LD          = $LD
LDFLAGSLIB  = $LDFLAGS $LDFLAGSLIB
LDFLAGSOP   = $LDFLAGS $LDFLAGSOP
PPP         = $PPP
MAKE        = $MAKE
DYN_LIB	    = $libdir/$DYN_LIB_NAME
STATIC_LIB  = $libdir/$STATIC_LIB_NAME

$OBJLIB

$TARGETS

$MEXTARGETS
EOF

cat >> Makefile <<EOF

# Avoid removing intermediates files.
.PRECIOUS: %.cc

.IGNORE:

.PHONY: all clean cleandist distclean opers

all:  bin lib opers matlab

bin:
	mkdir -p $libdir $bindir

opers: \$(TARGETS)

matlab: \$(MEXTARGETS)

FORCE:

EOF

#Cleaning facilities.
cat >> Makefile <<EOF
$CLEAN

cleandist: distclean

$DISTCLEAN

EOF

echo "lib: \$(DYN_LIB) \$(STATIC_LIB)" >> Makefile
cat >> Makefile <<EOF

\$(DYN_LIB): \$(OBJ)
	\$(RM) \$@
	\$(LD) -o \$@ \$(LDFLAGSLIB) \$(OBJ)
	chmod 755 \$@

\$(STATIC_LIB): \$(OBJ)
	\$(RM) \$@
	\$(AR) \$(ARFLAGS) \$@ \$(OBJ)
	chmod 644 \$@
	
EOF

echo "$DEPENDENCIES" >> Makefile
echo 'done.'

cat >> Makefile <<EOF
#
# dependencies :
#

${bindir}/%: src/%.cc
	\$(CXX) \$(CXXFLAGSOP) -o \$@ \$< \$(LDFLAGSOP)

${bindir}/%: src/%.cpp
	\$(CXX) \$(CXXFLAGSOP) -o \$@ \$< \$(LDFLAGSOP)

./matlab/%: matlab/%.cpp
	\$(MEX) -outdir matlab \$< \$(MEXFLAGS) \$(STATIC_LIB)
	
./matlab/%: matlab/%.cpp
	\$(MEX) -outdir matlab \$< \$(MEXFLAGS) \$(STATIC_LIB)
EOF
