#!/bin/bash 

# The first task is to figure out OSTYPE.
# In most cases it is set automatically before
# the user's shell begins, but sometimes it is
# not set or set in a way that is inconsistent
# with our usage.

# On SGI IRIX systems, OSTYPE is not set
# (at least in Tel-Aviv University)

if [ \( ${OSTYPE:-fail} = "fail" \) -a \( `uname` = "IRIX64" \) ] ; then
  OSTYPE=irix
fi

# On Linux OSTYPE is usually set to "linux",
# but if it is not set (or if the user unsets it),
# /bin/sh supplies its own politically-correct,
# string, which we need to normalize.

if [ ${OSTYPE:-fail} = "linux-gnu" ] ; then
  OSTYPE=linux
fi

# If OSTYPE is still not set, we try to set it
# from uname, folding to lower case. This should sometimes
# work, but sometimes uname returns a value that is
# inconsistent with the way OSTYPE is set. For example, on
# Solaris, OSTYPE=solaris but uname returns SunOS.

if [ ${OSTYPE:-fail} = "fail" ] ; then
  OSTYPE=`uname | tr "A-Z" "a-z"`
fi

# If nothing works, we continue, but configurator will abort

if [ ${OSTYPE:-fail} = "fail" ] ; then
  echo ""
else
  export OSTYPE
fi

# Second phase: we try to make configurator.
# The makefile.unix is highly generic and should work
# on most systems.
# If we find exceptions that cannot be fixed in a 
# generic way, we should use OSTYPE to fix the problems.

make -f configurator/makefile.unix

# Third phase: try to run configurator with whatever
# arguments were passed to the script. If it fails, perhaps
# it was built on another platform, in which case we simply
# clean it and try to remake.

if configurator/configurator $* > .lastconf ; then
  TAUCS_LASTCONF=`cat .lastconf`
  /bin/rm .lastconf
  echo ""
else
  echo "Failed to run the configurator, maybe it was built"
  echo "on another platform; trying to clean and remake."
  make -f configurator/makefile.unix clean
  make -f configurator/makefile.unix

  configurator/configurator $* > .lastconf
  TAUCS_LASTCONF=`cat .lastconf`
  /bin/rm .lastconf
fi

# end of script

