#!/bin/sh
# Create a loadable object from a C++ function defined in a .cpp file
# $Id: load.link.in,v 1.3 2009/01/20 19:56:57 hecht Exp $
FFVERSION='3.0-5'
FFCXXFLAGS='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -DNDEBUG -DDRAWING -DBAMG_LONG_LONG  -DNCHECKPTR -rdynamic  '
INCFF=
if [ -z "$CXX" ] ; then CXX='g++' ; fi
if [ "$CXX" = 'g++' ] ; then CXX=""; fi
if [ "$FFCXXFLAGS" = '-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -DNDEBUG -DDRAWING -DBAMG_LONG_LONG  -DNCHECKPTR -rdynamic' ] ; then FFCXXFLAGS=""; fi
if [ "$FFVERSION" = '3.0-5' ] ; then FFVERSION=""; fi
if [ -z "$INCFF" ] ; then
    if [ -f "@INCFF@/ff++.hpp" ] ; then
	INCFF="-I@INCFF@"
    elif [ -f include/ff++.hpp ]; then
	INCFF="-Iinclude"
    elif [ -f "FFAPPLI_INC/ff++.hpp" ] ; then 
	INCFF="-IFFAPPLI_INC" 
    elif [ -f '/usr/include/freefem++/ff++.hpp' ] ; then 
	INCFF='-I/usr/include/freefem++' 
    else
	echo " erreur PB include freefem++ directory " ;
	exit 1;
    fi
fi
do="yes"
DEBUG=""
CONLY="false"
uu=`uname -s` 
INC=""
LIBS=""
DLL=""
bin="."
out=""
files=""
fileso=""
OTHER="";
WIN32="";

case "$uu" in
  CYGWIN*)
  WIN32="win32-"
  wff=`which FreeFem++.exe`
  dff=`dirname "$wff"`
  if [ "$bin" = "." -a  -f ../src/bin-win32/libff.dll  ]; then
    bin=../src/bin-win32 
  elif [  -f "$dff/libff.dll"  ]; then
#  modif for install on cygwin
    bin=$dff
  elif [ "$bin" = "." -a  -f ../libff.dll  ]; then
    bin=..
  elif [ "$bin" = "." -a  -f ../../libff.dll  ]; then
    bin=../..
  fi;; 
esac

while [ $# -ne 0 ]  ; do
    case "$1" in
	-[h?]*) echo usage $0 "[-n] [-g] [-win32] [-l libfile] [-I includedir]  files"
	    echo "    -n  :  do nothing just print"
	    echo "    -g  :  compile with -g option"
	    echo "    -c  : compile only"
	    echo "    -O*  :  compile with -O* option"
	    echo "    -cygwin: compile for cygwin/win32 OS (Window XP, ...)"
	    echo "    -win32:  compile for win32 OS (Window XP, ...) default under cygwin"
	    echo "    -l files  add files to the ld process (link)"
	    echo "    -I dir  add dir in include seach dir for compilation"
	    echo "    -b dir  to change the default install  dir " 
	    echo "    -dll file  add  dll and this file copie in the install dir"
	    echo "    -o outputfile without suffix"
	    echo "    file.{cpp,cp,cxx}"
	    exit 0;
	    ;;
	CXX=*) CXX=`echo $1|sed 's/[A-Z0-9]*=//'` ;;
	CXXFLAGS=*) CXXFLAGS=`echo $1|sed 's/[A-Z0-9]*=//'` ;;
	-n)  do="no";;
	-g)  DEBUG="$DEBUG $1";;
	-c)  CONLY=yes;;
	-O*) DEBUG="$DEBUG $1";;
	-cygwin)  onwin23=1; uu="cygwin-version";; 
	-win32)   onwin32=1; uu="win32";; 
	-b)     bin=$2  ; shift;;
	-I*)    INC="$INC $1";;
	-dll)   DLL="$DLL $2";shift;;
	-[Ll]*)    LIBS="$LIBS $1" ;;
	*.cpp) files="$files $1";  o=`basename $1 .cpp` ; fileso="$fileso $o.o";; 
	*.cp) files="$files $1";  o=`basename $1 .cp`   ; fileso="$fileso $o.o";; 
	*.cxx) files="$files $1";  o=`basename $1 .cxx` ; fileso="$fileso $o.o";; 
	*.o) fileso="$fileso $1"; o=`basename $1 .o` ;; 
	-o)  out=$2; shift;;
	*) OTHER="$OTHER $1";;
    esac
    shift
done

if [ -n "$onwin32" -a -f "$bin/libff.dll" ] ; then 
    echo " erreur the file libff.dll  must be exist in '$bin' the install directory," 
    echo " to link on windows OS "
    echo " Use the parameter -b to set the correct install directory " 
    exit 1;
fi

if [ -z "$out" ] ; then  out=$o  ; fi;
# Default compiler
if [ -z "$CXX"  ];then  CXX=g++ ; fi

INC="$INCFF $INC"  
SUF=so
SHARED="-shared"
case "$WIN32$uu" in
    Darwin*) 
        echo "export MACOSX_DEPLOYMENT_TARGET=10.3"
	export MACOSX_DEPLOYMENT_TARGET=10.3
	SUF=dylib
	SHARED="-bundle -undefined dynamic_lookup"  ;;
    win32-CYGWIN*|win32-win32)
        echo " Window without cygwin "
        b=$bin
        LIBS=" $b/libff.dll $LIBS $DLL"
        SHARED="-shared --unresolved-symbols=ignore-all -Wl,--enable-auto-import"
        FLAGS='  -mno-cygwin '
        SUF=dll;;
    win32-cygwin-version)
        echo " cygwin-version "
        b=$bin
        FLAGS=' '
        LIBS="$b/libff.dll $LIBS $DLL"
        SUF=dll;;
    FreeBSD)
	SHARED="-shared" 
	FLAGS='-fPIC';;
	# 64 bit Linux needs -fPIC (ALH)
    SunOS)
        SHARED="-shared"
        FLAGS='-fPIC';;
        # 64 bit Linux needs -fPIC (ALH)
    Linux)
	FLAGS='-fPIC'
	SHARED="-shared " ;;
    *)
	echo "sorry unknown achitecture "`uname`
	exit 1;;
esac
FLAGS="$FLAGS $DEBUG $FFCXXFLAGS"
if [ -n "$files$fileso" ] ; then
    if [ "$WIN32" = "yes" -a -f $bin/libff.dll  ]; then
	echo " Sorry, no freefem .dll file (libff.dll)  in $bin dir "
	echo " try with -b dir-path where the file libff.dll exist"
	exit 1;
    fi
    if [ "$files" ] ;then 
	echo $CXX -c $FLAGS $CXXFLAGS $INC $PIC $files
	if [  $do = yes ] ; then 
	    $CXX -c $INC $FLAGS $CXXFLAGS $PIC  $files
	    ret=$?; if [ $ret -ne 0 ] ; then exit $ret ;fi
	fi
    fi
    if [ "$CONLY" != yes ]; then
	echo $CXX $SHARED $FLAGS $CXXFLAGS $fileso -o $out.$SUF $LIBS $DLL $OTHER
	if [  $do = yes ] ; then 
	    $CXX $SHARED $FLAGS $CXXFLAGS $fileso -o $out.$SUF $LIBS $DLL $OTHER
	    ret=$?; if [ $ret -ne 0 ] ; then exit $ret ;fi
	fi
	if [  "$bin" != "." ]; then
	    echo cp $out.$SUF  $bin
	    test $do = yes &&  cp $out.$SUF  $bin
	fi
	if [ -n "$DLL" ] ; then
	    echo cp $DLL $bin
	    test $do = yes &&  cp $DLL $bin
	fi
    fi
fi
