#	CreateMake - create a program makefile
#
#	Usage:
#		CreateMake ([ -Application] [ -c creator ] [-sym on] | -Tool [-sym on] |
#                   -DA | -CR -m mainEntryPoint -rt resourceType [ -t fileType ]
#                   [ -c creator ] [-sym on] | -SIOW [ -c creator] [ -sym on ])
#			        [ -mc68020 | -mc68881 | -elems881] program file…
#
#	Script CreateMake creates a simple MakeFile for building the specified
#	program.  The program parameter is the name of the program.  Makefile
#	<program>.make is created.  The default program type is Application.
#	The program may be written in any combination of Assembly Language, C, C++
#	Pascal, and/or Rez.	The list of files may include both source in
#	these languages and library object files that don't appear below.
#
#	CreateMake links the program with the following set of MPW Libraries:
#
#		Inside Macintosh Interfaces
#			{Libraries}Interface.o	
#	
#		Runtime support - one of the following:
#			{Libraries}Stubs.o			# building a tool
#			{Libraries}Runtime.o 		# no C object files
#
#		C Libraries - if any source is in C
#			{CLibraries}StdCLib.o
#			{CLibraries}CSANELib.o
#			{CLibraries}Math.o
#
#		C++ Libraries - if any source is in C++
#			{Clibraries}Streams.o		
#			{Clibraries}CPlusLib.o
#			{CLibraries}StdCLib.o
#			{CLibraries}CSANELib.o
#			{CLibraries}Math.o
#
#		Pascal Libraries - if any source is in Pascal
#			{PLibraries}PasLib.o
#			{PLibraries}SANELib.o
#
#		For tools:
#			{Libraries}ToolLibs.o
#
#		For desk accessories:
#			{Libraries}DRVRRuntime.o
#
#
#	Copyright Apple Computer, Inc. 1987 - 1992
#	All rights reserved.


Set Exit 0
Set CaseSensitive 0
Set type Application
Set creator '????'
Set runtime '{Libraries}Runtime.o'
Set interface '{Libraries}Interface.o'
Set cFloatLib '{CLibraries}CSANELib.o {CLibraries}Math.o #{CLibraries}Complex.o'
Set cplusFloatLib '{CLibraries}CSANELib.o {CLibraries}Math.o ∂
						{CLibraries}CplusLib.o #{CLibraries}Complex.o'
Set pFloatLib '{PLibraries}SANELib.o'						
Unset sources clibs plibs toollibs DRVRRuntime resource objects program suffix typ
Unset cSeen tSeen rtSeen mSeen multipleType typeSpec CPls CLang symarg symseen compileOptions
Unset hardwardFloat cCompileOptions pCompileOptions multipleHWOptions 

#	Collect the script parameters.
Loop
	Break if {#} == 0
	If "{1}" =~ /-application/
		Set type Application
		Unset toollibs
		If {typeSpec} == 0
			Set typeSpec 1
		Else
			Set multipleType 1
		End
		shift 1
		continue
	Else If "{1}" =~ /-tool/
		Set type Tool
		Set toollibs '{Libraries}ToolLibs.o'
		If {typeSpec} == 0
			Set typeSpec 1
		Else
			Set multipleType 1
		End
		shift 1
		continue
	Else If "{1}" =~ /-da/
		Set type DA
		Unset toollibs
		If {typeSpec} == 0
			Set typeSpec 1
		Else
			Set multipleType 1
		End
		shift 1
		continue
	Else If "{1}" =~ /-cr/
		Set type CR
		Unset toollibs
		If {typeSpec} == 0
			Set typeSpec 1
		Else
			Set multipleType 1
		End
		shift 1 
		continue
	Else If "{1}" =~ /-siow/
		Set type SIOW
		If {typeSpec} == 0
			Set typeSpec 1
		Else
			Set multipleType 1
		End
		shift 1
		continue
	Else If "{1}" =~ /-mc68020/
		If "{cCompileOptions}" == ""
			set cCompileOptions "-mc68020 "
			set pCompileOptions "-mc68020 "
		Else
			set multipleHWOptions 1
		End
		shift 1
		continue
	Else If "{1}" =~ /-mc68881/
		If "{cCompileOptions}" == ""
			set cCompileOptions "-mc68020 -mc68881 "
			set pCompileOptions "-mc68020 -mc68881 "
			set hardwareFloat 1
		Else
			set multipleHWOptions 1	
		End
		shift 1
		continue
	Else If "{1}" =~ /-elems881/
		If "{cCompileOptions}" == ""
			set cCompileOptions "-mc68020 -mc68881 -elems881 "
			set pCompileOptions "-mc68020 -mc68881 -d elems881=true "
			set hardwareFloat 1
		Else
			set multipleHWOptions 1
		End
		shift 1
		continue
	Else If "{1}" =~ /-c/
		set creator "{2}"
		set cseen 1
		shift 2
		continue
	Else If "{1}" =~ /-t/
		set typ "{2}"
		set tseen 1
		shift 2
		continue
	Else If "{1}" =~ /-rt/
		set rtype "{2}"
		set rtseen 1
		shift 2
		continue
	Else If "{1}" =~ /-m/
		set mentry "{2}"
		set mseen 1
		shift 2
		continue
	Else If "{1}" =~ /-sym/ 
		set symarg {2}
		set symseen 1
		shift 2
		continue
	Else If ("{1}" =~ /≈.[acpr]/ || "{1}" =~ /≈.cp/)
		Set sources "{sources}'{1}' "
		shift 1
		continue
	Else If "{1}" =~ /≈.[o]/
		Set objects "{objects}'{1}' "
		shift 1
		continue
	Else If "{program}" == "" && "{1}" !~ /-≈/
		Set program "{1}"
		shift 1
		continue 
	Else If "{1}" =~ /-≈/
		Echo "### {0} - ∂"{1}∂" is not an option." > Dev:StdErr
		Echo "# Usage - {0} ([ -Application] [ -c creator ] [-sym on] | -Tool [-sym on] | ∂n ∂
                    -DA | -CR -m mainEntryPoint -rt resourceType [ -t fileType ] ∂n ∂
                    [ -c creator ] [-sym on] | -SIOW [ -sym on ]) ∂n ∂
                    [ -mc68020 | -mc68881 | -elems881] program file…" > Dev:StdErr
		Exit 1
	Else
		Echo "### {0} - Files must end in .a, .c, .p, .cp, .r, or .o." > Dev:StdErr
		Echo "# Usage - {0} ([ -Application] [ -c creator ] [-sym on] | -Tool [-sym on] | ∂n ∂
                    -DA | -CR -m mainEntryPoint -rt resourceType [ -t fileType ] ∂n ∂
                    [ -c creator ] [-sym on] | -SIOW [ -sym on ]) ∂n ∂
                    [ -mc68020 | -mc68881 | -elems881] program file…" > Dev:StdErr
		Exit 1
	End
End
If {type} == DA && {symseen}
	Echo "### {0} - Option -sym not supported for desk accessory." > Dev:StdErr
	Echo "# Usage - {0} ([ -Application] [ -c creator ] [-sym on] | -Tool [-sym on] | ∂n ∂
                    -DA | -CR -m mainEntryPoint -rt resourceType [ -t fileType ] ∂n ∂
                    [ -c creator ] [-sym on] | -SIOW [ -sym on ]) ∂n ∂
                    [ -mc68020 | -mc68881 | -elems881] program file…" > Dev:StdErr
	Exit 1
End
If {type} == CR && ("{rtype}" == "" || "{mentry}" == "")
	Echo "### {0} - Options -rt and -m are required for code resource." > Dev:StdErr
	Echo "# Usage - {0} ([ -Application] [ -c creator ] [-sym on] | -Tool [-sym on] | ∂n ∂
                    -DA | -CR -m mainEntryPoint -rt resourceType [ -t fileType ] ∂n ∂
                    [ -c creator ] [-sym on] | -SIOW [ -sym on ]) ∂n ∂
                    [ -mc68020 | -mc68881 | -elems881] program file…" > Dev:StdErr
	Exit 1
End
If "{program}" == ""
	Echo "### {0} - A program name is required." > Dev:StdErr
	Echo "### {0} - Don't include the .a, .c, .cp, or .p suffix." > Dev:StdErr
	Echo "# Usage - {0} ([ -Application] [ -c creator ] [-sym on] | -Tool [-sym on] | ∂n ∂
                    -DA | -CR -m mainEntryPoint -rt resourceType [ -t fileType ] ∂n ∂
                    [ -c creator ] [-sym on] | -SIOW [ -sym on ]) ∂n ∂
                    [ -mc68020 | -mc68881 | -elems881] program file…" > Dev:StdErr
	Exit 1
End
If "{sources}" == ""
	Echo "### {0} - A source file is required." > Dev:StdErr
	Echo "# Usage - {0} ([ -Application] [ -c creator ] [-sym on] | -Tool [-sym on] | ∂n ∂
                    -DA | -CR -m mainEntryPoint -rt resourceType [ -t fileType ] ∂n ∂
                    [ -c creator ] [-sym on] | -SIOW [ -sym on ]) ∂n ∂
                    [ -mc68020 | -mc68881 | -elems881] program file…" > Dev:StdErr
	Exit 1
End
If {multipleType} != 0
	Echo "### {0} - Only one of Application, DA, Tool, or CR may be specified." > Dev:StdErr
	Echo "# Usage - {0} ([ -Application] [ -c creator ] [-sym on] | -Tool [-sym on] | ∂n ∂
                    -DA | -CR -m mainEntryPoint -rt resourceType [ -t fileType ] ∂n ∂
                    [ -c creator ] [-sym on] | -SIOW [ -sym on ]) ∂n ∂
                    [ -mc68020 | -mc68881 | -elems881] program file…" > Dev:StdErr
	Exit 1
End
If {multipleHWOptions} != 0
	Echo "### {0} - Only one of -mc68020, -mc68881, or -elems881 may be specified." > Dev:StdErr
	Echo "# Usage - {0} ([ -Application] [ -c creator ] [-sym on] | -Tool [-sym on] | ∂n ∂
                    -DA | -CR -m mainEntryPoint -rt resourceType [ -t fileType ] ∂n ∂
                    [ -c creator ] [-sym on] | -SIOW [ -sym on ]) ∂n ∂
                    [ -mc68020 | -mc68881 | -elems881] program file…" > Dev:StdErr
	Exit 1
End

If {cSeen} != 0 && ({type} == Tool || {type} == DA || {type} == SIOW)
		Echo "### {0} - Warning: Option -c ignored for {type}." > Dev:StdErr
End
If {tseen} != 0 && {type} != CR
		Echo "### {0} - Warning: Option -t ignored for {type}." > Dev:StdErr
End
If {rtseen} != 0 && {type} != CR
		Echo "### {0} - Warning: Option -rt ignored for {type}." > Dev:StdErr
End
If {mseen} != 0 && {type} != CR
		Echo "### {0} - Warning: Option -m ignored for {type}." > Dev:StdErr
End
If {type} == Tool
	Set options "-d -c 'MPS ' -t 'MPST'"
Else If {type} == Application
	Set options " -t 'APPL' -c '{creator}'"
Else If {type} == SIOW
	Set options "-d -c '????' -t 'APPL'"
End

#	Create the heading at the top of the makefile.

Begin
	Echo -n '#   File:       '; Quote "{program}".make
	Echo -n '#   Target:     '; Quote "{program}"
	If "{sources}" =~ /≈«1,80»/
		Echo -n '#   Sources:    '; Quote  {sources}
	Else
		Set heading '#   Sources:    '
		For i In {sources}
			Echo -n "{heading}"
			Quote "{i}"
			Set heading '#               '
		End
	End
	Echo -n '#   Created:    '; Date
	Echo
	Echo

#	Create list of implicit objects

	For file in {sources}
		If "{file}" =~ /≈.[acp]/ || "{file}" =~ /≈.cp/
			Set objects "{objects} '{file}.o' "
		End
	End

# generate OBJECTS definitions

	quote -n OBJECTS =
	If "{objects}" =~ /?«1,80»/
		Echo -n " "; Quote {objects}
	Else
		For i In {objects}
			Echo ' ∂'
			Echo -n ∂t∂t; Quote -n "{i}"
		End
		Echo
	End
	Echo

#  Set up library definitions and link options	

	If {symseen} == 1
		Set compileOptions "-sym {symarg}"
	End
	
	If {hardwareFloat} == 1
		Set cFloatLib '{CLibraries}Clib881.o {CLibraries}CSANELib881.o {CLibraries}Math881.o ∂
							#{CLibraries}Complex881.o'
		Set cplusFloatLib '{CLibraries}Clib881.o {CLibraries}CSANELib881.o {CLibraries}Math881.o ∂
						{CLibraries}CplusLib881.o #{CLibraries}Complex881.o'
		Set pFloatLib '{PLibraries}SANELib881.o'
 	End
	Echo
	
	For file in {sources}
		If "{file}" =~ /≈.c/
			Set CLang 1
		Else If "{file}" =~ /≈.cp/
			Set CPls 1
 		Else If "{file}" =~ /≈.p/
			Set plibs "{pFloatLib} ∂{PLibraries∂}PasLib.o"
		End
	End
	
	If {CPls} == 1
		 Set clibs "{cplusFloatLib} ∂{CLibraries∂}StdCLib.o" 
	Else If {CLang} == 1
		Set  clibs "{cFloatLib} ∂{CLibraries∂}StdClib.o" 
	End
	
	If {type} == Tool
		set runtime "∂{Libraries∂}Stubs.o {runtime}"
	End
	
	If {type} == SIOW
		set runtime "∂{Libraries∂}SIOW.o {runtime}"
	End
	
	For file in {sources}
			If "{file}" =~ /≈.r/
				Set resource "{file}"
			End
	End
	
	# Generate rez call

For file in {sources}
		If "{file}" =~ /≈.r/
			If {type} == DA
				Quote "{program}" ƒ "{program}".make "{program}".DRVW "{file}"
				Echo -n ∂t; Quote Rez -rd -c DMOV -t DFIL -d NOASM_BUILD "{file}" -o "{program}"
			Else 
				Quote "{program}" ƒƒ "{program}".make "{file}"
				Echo -n ∂t; Quote Rez "{file}" -append -o "{program}"
				If {type} == CR
					Echo -n ∂t; Quote SetFile -a B "{program}"
					Echo -n ∂t; Quote Duplicate -y "{program}" "{SystemFolder}"
				End
			End
		End
	End
	Echo

	If {type} == DA
		If "{resource}" == ""
			Set options "-t '????' -c '????' -rt DRVW=12 -sg '{program}'"
		Else
			Set options "-t '????' -c '????' -rt DRVW=0 -sg '{program}'"
		End
		Set DRVRruntime '{Libraries}DRVRRuntime.o'
		Quote -n "{program}".DRVW ƒ "{program}".make
		Echo ∂ ∂{OBJECTS∂}
		Set suffix '.DRVW'
	Else If {type} == CR
		Quote -n "{program}" ƒƒ "{program}".make
		Echo ∂ ∂{OBJECTS∂}
		If {typ} == 0
			set typ '????'
		End
		Set options "-t '{typ}' -c '{creator}' -rt '{rtype}' -m '{mentry}' ∂
		   -sg '{program}'"
	Else
		Quote -n "{program}" ƒƒ "{program}".make
		Echo ∂ ∂{OBJECTS∂}
	End

#	Generate Link rule.
	
	If {symseen} == 1
		Set options "{options} -sym {symarg} -mf"
	End
	Echo -n ∂t; Quote -n Link {options}; Echo ' ∂'
	If {type} == DA
		Echo -n ∂t∂t
		Echo -n {DRVRRuntime}; Echo ' ∂'
	End
	Echo -n ∂t∂t; Echo -n ∂{OBJECTS∂}; Echo ' ∂'
	For i In {clibs} {runtime} {interface} {plibs} {toollibs}
		If "{i}" =~ /(≈)®1(∂{≈∂})®2(≈)®3/	# contains {}s ?
			Echo -n ∂t∂t; Echo "{®1}∂"{®2}∂"{®3}" ∂∂
		Else
			Echo -n ∂t∂t; Quote -n "{i}"; Echo ' ∂'
		End
	End
	Echo -n ∂t∂t; Quote -o "{program}{suffix}"
	
#	Generate Asm, C, C++, Pascal, and Rez rules.

	For file in {sources}
		If "{file}" =~ /≈.a/
			Quote "{file}".o ƒ "{program}".make "{file}"
			Echo -n ∂t Asm "{compileOptions} "; Quote "{file}"
		Else If "{file}" =~ /≈.c/ 
			Quote "{file}".o ƒ "{program}".make "{file}"
			Echo -n ∂t C  -r "{compileOptions} {cCompileOptions}"; Quote "{file}"
		Else If "{file}" =~ /≈.cp/
			Quote "{file}".o ƒ "{program}".make "{file}"
			Echo -n ∂t CPlus "{compileOptions} {cCompileOptions}"; Quote "{file}"
		Else If "{file}" =~ /≈.p/
			Quote "{file}".o ƒ "{program}".make "{file}"
			Echo -n ∂t Pascal "{compileOptions} {pCompileOptions}"; Quote "{file}"
		End
	End
	If {type} == SIOW
		Quote -n "{program}"; Echo ' 'ƒƒ ∂"∂{RIncludes∂}∂"SIOW.r
		Echo -n ∂t; Echo Rez -a ∂"∂{RIncludes∂}∂"SIOW.r -o "{program}"
	End

End > "{program}".make
Echo "{program}"





