# The sysname function uses uname -s to try to decode what kind of
# system to build. Currently the return value of uname is mapped as
#       Linux          --> linux
#       MINGW32_NT-5.1 --> windows
#       SunOS          --> Solaris9
#       Fedora Core 3  --> fedora3
#       freebsd        --> freebsd
#
# The solaris platform needs patch->gpatch, awk->gawk, tar->gtar

sysname () {
if [ -f /etc/redhat-release ] ;
 then 
  SYSNAME=`cat /etc/redhat-release` 
  if [ "$SYSNAME" = "Fedora Core release 3 (Heidelberg)" ] ; 
   then SYSNAME=fedora3
  fi
  echo SYSNAME=$SYSNAME
fi
if [ ! "$SYSNAME" = "fedora3" ] ; 
 then
   SYSNAME=`uname -s`
   echo $SYSNAME 
   if [ "$SYSNAME" = "Linux" ] ; then SYSNAME=linux
   elif  [ "$SYSNAME" = "MINGW32_NT-5.1" ] ; then SYSNAME=windows
   elif  [ "$SYSNAME" = "SunOS" ] ; then SYSNAME=solaris9
   elif  [ "$SYSNAME" = "freebsd" ] ; then SYSNAME=freebsd
   else
     echo Your system name is $SYSNAME
     echo We do not know how to build for this kind of system
     echo Send a note to axiom-developer@nongnu.org about it
     echo
     exit 0
   fi
fi

}

# This function checks for the gawk command. 
# If it exists then AWKNAME is the complete pathname

checkgawk() {
AWKNAME=`which gawk 2>>trace`
if [ -n "$AWKNAME" ] ; then
 if [ -x $AWKNAME ] ; then 
  echo 
 fi
fi
}

# This function checks for the nawk command. 
# If it exists then AWKNAME is the complete pathname

checknawk() {
AWKNAME=`which nawk 2>>trace`
if [ -n "$AWKNAME" ] ; then
 if [ -x $AWKNAME ] ; then 
  echo 
 fi
fi
}

# This function checks for the awk command. 
# If it exists then AWKNAME is the complete pathname

checkawk() {
AWKNAME=`which awk 2>>trace`
if [ -n "$AWKNAME" ] ; then
 if [ -x $AWKNAME ] ; then 
  echo 
 fi
fi
}

# This function uses the check*awk functions to decide 
# whether the system can build noweb. If one of gawk, nawk or awk
# are not found we fail.
needAwk ()
{
checkgawk
if [ -z "$AWKNAME" ] ; then
  checknawk
  if [ -z "$AWKNAME" ] ; then
    checkawk
    if [ -z "$AWKNAME" ] ; then
      echo We need the commands gawk, nawk, or awk
      exit 0
    fi
  fi
fi
}

# The mustSet function tells the user what needs to be typed on the 
# command line. If any extra variables need to be set we add them here.
# Currently the only thing we check if for the presence of gawk, which
# is the default in the Makefile. If gawk does not exist we can use 
# either nawk or awk but the user has to specify that on the command line.

# We check the system we are using with the uname command and try to
# generate the appropriate value. We fail otherwise.

# We generate the appropriate command line that the user should use.

mustSet() {
echo
echo ===================================================
echo You must set your AXIOM and PATH variables. Type:
echo
echo To build the rest of the system type:
echo
echo export AXIOM=`pwd`/mnt/$SYSNAME
echo 'export PATH=$AXIOM/bin:$PATH'
if [ "$SYSNAME" = "freebsd" ] ; then
  echo Note that freebsd usually has noweb available
  echo If you wish to use the standard version you must type
  echo touch noweb 
  echo If you wish to use a pre-installed GCL you must type
  echo make GCLVERSION=gcl-system
fi
if [ "$SYSNAME" = "solaris9" ] ; 
 then echo make AWK=gawk TAR=gtar PATCH=gpatch
elif [ "`basename $AWKNAME`" = "gawk"  ] ; 
 then echo make
 else echo make AWK=$AWKNAME
fi
echo
echo configure finished.
}

#########################################################################
# This is the main line of configure logic.
# (1) We test to see if we understand this system name. So far
#     the recognized strings from uname -s are translated as:
#       Linux          --> linux
#       MINGW32_NT-5.1 --> windows
#       SunOS          --> Solaris9
#       Fedora Core 3  --> fedora3
#       freebsd        --> freebsd
# (1) We test for the AWK variable. We need one of gawk, nawk, or awk
#     in order to build the noweb software.
# (2) Then we output the final message for the user.
#
# The solaris platform needs patch->gpatch, awk->gawk, tar->gtar
#########################################################################

sysname
needAwk

if [ "x$AXIOM" = "x" ] ;
 then mustSet
 else 
  if [ ! "`dirname $AXIOM`" = "`pwd`/mnt" ]
    then mustSet
    else 
     echo Configure complete. Now type
     echo
     echo make
     echo
  fi
fi
  
