#!/bin/sh
#
# KROC (Kent Retargetable Occam Compiler) Configure program
#
# $Source: /u3/proj/kroc/develop/RCS/configure,v $
#
# $Id: configure,v 1.24 1997/02/04 14:30:01 djb1 Exp $
#
# For KROC version 0.7beta
#
# Dave Beckett <D.J.Beckett@ukc.ac.uk>,
# University of Kent at Canterbury, England
#
# THIS IS NOT YET A GNU CONFIGURE SCRIPT - but bits were taken from it.
# The original GNU configure script said:
#
# "Guess values for system-dependent variables and create Makefiles.
# Generated automatically using autoconf version 2.4 
# Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
#
# This configure script is free software; the Free Software Foundation
# gives unlimited permission to copy, distribute and modify it."
#
# so I have.

package_name=kroc

export PATH || (echo "OOPS, this isn't sh.  Desperation time.  I will feed myself to sh."; sh $0 $argv; kill $$)

## this is a little touchy and won't always work, but...
##
## if the argv[0] starts with a slash then it is an absolute name that can (and
## must) be used as is.
##
## otherwise, if argv[0] has no slash in it, we can assume that it is on the
## path.  Since PATH might include "." we also add `pwd` to the end of PATH.
##

progname=$0
# if PWD already has a value, it is probably wrong.
if [ -n "$PWD" ]; then PWD=`pwd`; fi

case "${progname}" in
/*) ;;
*/*) ;;
*)
        PATH=$PATH:${PWD=`pwd`} ; export PATH
        ;;
esac


srcdir=`echo ${progname} | sed 's:/configure$::'`

ac_default_prefix=/usr/local
ac_config_guess=./config.guess
ac_config_sub=`echo ${progname} | sed 's/configure$/config.sub/'`


fatal=
progname=
host=NONE
prefix=NONE
nonopt=NONE

ac_prev=
for ac_option
do

  # If the previous option needs an argument, assign it.
  if test -n "$ac_prev"; then
    eval "$ac_prev=\$ac_option"
    ac_prev=
    continue
  fi

  case "$ac_option" in
  -*=*) ac_optarg=`echo "$ac_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) ac_optarg= ;;
  esac

  # Accept the important Cygnus configure options, so we can diagnose typos.

  case "$ac_option" in

  -help | --help | --hel | --he)
    # Omit some internal or obsolete options to make the list less imposing.
    # This message is too long to be a string in the A/UX 3.1 sh.
    cat << EOF
Usage: configure [options] [target]
Options: [defaults in brackets after descriptions]
Configuration:
  --help                  print this message
Directory and file names:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [$ac_default_prefix]
Host type:
  --target=HOST           configure for TARGET [guessed]
EOF
    exit 0 ;;

  -prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
    ac_prev=prefix ;;
  -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
    prefix="$ac_optarg" ;;

  -target | --target | --targe | --targ | --tar | --ta | --t)
    ac_prev=target ;;
  -target=* | --target=* | --targe=* | --targ=* | --tar=* | --ta=* | --t=*)
    host="$ac_optarg" ;;

  -*) { echo "configure: error: $ac_option: invalid option; use --help to show usage" 1>&2; exit 1; }
    ;;

  *) 
    if test -n "`echo $ac_option| sed 's/[-a-z0-9.]//g'`"; then
      echo "configure: warning: $ac_option: invalid host type" 1>&2
    fi
    if test "x$nonopt" != xNONE; then
      { echo "configure: error: can only configure for one host and one target at a time" 1>&2; exit 1; }
    fi
    nonopt="$ac_option"
    ;;

  esac
done

if test -n "$ac_prev"; then
  { echo "configure: error: missing argument to --`echo $ac_prev | sed 's/_/-/g'`" 1>&2; exit 1; }
fi

package_version=`cat ${srcdir}/VERSION`
package="${package_name}-${package_version}"


# Do some error checking and defaulting for the host and target type.
# The inputs are:
#    configure --target=TARGET NONOPT
#
# The rules are:
# 1. You are not allowed to specify --target, and nonopt at the
#    same time. 
# 2. Target defaults to nonopt.
# 3. If nonopt is not specified, then target defaults to the current host,
#    as determined by config.guess.

# The aliases save the names the user supplied, while $host etc.
# will get canonicalized.
case $host---$nonopt in
NONE---* | *---NONE) ;;
*) { echo "configure: error: can only configure for one host and one target at a time" 1>&2; exit 1; } ;;
esac


# Make sure we can run config.sub.
if $ac_config_sub sun4 >/dev/null 2>&1; then :
else { echo "configure: error: can not run $ac_config_sub" 1>&2; exit 1; }
fi

echo "checking host system type" 1>&2

host_alias=$host
case "$host_alias" in
NONE)
  case $nonopt in
  NONE)
    if host_alias=`$ac_config_guess`; then :
    else { echo "configure: error: can not guess host type; you must specify one" 1>&2; exit 1; }
    fi ;;
  *) host_alias=$nonopt ;;
  esac ;;
esac

echo "Configuring $package_name for a ${host_alias} target." 1>&2

test "x$prefix" = xNONE && prefix=$ac_default_prefix

host=`$ac_config_sub $host_alias`
host_cpu=`echo $host | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\1/'`
host_vendor=`echo $host | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\2/'`
host_os=`echo $host | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\3/'`
host=${host_cpu}-${host_vendor}-${host_os}

# if this is a binary distribution, it is for this architecture/vendor:
if [ -r BINHOST ]; then
  bin_alias=`cat BINHOST`
  bin_cpu=`echo $bin_alias | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\1/'`
  bin_vendor=`echo $bin_alias | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\2/'`
  bin_os=`echo $bin_alias | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\3/'`
else
  bin_alias=$host
  bin_cpu=$host_cpu
  bin_vendor=$host_vendor
  bin_os=$host_os
  echo $bin_alias > BINHOST
fi

kroc_octranopts=
kroc_ccopts=""
# Can be linked BEFORE libkroc.a
kroc_llibs="-loccam8 -loccamutl"
# Must be linked AFTER libkroc.a
kroc_rtslibs="-lm"

ims_libconv_srcs="conv.occ"
ims_libconv_objs="conv.o"

# FIXME: burnt in dependencies
# ALPHA
case "$host_cpu" in
alpha)
	CCFLAGS=
	# Need to do this to get debugging info (in as(1), not gas)
	ASFLAGS=-g
	RANLIB=ranlib

	# -r  : NO reverse endianism
	# -g  : NO global PROC names
	# -h  : On Alpha, don't use C library integer divide and remainder
	#       functions but use appropriate alpha (pseudo)instructions
        #       (aka use the 'Hardware' arithmetic)
	kroc_octranopts="$kroc_octranopts -r -g -h"

	# -lm
	#  link with math library
	# -Wa,-w
	#  Pass -w to as(1)    = suppress warning messages
	# -Xlinker -taso
	#  Pass -taso to ld(1) = put all stuff in 32 bit address space (no 64
	#  bit pointers).
	kroc_ccopts="$kroc_ccopts -Wa,-w -g -Xlinker -taso"
	;;
sparc )
	case "$host_os" in
	sunos4*)
		# Sunos 4 (Solaris 1)
		CCFLAGS=
		ASFLAGS=-P
		RANLIB=ranlib
		;;
	solaris2*)
		# Sunos 5 (Solaris 2)
		CCFLAGS=
		ASFLAGS=-P
		RANLIB=
		;;
	esac
	ims_libconv_srcs="convsparc.s"
	ims_libconv_objs="convsparc.o"

	;;

i[3456]86 )
	case "$host_os" in
        linux*)
		# Linux (any)
		CCFLAGS=
		ASFLAGS=
		RANLIB=ranlib
		;;
        esac
	;;

* )
	CCFLAGS=
	ASFLAGS=
	RANLIB=ranlib
esac

KROC=`pwd`

HOST_CPU=`echo $host_cpu | tr '[a-z]' '[A-Z]'`
HOST_VENDOR=`echo $host_vendor | tr '[a-z]' '[A-Z]'`
HOST_OS=`echo $host_os | tr '[a-z]' '[A-Z]' | tr -d '_.'`

# Without the "./", some shells look in PATH for config.status.
: ${CONFIG_STATUS=./config.status}

echo creating $CONFIG_STATUS
rm -f $CONFIG_STATUS
cat > $CONFIG_STATUS <<EOF
#! /bin/sh
# Generated automatically by configure.
# but isn't a GNU config.status

ac_given_srcdir=$srcdir

trap 'rm -fr `echo "Makefile" | sed "s/:[^ ]*//g"` conftest*; exit 1' 1 2 15

# Protect against being on the right side of a sed subst in config.status. 
sed 's/%@/@@/; s/@%/@@/; s/%g$/@g/; /@g$/s/[\\\\&%]/\\\\&/g; 
 s/@@/%@/; s/@@/@%/; s/@g$/%g/' > conftest.subs <<\CEOF
s%@package_name@%$package_name%g
s%@package_version@%$package_version%g
s%@srcdir@%$srcdir%g
s%@host_alias@%$host_alias%g
s%@host_cpu@%$host_cpu%g
s%@host_vendor@%$host_vendor%g
s%@host_os@%$host_os%g
s%@host_canonical@%$host%g
s%@HOST_CPU@%$HOST_CPU%g
s%@HOST_VENDOR@%$HOST_VENDOR%g
s%@HOST_OS@%$HOST_OS%g
s%@CCFLAGS@%$CCFLAGS%g
s%@ASFLAGS@%$ASFLAGS%g
s%@RANLIB@%$RANLIB%g
s%@prefix@%$prefix%g
s%@KROC@%$KROC%g
s%@kroc_octranopts@%$kroc_octranopts%g
s%@kroc_ccopts@%$kroc_ccopts%g
s%@kroc_llibs@%$kroc_llibs%g
s%@kroc_rtslibs@%$kroc_rtslibs%g
s%@IMS_LIBCONV_SRCS@%$ims_libconv_srcs%g
s%@IMS_LIBCONV_OBJS@%$ims_libconv_objs%g

CEOF
EOF
cat >> $CONFIG_STATUS <<EOF

CONFIG_FILES=\${CONFIG_FILES-"Makefile bin/kroc src/config.h src/config.inc inmos/libs/hostio/Makefile inmos/libs/hostsp/Makefile"}
EOF
cat >> $CONFIG_STATUS <<\EOF
for ac_file in .. $CONFIG_FILES; do if test "x$ac_file" != x..; then
  # Support "outfile[:infile]", defaulting infile="outfile.in".
  case "$ac_file" in
  *:*) ac_file_in=`echo "$ac_file"|sed 's%.*:%%'`
       ac_file=`echo "$ac_file"|sed 's%:.*%%'` ;;
  *) ac_file_in="${ac_file}.in" ;;
  esac

  # Adjust relative srcdir, etc. for subdirectories.

  # Remove last slash and all that follows it.  Not all systems have dirname.
  ac_dir=`echo $ac_file|sed 's%/[^/][^/]*$%%'`
  if test "$ac_dir" != "$ac_file" && test "$ac_dir" != .; then
    # The file is in a subdirectory.
    #test ! -d "$ac_dir" && mkdir "$ac_dir"
    ac_dir_suffix="/`echo $ac_dir|sed 's%^\./%%'`"
    # A "../" for each directory in $ac_dir_suffix.
    ac_dots=`echo $ac_dir_suffix|sed 's%/[^/]*%../%g'`
  else
    ac_dir_suffix= ac_dots=
  fi

  case "$ac_given_srcdir" in
  .)  srcdir=.
      if test -z "$ac_dots"; then top_srcdir=.
      else top_srcdir=`echo $ac_dots|sed 's%/$%%'`; fi ;;
  /*) srcdir="$ac_given_srcdir$ac_dir_suffix"; top_srcdir="$ac_given_srcdir" ;;
  *) # Relative path.
    srcdir="$ac_dots$ac_given_srcdir$ac_dir_suffix"
    top_srcdir="$ac_dots$ac_given_srcdir" ;;
  esac

  if test -r  $ac_given_srcdir/$ac_file_in; then
  echo creating "$ac_file"
  rm -f "$ac_file"
  configure_input="Generated automatically from `echo $ac_file_in|sed 's%.*/%%'` by configure."
  case "$ac_file" in
  *Makefile*) ac_comsub="1i\\
# $configure_input" ;;
  *) ac_comsub= ;;
  esac
  sed -e "$ac_comsub
s%@configure_input@%$configure_input%g
s%@srcdir@%$srcdir%g
s%@top_srcdir@%$top_srcdir%g
" -f conftest.subs $ac_given_srcdir/$ac_file_in > $ac_file
  fi
fi; done
rm -f conftest.subs

chmod +x bin/kroc

exit 0
EOF
chmod +x $CONFIG_STATUS
rm -fr confdefs*
${CONFIG_SHELL-/bin/sh} $CONFIG_STATUS || exit 1

echo creating setup.csh
cat > ${KROC}/setup.csh <<EOF
set path=($KROC/bin \$path)
EOF

echo creating setup.sh
cat > ${KROC}/setup.sh << EOF
PATH=$KROC/bin:\$PATH
EOF

echo "Configuration done" 2>&1
echo


#if [ "$host_cpu" != "$bin_cpu" ]; then
#  echo "ERROR: CPU type not $bin_cpu - $package will almost certainly not work" 1>&1
#  exit 1
#fi
#if [ "$host_vendor" != "$bin_vendor" ]; then
#  echo "WARNING: Vendor not $bin_vendor - $package will probably not work" 1>&1
#fi
#if [ "$host_os" != "$bin_os" ]; then
#  echo "WARNING: OS type not $bin_os - but $package will probably work" 1>&1
#fi

echo "Now add $KROC/bin to your search path:"
echo "  set path=($KROC/bin \$path)"
echo "    for C-shell (csh) or related shells"
echo "    (Script for this stored in" ${KROC}/setup.csh ")"
echo
echo "  PATH=$KROC/bin:\$PATH"
echo "    for Bourne Shell (sh), Bash or related shells."
echo "    (Script for this stored in" ${KROC}/setup.sh ")"
exit 0
