#!/bin/bash


package=nlojet++
version=4.1.3

prefix=/usr
exec_prefix=/usr
bindir=/usr/bin
libdir=/usr/lib64
libexecdir=/usr/libexec
includedir=/usr/include

cc='gcc'
if test -n $CFLAGS; then
  cflags='-O2 -g -m64 -fmessage-length=0 -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables'
else 
  cflags="$CFLAGS"
fi

cxx='g++'
if test -n $CXXFLAGS; then
  cxxflags='-O2 -g -m64 -fmessage-length=0 -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables'
else 
  cxxflags="$CXXFLAGS"
fi

f77='gfortran'
if test -n $FFLAGS; then
  fflags='-O2 -g -m64 -fmessage-length=0 -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables '
else 
  fflags="$FFLAGS"
fi

ld='/usr/x86_64-suse-linux/bin/ld -m elf_x86_64'
if test -n $LDFLAGS; then
  ldflags=''  
else 
  ldflags="$LDFLAGS"
fi


#  libtool program
libtool="$libexecdir/nlo++/libtool-nlojet++" 

#  make the object directory
objdir=".obj"
if test ! -d $objdir;  then
  mkdir $objdir
fi

show="echo"
ac_prev= 
for option
do
  # If the previous option needs an argument, assign it.
   if test -n "$ac_prev"; then
    eval "$ac_prev=\$option"
    ac_prev=
    continue
   fi

   case "$option" in
     -Wf,*)
       fflags_=`echo "$option" | sed 's/-Wf,//'| sed 's/,/ /g'`
       fflags="$fflags $fflags_"
       ;;
     -Wc,*)
       cflags_=`echo "$option" | sed 's/-Wc,//'| sed 's/,/ /g'`
       cflags="$cflags $cflags_"
       ;;
     -Wx,*)
       cxxflags_=`echo "$option" | sed 's/-Wx,//'| sed 's/,/ /g'`
       cxxflags="$cxxflags $cxxflags_"
       ;;
     -Wl,*)
       ldflags_=`echo "$option" | sed 's/-Wl,//'| sed 's/,/ /g'`
       ldflags="$ldflags $ldflags_"
       ;;
     -o)
       ac_prev=output
       ;;
     --silent | --quiet)
       show=:
       libtool="$libtool --silent"
   esac
done

include="-I${includedir}/nlo++" 
cxxcompile="$libtool --mode=compile  $cxx -c $include $cxxflags"
ccompile="$libtool --mode=compile $cc -c $include $cflags"
f77compile="$libtool --mode=compile $f77 -c $fflags"

objects=
use_fortran=no

for option
do
    case "$option" in
    *.f)   xform=f ;;
    *.f77) xform=f77 ;;
    *.F)   xform=F ;;
    *.for) xform=for ;;
    *.c)   xform=c ;;
    *.cc)  xform=cc ;;
    *.c++) xform=c++ ;;
    *.cxx) xform=cxx ;;
    *.C)   xform=C ;;
    *.cpp) xform=cpp ;;
    esac

    case "$option" in
    *.f | *.F | *.for)
	obj=`echo "$option" | sed -e "s/\.$xform$/.lo/"`
	echo "Creating object $output-$obj from $option."
	if !(test -f "$objdir/$output-$obj") || test "$option" -nt "$objdir/$output-$obj"; then
	    $show "$f77compile $option -o $objdir/$output-$obj"
	    $f77compile $option -o $objdir/$output-$obj
	fi

	objects="$objects $objdir/$output-$obj"
	use_fortran=yes
	;;
    *.c)
	obj=`echo "$option" | sed -e "s/\.$xform$/.lo/"`
	echo "Creating object $output-$obj from $option."
	if !(test -f "$objdir/$output-$obj") || test "$option" -nt "$objdir/$output-$obj"; then
	    $show "$ccompile $option -o $objdir/$output-$obj"
	    $ccompile $option -o $objdir/$output-$obj
	fi
	objects="$objects $objdir/$output-$obj"
	;;
    *.cc | *.c++ | *.cxx | *.C | *.cpp)
	obj=`echo "$option" | sed -e "s/\.$xform$/.lo/"`
	echo "Creating object $output-$obj from $option."
	if !(test -f "$objdir/$output-$obj") || test "$option" -nt "$objdir/$output-$obj"; then
	    $show "$cxxcompile $option -o $objdir/$output-$obj"
	    $cxxcompile $option -o $objdir/$output-$obj
	fi
	objects="$objects $objdir/$output-$obj"
	;;
   esac
done

#   Adding the f77 interface library
ldflags="$ldflags -L${libdir} -lnlo++"

#   Adding the f77 standard libraries if it is necessary
if test "$use_fortran" = yes; then
  ldflags="$ldflags  -L/usr/lib64/gcc/x86_64-suse-linux/15 -L/usr/lib64/gcc/x86_64-suse-linux/15/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib64/gcc/x86_64-suse-linux/15/../../../../x86_64-suse-linux/lib -L/usr/lib64/gcc/x86_64-suse-linux/15/../../.. -L/lib -L/usr/lib -lgfortran -lm -lquadmath"
fi

#   Creating the module
path_current=`pwd`

echo "Creating loadable module $output.la"
$show "$libtool --mode=link $cxx -o $output.la -module -avoid-version $objects $ldflags -rpath $path_current"
$libtool --mode=link $cxx -o $output.la -module -avoid-version $objects $ldflags -rpath $path_current
