# Build
# By Bryan Atsatt
#
# Syntax:	Build [options] name [name [name]]
#
# Description:	This script will Make the specified makefile from the {MakeFiles} directory
# 		and execute its output.  It expects the filenames in the {MakeFiles} directory
#		to be in the format "make.name" where "name" is the name passed to Build.
#
# Options:	-l	Generate a listing in {Listings} directory. Forces -e option.
#		-o	Open makeout file so you can see what Make outputs.
#		-e	Force Make to rebuild everything.
#		-p	Write progress information to standard output.
#		-s	Suppress update dialog.	
#
# All of the Make options which do not have two parts (i.e. "-f makefile") can also
# be passed as options.
#
# The following names can be used in place of "os";  these are especially useful when creating
# listings because they will only list the named module:
#
#	scm, bank0, devmgr, devdisp, cache, init, gquit, loader
#
# If you type "all" for the name, all of the makefiles in the {MakeFiles} directory will be
# processed.   
#

#
# Check for zero parameters case...
#
if !{#}
	echo "### Build - Filename required." >> dev:StdOut
	exit 0
end

#
# Parse parameters as long as this is not a recursive call...
#
if !{MakeAllFlag}

	unset MakeParm
	unset MakeAllFlag
	unset FilesList
	unset ListFlag
	unset ListPartFlag
	unset AsmOptions
	unset LnkOptions
	unset WindowFlag
	unset SkipUpdate
	unset DoProgress
	
	set OSfiles "scm bank0 devmgr devdisp cache init gquit loader" 
	for Item in {Parameters}
		Begin
		if "{Item}" =~ /-l/ 
			set ListFlag "-l"
			set MakeParm "{MakeParm}-e "
		else if "{Item}" =~ /-o/
			open -n {WorkFolder}makeout > dev:null  dev:null
			set WindowFlag 1
		else if "{Item}" =~ /-s/
			set SkipUpdate 1
		else if "{Item}" =~ /-p/
			set DoProgress 1
		else if "{Item}" =~ /all/
			set MakeAllFlag 1 
		else if "{Item}" =~ /-?/
			set MakeParm "{MakeParm}{Item} "
		else
			set FilesList "{FilesList} {Item}"
		end
		set Parameters "{FilesList}"
		end
	end
	export	MakeAllFlag
	export	MakeParm
	export	ListFlag
	export	OSfiles
	export	DoProgress
	export	SkipUpdate
#
# If the {GSOS} variable is not set then we must set it as well as all the other variables
# required by our makefiles...
#
	if !{GSOS}

		directory ::
		set GSOS `directory`
		directory {GSOS}MakeFiles > dev:null  dev:null || 
		(loop
			alert "Please set the directory to GSOS..."
			directory `getfilename -d`
			set GSOS `directory`
			directory {GSOS}MakeFiles > dev:null  dev:null && break
			continue
		end)
		export GSOS
		directory {GSOS}Scripts
		execute {GSOS}Scripts:GSOSvariables
		set Commands "{Commands},{Scripts}"
		export Commands

		if !{SkipUpdate}
			set exit 0
			confirm -t 'Do you want to update your UserStartup script for GS/OS?'
			set Answer {status}
			if {Answer} == 5
				exit 0
			else if !{Answer} 
				search -q "# Set up for GS/OS project..." {MPW}UserStartup > dev:null  dev:null
				if {status}
					begin
						echo
						echo '#'
						echo '# Set up for GS/OS project...'
						echo '#'
						echo 'set GSOS '`echo {GSOS}`' ;export GSOS'
						echo 'execute {GSOS}Scripts:GSOSstartup'
					end >> {MPW}UserStartup
					alert 'Your UserStartup script has been updated for GS/OS...'
				else
					alert 'Your UserStartup script has already been updated, so no changes were made...'
				end
			end
		end
		set exit 1
	end

#
# If one of the parameters is "all", we will do a recursive call with all of
# the filenames in the MakeFiles directory...
#
	if {MakeAllFlag}
		set CurrentDir `directory`
		directory {MakeFiles}
		execute {Scripts}build `files make.`
		directory {CurrentDir}
		exit 0
	end
end

#
# Set up some variables, do a Make and execute its output for each file requested...
#
for File in {Parameters}

	if {DoProgress}
		echo "Making "{File}"..."
	end
	if "{File}" !~ /make./
		set Prefix "make."
	else
		unset Prefix	
	end

	if "{ListFlag}"
		set AsmOptions "-l -m -lo {Listings}{File}.lst -pagesize 70"
		Export AsmOptions
		set LnkOptions "-l > {Listings}{File}.map"
		Export LnkOptions
	end

	set exit 0
	begin
		if {DoProgress}
			echo "set echo 1"
		end
		make {MakeParm} -f "{MakeFiles}{Prefix}{File}"
	end	> "{WorkFolder}makeout"  dev:null || 
		begin
			set exit 1
			for Item in {OSfiles}
				if "{File}"  == "{Item}"
					set File "os"
					if "{ListFlag}"
						unset ListFlag
						set ListPartFlag "{Item}"
						export ListPartFlag
					end
					break
				end
			end
			begin
				if {DoProgress}
					echo "set echo 1"
				end
				make {MakeParm} -f "{MakeFiles}{Prefix}{File}"
			end	> "{WorkFolder}makeout"  dev:null
		end
		set exit 1
	
	{WorkFolder}makeout
end

if {DoProgress}
	echo "Done."
end

if {WindowFlag}

	target {WorkFolder}makeout > dev:null  dev:null
end
