#!/bin/sh

set -e

# Script to compile the Ampl Solver Library on Unix systems. Why this approach?
# Well, we don't want to get into the business of creating (and maintaining)
# the full set of autotools source files for the ASL. This approach tries to
# leverage makefile.u, which comes with ASL.

# To support VPATH builds, the strategy is to copy the sources to the build
# directory, build, and then erase the sources when we're done. Start by
# copying the sources.

# set -x
mkinstalldirs="@install_sh@ -d"
abs_source_dir=@abs_source_dir@
compdir=compdir

# Remove the old compilation directory, if present, and create a new one. Then
# copy the sources. The wildcards given to ls will produce duplicates, which
# may result in a warning as the files are linked or copied.

rm -rf $compdir
$mkinstalldirs $compdir
files=`cd $abs_source_dir/solvers ; ls *.[chs] *.[ch]0 *.hd arith.* makefile* *.bat amplsolv.lbc`

cd $compdir
for file in $files ; do
  rm -f $file
  @LN_S@ $abs_source_dir/solvers/$file $file
done

# Acquire the system details. On some systems (Cygwin, for example), this
# string will contain '/' characters, so escape them properly.

sys_details=`uname -sr | sed -e 's/\\//\\\\\\//g'`

sed -e "/sysdetails_ASL/s/System_details/$sys_details/" details.c0 > details.c

# Adjust solvers/makefile.u for the system at hand. Really all we're doing
# is trying to automate the instructions in makefile.u. If you have build
# problems, a good thing to do is compare the shell code here with makefile.u,
# to see if it contains instructions that are not automated here. The final
# result is left in makefile.coin

# Allow make to decide the name of the archive program and the appropriate
# flags. makefile.u hardwires `ar' and sets ARFLAGS = ruv. Since we trash the
# entire build and start from scratch, the `u' in ruv stands for `useless'.

sed -e 's# ar # @AR@ #g' \
    -e 's/^ARFLAGS =/# ARFLAGS =/' < makefile.u > makefile.coin

# If we're in a fake unix environment on a Windows box, the default compiler
# output file is a.exe, not a.out.

if test "x@EXEEXT@" = "x.exe" ; then
  sed -e 's/a\.out/a.exe/' < makefile.coin > makefile.coin.tmp
  mv makefile.coin.tmp makefile.coin
fi

# System-specific tweaks.  On Solaris x86, we need to build fpsetprec.s
# (fpsetprec64.s for 64-bit builds). Add it to the dependency list for
# amplsolver.a. The 64-bit question here is how to reliably recognise a 64-bit
# build. For GCC, -m64 should do it. For Sun Studio, it's more difficult.
# Studio 12 and later recognises -m64. Earlier versions require
# -xarch=<something>, where something can be any of generic64, native64, amd64,
# or amd64a (and perhaps others). So ... let's go out on a limb and hope that
# -xarch=*64* will not pick up anything it shouldn't. Force leading and
# trailing spaces in the string we're matching, in case the 64-bit flag is the
# only content in CFLAGS.

case "@build@" in
  *86-*-solaris*)
    case " @CFLAGS@ " in
      *" "-m64" "* | *" "-xarch=*64*" "*)
	sed -e 's/^amplsolver.a:/amplsolver.a: fpsetprec64.s/' \
	  < makefile.coin > makefile.coin.tmp
	;;
      *)
	sed -e 's/^amplsolver.a:/amplsolver.a: fpsetprec.s/' \
	  < makefile.coin > makefile.coin.tmp
	;;
    esac
    mv makefile.coin.tmp makefile.coin
    ;;
esac

# That's it, we can do the build.
# A last detail: makefile.u will try to force CC=cc. We need to make sure
# that we stay with our chosen compiler. It will try and force CFLAGS, too, but
# configure has already put the appropriate information into ASLMAKEFLAGS.

libampl=@AMPLSOLVER_A@
make -f makefile.coin @ASLMAKEFLAGS@ CC="@CC@"
mv amplsolver.a ../$libampl
mv stdio1.h arith.h funcadd0.@OBJEXT@ ..
cd ..
rm -rf $compdir

