# Configuration script
# Copyright (C) 1994, 1995 Free Software Foundation, Inc.

# This program 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 2 of the License, or
# (at your option) any later version.
# 
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

### WARNING
### This script (and mpw-config.in fragments) must NOT use any 8-bit chars!
### WARNING

# This is an MPW Shell script that sets everything up for compilation,
# mainly creating directories, and editing copies of files.

Set savedir "`Directory`"

#Set Echo 1

Set ThisScript "{0}"

Set srcroot "--------"

Set srcdir ":"

Set objdir ":"

Set prefix ":"

Set host_alias "m68k-apple-mpw"

Set target_alias {host_alias}

Set prefix "{Boot}"Cygnus:latest:

Set verify 0
Set verifystr ""

# Parse arguments.

Loop
	Break If {#} == 0
	If "{1}" =~ /--prefix/
		Set prefix "{2}"
		Shift 1
	Else If "{1}" =~ /--srcdir/
		Set srcdir "{2}"
		Shift 1
	Else If "{1}" =~ /--srcroot/
		Set srcroot "{2}"
		Shift 1
	Else If "{1}" =~ /--target/
		Set target_alias "{2}"
		Shift 1
	Else If "{1}" =~ /-v/
		Set verify 1
		Set verifystr "-v"
		Shift 1
	Else
		Echo "{1}" is not a valid argument
		Exit 1
	End If
	Shift 1
End Loop

Set Exit 0

# (should interpret aliases if not in canonical form)

Set host_canonical "{host_alias}"

Set target_canonical "{target_alias}"

Set configdirs ""

If "{srcroot}" =~ /--------/
	Set srcroot "{srcdir}"
End If
If "`Exists "{srcdir}"`" == ""
	Echo Source directory {srcdir} does not exist!
	Exit 1
End If
If "`Exists "{srcroot}"`" == ""
	Echo Top-level source directory {srcroot} does not exist!
	Exit 1
End If

Set target_cpu "`echo {target_canonical} | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\1/'`"
Set target_vendor "`echo {target_canonical} | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\2/'`"
Set target_os "`echo {target_canonical} | sed 's/^\(.*\)-\(.*\)-\(.*\)$/\3/'`"

If "`Exists "{srcdir}"mpw-config.in`" != ""
	Execute "{srcdir}"mpw-config.in
End If

If "`Exists "{srcdir}"mpw-make.in`" != ""
	Echo "# This Makefile produced by mpw-configure.  Changes may get lost!" > "{objdir}"Makefile.tem
	Echo "srcroot = " {srcroot} >> "{objdir}"Makefile.tem
	Echo "topsrcdir = " {srcroot} >> "{objdir}"Makefile.tem
	Echo "srcdir = " {srcdir} >> "{objdir}"Makefile.tem
	Echo "mpw_prefix = " {prefix} >> "{objdir}"Makefile.tem
	Echo "host_alias = " {host_alias} >> "{objdir}"Makefile.tem
	Echo "target_alias = " {target_alias} >> "{objdir}"Makefile.tem
	Echo "target_cpu = " {target_cpu} >> "{objdir}"Makefile.tem
	Echo "target_vendor = " {target_vendor} >> "{objdir}"Makefile.tem
	Echo "target_os = " {target_os} >> "{objdir}"Makefile.tem
	Echo "target_canonical = " {target_canonical} >> "{objdir}"Makefile.tem
	Echo "host_makefile_frag = " >> "{objdir}"Makefile.tem
	Echo "target_makefile_frag = " >> "{objdir}"Makefile.tem
	If "`Exists "{srcdir}"config:mpw-mh-mpw`" != ""
		tr-7to8 "{srcdir}"config:mpw-mh-mpw >>"{objdir}"Makefile.tem
	Else If "`Exists "{srcroot}"config:mpw-mh-mpw`" != ""
		tr-7to8 "{srcroot}"config:mpw-mh-mpw >>"{objdir}"Makefile.tem
	Else
		Echo "can't find a host config file!"
		Exit 0
	End If
	If "`Exists "{objdir}"mk.tmp`" != ""
		Catenate "{objdir}"mk.tmp >>"{objdir}"Makefile.tem
		# An mpw-config.in might change so as not to create this anymore,
		# so get rid of it now to be safe.
		Delete -i -y "{objdir}"mk.tmp
	End If
	tr-7to8 "{srcdir}"mpw-make.in >>"{objdir}"Makefile.tem
	sed -e 's/^prefix = .*$/prefix = {mpw_prefix}/g' "{objdir}"Makefile.tem >"{objdir}"Makefile.tem2
	Delete -i -y "{objdir}"Makefile.tem
	MoveIfChange "{objdir}"Makefile.tem2 "{objdir}"Makefile
	If {verify} == 1
		Echo Created Makefile in "`Directory`"
	End If
End If

If "`Exists "{srcdir}"mpw-build.in`" != ""
	Echo "Set srcroot " {srcroot} > "{objdir}"mpw-build.tem
	Echo "Set srcdir " {srcdir} >> "{objdir}"mpw-build.tem
	Echo "Set target_canonical " {target_canonical} >> "{objdir}"mpw-build.tem
	Echo "Set prefix " {prefix} >> "{objdir}"mpw-build.tem
	tr-7to8 "{srcdir}"mpw-build.in >>"{objdir}"mpw-build.tem
	MoveIfChange "{objdir}"mpw-build.tem "{objdir}"mpw-build
	If {verify} == 1
		Echo Created mpw-build in "`Directory`"
	End If
Else
	Echo '::mpw-build {1}' >"{objdir}"mpw-build
End If

For subdir In {configdirs}
	Set savedir "`Directory`"
	If "`Exists "{srcdir}{subdir}:"`" == ""
		Echo Strange, no {subdir} in {srcdir}
		Continue
	End If
	If {verify} == 1
		Echo Configuring {subdir}...
	End If
	If "`Exists "{objdir}{subdir}:"`" == ""
		NewFolder "{objdir}{subdir}"
	End If
	SetDirectory "{objdir}{subdir}:"
	"{ThisScript}" --target "{target_canonical}" --srcdir "{srcdir}{subdir}:" --srcroot "{srcroot}" --prefix "{prefix}" {verifystr}
	SetDirectory "{savedir}"
End For

Echo "# This directory was configured as follows:" >config.new
Echo "mpw-configure --host {host_alias} --target {target_alias} --srcdir {srcdir}" >>config.new
MoveIfChange config.new config.status


SetDirectory "{savedir}"
