#!/bin/sh
# usage: configure gappath
# this script creates a `Makefile' from `Makefile.in' 

if [ ! -f Makefile.in ]; then
   echo "ERROR:  File 'Makefile.in' not found in current directory"
   echo "        Change directory to .../gap/pkg/pargapmpi/ first"
   echo "        Then execute:  ./configure PATH_TO_GAP_ROOT"
   echo "        For example:   ./configure ../.."; exit
fi
if [ -z "$1" ]; then
   echo "  Usage:     ./configure PATH_TO_GAP_ROOT"
   echo "  Example:   ./configure ../.."; exit
fi
if [ ! -f ../../sysinfo.gap ]; then
   echo "WARNING:  This isn't being run from GAP_ROOT/pkg/pargapmpi."
   echo "We will need to modify the GAP kernel."
   echo "If we fail due to lack of write permission, you may have to"
   echo "  start again with a private copy of GAP."
   echo ""
   echo "You can build a private copy in /tmp/ if you are short of disk space."
   echo "The private copy need only contain the .../gap4/src/ directory."
   echo 'Then do a standard GAP "./configure; make" in the private copy.'
   echo "Then make pargapmpi in that copy, and copy the pargapmpi.sh and gap"
   echo "  binary from the private GAP to the bin subdirectory of this package."
   echo "It then remains only to modify the paths inside pargapmpi.sh."
fi

# HACK:  Make sure last GAP configure was for current architecture
(cd $1; ./configure)

# HACK:
# When GAP's sysinfo.gap includes a CC=... variable, second cat can go away.
rm -f Make_sedfile sedfile
echo "#!/bin/sh" > Make_sedfile
cat $1/sysinfo.gap >> Make_sedfile
cat $1/sysinfo.gap | sed -e 's#^GAParch=.*-#CC=#' >> Make_sedfile
echo "echo s#@GAPARCH@#\$GAParch#g >>sedfile" >> Make_sedfile
echo "echo s#@CC@#\$CC#g >>sedfile" >> Make_sedfile
echo "echo s#@GAPPATH@#$1#g >>sedfile" >> Make_sedfile
echo "echo s#@PWD@#$PWD#g >>sedfile" >> Make_sedfile
if ( uname -sr | grep "SunOS 5" > /dev/null ) ; then
  echo "echo s#@SOCKETLIBS@#-lsocket -lnsl#g >>sedfile" >> Make_sedfile
else
  echo "echo s#@SOCKETLIBS@##g >>sedfile" >> Make_sedfile
fi
chmod +x Make_sedfile
./Make_sedfile

rm -f Makefile Make_sedfile procgroup
sed -f sedfile Makefile.in >Makefile
sed -f sedfile bin/procgroup.in >bin/procgroup
rm -f sedfile
