#!/bin/sh
fndex=0
if [ $# -eq 4 ]
then
   ext=$4
   fndex=1
elif [ $# -ne 3 ]
then
   echo "Wrong number of parameters: $#"
   echo "USAGE: gcc 'gccflags' 'defs' [extract]"
   exit 1
fi
gcc=$1
shift
flags=$1
shift
defs=$1
shift

blddir=`pwd`
path=`echo $0 | sed -e "s/configure$//"`
#
# topdir is simply path if the line does not begin with "../" and is
#  blddir/path otherwise
chk=`echo "$path" | sed -e "s/^\.\.\///"`
if test "$chk" = "$path"
then
   topdir="$path"
else
   topdir="$blddir"/"$path"
fi
#
# Error out if topdir == blddir
#
touch $blddir/TstBld974.txt
if [ -f "$topdir/TstBld974.txt" ]
then
   echo "To use this script, build in a directory separate from the source"
   rm $blddir/TstBld974.txt
   exit 1
fi
rm $blddir/TstBld974.txt

if test $fndex -eq 0
then
   $gcc $flags -o extract $topdir/src/extract.c
   ext=$blddir/extract
   if [ ! -x $ext ]
   then
      echo "Unable to build extract with $gcc $flags!"
      exit 2
   fi
fi

#
# Create the Make.inc needed to build
#
echo SHELL=/bin/sh > Make.inc
echo BLDdir="$blddir" >> Make.inc
echo TOPdir="$topdir" >> Make.inc
echo CC="$gcc" >> Make.inc
echo CDEFS="$defs" >> Make.inc
echo PDEFS= >> Make.inc
echo CFLAGS="$flags" >> Make.inc
echo fINCs="-I\$(BLDdir)/include -I\$(TOPdir)/FKO/include" >> Make.inc
echo iINCs="-I\$(TOPdir)/include" >> Make.inc
echo "CCFLAGS=\$(CFLAGS) \$(CDEFS) \$(PDEFS)" >> Make.inc
echo "AR=ar" >> Make.inc
echo "ARF=r" >> Make.inc
echo "RANLIB=echo" >> Make.inc
echo EXTRACT="$ext" >> Make.inc

mkdir FKOsrc src include lib
$ext -langM -b $topdir/base/hmake.base -o Makefile rout=Make.bin 
$ext -langM -b $topdir/base/hmake.base -o src/Makefile rout=Make.isrc
$ext -langM -b $topdir/base/hmake.base -o FKOsrc/Makefile rout=Make.fsrc
$ext -langC -b $topdir/base/hmake.base -o include/fko_settings.h rout=fko_settings.h

echo "\nConfigure done."
