#!/bin/sh
# Configuration script for MIT Scheme
# $Id: config,v 1.28 1993/11/09 16:11:39 gjr Exp $
# Modelled on the configuration script for GNU CC
# The section between lines is the copyright prefix from the GNU CC config.
#----------------------------------------------------------------------

#   Copyright (C) 1988 Free Software Foundation, Inc.

#This file is part of GNU CC.

#GNU CC is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 1, or (at your option)
#any later version.

#GNU CC is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with GNU CC; see the file COPYING.  If not, write to
#the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

#----------------------------------------------------------------------
#
# Shell script to create proper links to machine-dependent files in
# preparation for compiling gcc.
#
# Usage: config [-C-back-end | -native-back-end | -no-back-end] machine
#
# If config succeeds, it leaves its status in config.out.
# If config fails after disturbing the status quo, 
# 	config.out is removed.
#

usage()
{
	echo "Usage: $progname [-C-back-end | -native-back-end | -no-back-end] machine"
	echo "Where \`machine' is one of:"
	echo "alpha-osf alpha"
	echo "mips-ultrix nws3250 sgi4d"
	echo "hp9k300 hp9k400 hp9k700 hp9k800 next"
	echo "sun3 sun3-os3 sun3-nfp sun3-os3-nfp sun4 sparc"
	echo "i386-sysv 386bsd umax"
	echo "vax-bsd42 vax-bsd43 vax-ultrix"
	echo "rs6000"
	if [ -r config.out ]
	then
		cat config.out
	fi
	exit 1;
}


discriminate()
{
	machine=$1

	case $machine in
	alpha-osf | alpha)		# DEC Alpha running OSF
		system_file=decosf
		machine_file=alpha
		cmpint_file=alpha.h
		cmpaux_file=alpha.m4
		;;
	mips-ultrix | dec-3100 | dec-5100 | pmax)
		system_file=ultrix
		machine_file=mips
		cmpint_file=mips.h
		cmpaux_file=mips.m4
		;;
 	sgi4d)
 		system_file=irix4
 		machine_file=mips
 		cmpint_file=mips.h
		cmpaux_file=mips.m4
 		;;
	nws3250)			# sony news laptop
		system_file=newsos5
		machine_file=mips
		cmpint_file=mips.h
		cmpaux_file=mips.m4
		;;
 	linux | 386bsd)
 		system_file=386bsd
 		machine_file=i386
 		cmpint_file=i386.h
		cmpaux_file=i386.m4
 		;;
	i386-sysv)
		system_file=sysv3
		machine_file=i386
		cmpint_file=i386.h
		cmpaux_file=i386.m4
		;;
	hp9k800 | hp9k700)		# HP9000 series 800
		system_file=hpux
		machine_file=hp9k800
		cmpint_file=hppa.h
		cmpaux_file=hppa.m4
		;;
	hp9k300)			# HP9000 series 300
		system_file=hpux
		machine_file=hp9k300
		cmpint_file=mc68k.h
		cmpaux_file=mc68k.m4
		;;
	hp9k400)			# HP9000 series 400
		system_file=hpux
		machine_file=hp9k400
		cmpint_file=mc68k.h
		cmpaux_file=mc68k.m4
		;;
	next)
		system_file=nextos
		machine_file=next
		cmpint_file=mc68k.h
		cmpaux_file=mc68k.m4
		cmp_file=sun3-gcc.s
		;;
	sun3)
		system_file=sunos4
		machine_file=sun3
		cmpint_file=mc68k.h
		cmpaux_file=mc68k.m4
		cmp_file=sun3.s
		;;
	sun3-os3)			# sun3, pre-4.0 sunos
		system_file=sunos3
		machine_file=sun3
		cmpint_file=mc68k.h
		cmpaux_file=mc68k.m4
		cmp_file=sun3.s
		;;
	sun3-nfp)			# sun3, No Floating Point
		system_file=sunos4
		machine_file=sun3
		cmpint_file=mc68k.h
		cmpaux_file=mc68k.m4
		cmp_file=sun3-nfp.s
		;;
	sun3-os3-nfp)			# sun3, pre-4.0 sunos, No FP
		system_file=sunos3
		machine_file=sun3
		cmpint_file=mc68k.h
		cmpaux_file=mc68k.m4
		cmp_file=sun3-nfp.s
		;;
	sun4 | sparc)
		system_file=sunos4
		machine_file=sun4 
		;;
	rs6000)
		system_file=aix
		machine_file=rs6000
		;;
	umax)				# Encore Multimax
	    	system_file=umax
		machine_file=umax
		;;
	vax-bsd42)			# vaxen running 4.2BSD
		system_file=bsd4-2
		machine_file=vax
		cmpint_file=vax.h
		cmpaux_file=vax.m4
		;;
	vax-bsd43)			# vaxen running 4.3BSD
		system_file=bsd4-3
		machine_file=vax
		cmpint_file=vax.h
		cmpaux_file=vax.m4
		;;
	vax-ultrix)			# vaxen running ultrix
		system_file=ultrix
		machine_file=vax
		cmpint_file=vax.h
		cmpaux_file=vax.m4
		;;
	*)
		echo "$progname: unknown machine name: $machine"
		usage
	esac;
}

perform()
{
	files="s/${system_file}.h m/${machine_file}.h unxutl/makefile unxutl/ymkfile unxutl/ymake.sed"
	links="s.h m.h makefile ymkfile ymake.sed"

	while [ -n "$files" ]
	do
		# set file to car of files, files to cdr of files
		set $files; file=$1; shift; files=$*
		set $links; link=$1; shift; links=$*

		if [ ! -r $file ]
		then
			echo "$progname: cannot create a link \`$link',"
			echo "since the file \`$file' does not exist."
			exit 1
		fi

		$remove -f $link
		$remove -f config.out
		# Make a symlink if possible, otherwise try a hard link
		$symbolic_link $file $link 2>/dev/null || $hard_link $file $link

		if [ ! -r $link ]
		then
			echo "$progname: unable to link \`$link' to \`$file'."
			exit 1
		fi
		echo "Linked \`$link' to \`$file'."
	done

	files="unxutl/cf-dist.h"
	targets="cf.h"

	while [ -n "$files" ]
	do
		# set file to car of files, files to cdr of files
		set $files; file=$1; shift; files=$*
		set $targets; target=$1; shift; targets=$*

		if [ ! -r $file ]
		then
			echo "$progname: cannot create file \`$target',"
			echo "since the file \`$file' does not exist."
			exit 1
		fi

		$remove -f $target
		$remove -f config.out
		# Make a symlink if possible, otherwise try a hard link
		$copy $file $target

		if [ ! -r $target ]
		then
			echo "$progname: unable to copy \`$file' to \`$target'."
			exit 1
		fi
		echo "Copied \`$file' to \`$target'."
	done

	case $cmpint_file in
	nothing_special)
		;;
	*)
		$symbolic_link cmpintmd/$cmpint_file cmpintmd.h 2>/dev/null \
		  || $hard_link cmpintmd/$cmpint_file cmpintmd.h
		if [ ! -r cmpintmd.h ]
		then
			echo "$progname: unable to link \`cmpintmd.h' to \`cmpintmd/$cmpint_file'."
			exit 1
		fi
		echo "Linked \`cmpintmd.h' to \`cmpintmd/$cmpint_file'."
		;;
	esac

	case $cmpaux_file in
	nothing_special)
		;;
	*)
		$symbolic_link cmpauxmd/$cmpaux_file $cmpaux_target 2>/dev/null \
		  || $hard_link cmpauxmd/$cmpaux_file $cmpaux_target
		if [ ! -r $cmpaux_target ]
		then
			echo "$progname: unable to link \`$cmpaux_target' to \`cmpauxmd/$cmpaux_file'."
			exit 1
		fi
		echo "Linked \`$cmpaux_target' to \`cmpauxmd/$cmpaux_file'."
		;;
	esac

	case $cmp_file in
	nothing_special)
		;;
	*)
		$symbolic_link cmpauxmd/$cmp_file cmpauxmd.s 2>/dev/null \
		  || $hard_link cmpauxmd/$cmp_file cmpauxmd.s
		if [ ! -r cmpauxmd.s ]
		then
			echo "$progname: unable to link \`cmpauxmd.s' to \`cmpauxmd/$cmp_file'."
			exit 1
		fi
		echo "Linked \`cmpauxmd.s' to \`cmpauxmd/$cmp_file'."
		;;
	esac

	(echo "Links are now set up for use with a $machine." ; \
         echo "Remember to edit file cf.h before using make.") \
		| tee config.out
}

progname=$0

remove=rm
hard_link=ln
symbolic_link='ln -s'
copy=cp
move=mv
edit=sed

#for Test
#remove="echo rm"
#hard_link="echo ln"
#symbolic_link="echo ln -s"
#copy="echo cp"
#move="echo mv"
#edif="echo sed"

cmpint_file=nothing_special
cmpaux_file=nothing_special
cmpaux_target=cmpauxmd.m4
cmp_file=nothing_special

case $# in
1)
	discriminate $*
	perform $*
	exit 0
	;;

2)
	case $1 in

	-C-back-end | -c-back-end)
		shift
		discriminate $*
		cmpint_file=c.h
		cmpaux_file=c.c
		cmpaux_target=cmpauxmd.c
		perform $*
		$move cf.h cf-orig.h
		$edit 's/BACK_END_TYPE 2/BACK_END_TYPE 1/' <cf-orig.h >cf.h
		$remove -f cf-orig.h
		exit 0
		;;

	-native-back-end)
		shift
		discriminate $*
		perform $*
		$move cf.h cf-orig.h
		$edit 's/BACK_END_TYPE 2/BACK_END_TYPE 2/' <cf-orig.h >cf.h
		$remove -f cf-orig.h
		exit 0
		;;

	-no-back-end)
		shift
		discriminate $*
		cmpint_file=nothing_special
		cmpaux_file=nothing_special
		cmpaux_target=cmpauxmd.c
		perform $*
		$move cf.h cf-orig.h
		$edit 's/BACK_END_TYPE 2/BACK_END_TYPE 0/' <cf-orig.h >cf.h
		$remove -f cf-orig.h
		exit 0
		;;

	*)
		usage
		;;
	esac
	;;

*)
	usage
	;;
esac
