#!/bin/sh
#
# BFI configure-like script to bootstrap ATLAS's C-based config scripts
# dependencies: sed, pwd
# shell built-in deps: echo, test
#
#
# srcdir0 is configure path without trailing configure :)
#
srcdir0=`echo $0 | sed -e "s/configure$//"`
#
# blddir is present directory
#
blddir=`pwd`
#
# srcdir is simply srcdir0 if the line does not begin with "../" and is
# blddir/srcdir0 otherwise
chk=`echo "$srcdir0" | sed -e "s/^\.\.\///"`
if test "$chk" = "$srcdir0"
then
   srcdir="$srcdir0"
else
   srcdir="$blddir"/"$srcdir0"
fi
#
# Default topdir to srcdir/../ATLAS
#
topdir0=$srcdir/../ATLAS
for arg in "$@"
do
   handled=0
   flag=`echo "$arg" | sed -e "s/--help//"`
   if test "$flag" != "$arg"
   then
      echo "USAGE: configure --atldir=<path/to/atlas/BLDdir>"
      exit 1
   fi
   flag=`echo "$arg" | sed -e "s/--atldir=//"`
   if test "$flag" != "$arg"
   then
      topdir0="$flag"
      handled=1
   fi
   if test $handled -eq 0
   then
      echo "Unknown flag ($arg), exiting."
      exit 2
   fi
done
#
# If the topdir begins with ../, then concat it to pwd, otherwise leave alone
#
chk=`echo "$topdir0" | sed -e "s/^\.\.\///"`
if test "$chk" = "$topdir0"
then
   topdir="$topdir0"
else
   topdir="$blddir"/"$topdir0"
fi
#
# If $topdir/Make.inc does not exist, die with error message
#
if [ ! -s $topdir/Make.inc ]
then
   echo "No such file $topdir/Make.inc"
   echo "Try setting --atldir=ATLAS/install/directory, and try again"
   exit 3
fi
rm -rf Makefile res F77 C
mkdir res F77 C
echo "TOPdir = $topdir" > Makefile
echo "include $topdir/Make.inc" >> Makefile
echo "SRCdir = $srcdir" >> Makefile
echo "TSTdir = $blddir" >> Makefile
cat $srcdir/Makefile >> Makefile
