#!/bin/sh
#
# This is just a driver for configure, the real configure is in src.
# This script identifies the machine, and creates a directory for
# the installation, where it copies configure, and then runs it.

progname="`echo $0 | sed 's%.*/%%'`"

machine=unknown
arguments=
## Sometimes PWD is inaccurate.
if [ "${PWD}" != "" ] && [ "`(cd ${PWD} ; sh -c pwd)`" = "`pwd`" ] ; then
  srcdir="$PWD/src"
else
  srcdir="./src"
fi

### Process  options:
while [ $# != 0 ]; do
  arg="$1"; shift
  case "${arg}" in

    ## Anything starting with a hyphen we assume is an option.
    -* )
      ## Separate the switch name from the value it's being given.
      case "${arg}" in
        --srcdir=*)
	  srcdir=`echo ${arg} | sed 's:^-*[^=]*=\(.*\)$:\1:'`
	;;
	--srcdir )
	  ## If the value was omitted, get it from the next argument.
	  ## Get the next argument from the argument list, if there is one.
          if [ $# = 0 ]; then
	     (echo "${progname}: You must give a value for the \`--${optname}' option, as in
    \`--${optname}=FOO'."
	      ./src/configure --help) >&2
	      exit 1
	  fi
	  val="$1"; shift
	;;

	--in-place)
	  buildir='src'
	;;

        ## Has the user asked for some help?
        "-usage" | "-help" )
  	  ./src/configure --help
          exit 1
	;;

	* )
	arguments="${arguments} ${arg}"
	;;
      esac
    ;;


    ## Anything not starting with a hyphen we assume is a
    ## MACHINE name.
    *)
      machine=${arg}
    ;;

  esac
done

echo "Type 'configure -help' if you need help in using this script"

# If we are in a different directory and buildir is not supplied,
# use this directory for building
dirname=`dirname $0`
if [ "$buildir" = "" -a $dirname != ${PWD} -a $dirname != "." ] ; then
 buildir=${PWD}
fi

if [ ${machine} = unknown ]; then
   # Try to guess the system name.
   #
   machine=`(uname -m) 2>/dev/null` || machine=unknown
   OS=`(uname -s) 2>/dev/null` || OS=unknown

   case "${machine}:${OS}" in
       alpha:*)
	   break ;;
       arm:*)
	   break ;;
       sun4*:*)
	   case `(uname -r) 2>/dev/null` in
	      5*)
		   machine=sun4_sunos5
		   break ;;
	      *)
		   machine=sun4
		   break ;;
	    esac
	    break ;;
       sun3*:*)
	   machine=sun3
	   break ;;
       RISC*:ULTRIX)
	   machine=mips_dec
	   break ;;
       VAX*:ULTRIX*)
	   machine=vax
	   break ;;
       *:AIX)
	   machine=IBMRT
	   break ;;
       *:IRIX)
	   machine=sgi
	   break ;;
       *:Linux)
	   machine=linux
	   break ;;
       *:HP-UX)
	   break ;;
   esac
fi
if [ ${machine} = unknown ] ; then 
    echo Unable to figure out for which machine to build
    echo Please check whether your machine is among those listed in:
    echo    src/h/machine.h
    echo
    echo "${usage}"
    exit 1
fi

#### Get ready to bootstrap

test -n "${buildir}" || buildir=${machine}

if [ ! -d ${buildir} ] ; then
   echo Creating directory "\`${buildir}'"
   mkdir ${buildir}
fi

# Now run the real configure script
echo Switching to directory "\`${buildir}'" to continue configuration.
cd ${buildir}
${srcdir}/configure ${machine} --srcdir=${srcdir} ${arguments}

echo Configuration complete. To build ECL, issue 'make' in this directory.
