#
#	BuildIWEm
#
#   This scripts strips all unnecessary characters from the IWEm source file
#   using the following steps:
#
#	8 Removing comments
#	7 Replacing	tabs with spaces
#	6 Removing duplicate spaces
#	5 Removing spaces next to carriage returns
#	4 Removing spaces next to special characters
#	3 Removing carriage returns next to special characters
#	2 Removing	duplicate carriage returns
#	1 Inserting	PostScript identifiers
#
# Ver 1.1 and prior     Networking Group
#
# Ver 1.2   September 23, 1988       Suki Lee
#     Added step #3 to reduce the size of the file.
#           December 19, 1988        Suki Lee
#     Added -d option to duplicate statement so the new file is not read-only.
#     SCM cannot build since the IWEm.src file is read-only from Projector.
# Ver 1.3   May 23, 1989			 Suki Lee
#     Removed standard file call and sys beep at the end.
# Ver 1.4	June 6, 1991			Wes Johnson
#		Added Removing carriage returns next to special characters
#		Made all changes repetitive until no more occurences of pattern.
# Ver 1.5	Feb 6, 1992				Wes Johnson
#	Problem:
#		Due to some changes in the IIgs the imaging area was reduced
#		to allow grayscale to operate in its minimal configuration.
#		This causes a side-effect of characters on the outside edges
#		to be lost.
#	Bug fix:
#		When the IWEmDict is invoked it will execute the letter command.
#		The letter command should cause most printers to default to the
#		standard letter size imaging area.
#	Note:
#		This fix will not help the LaserWriter IIf with 2 meg (minimum config)
#		The only solution is to upgrade it to 5 meg, then it will work like the IIg.
#

unalias
set echo 0
set exit 0
set casesensitive 0
set file ""
set exitstatus 0
		set file "IWEm.src"
		set newfile "IWEm"
		echo "## BuildIWEm (Ver 1.5) - `date -a`";
		echo
		
		# Make sure this window is closed so we can duplicate to it.
		close -n "{newfile}" > Dev:Null  Dev:Null	
		
		echo "## Duplicating '{file}' to '{newfile}'."
		
		duplicate -y -d {file} "{newfile}"
		set mystatus {status}
		if ({mystatus} == 2)
			Begin
				echo "# Can't Duplicate file: '{file}'  - Usage Err";
				echo "# Usage - BuildIWEm [File]";
				exit {mystatus}
			End
		else
			Begin
				echo "## Opening file '{newfile}'";
				open "{newfile}"
				echo "## Squeezing '{newfile}'"
				echo

				target "{newfile}"
								
				echo "#	8 Removing	comments"		
					find 
					Replace -c  /%Ŷn/ " "
		
				echo "#	7 Replacing	tabs with spaces"
					Loop
					find 
					Replace -c  /t/ " "
						Break If {Status} !=0;
					End
		
				echo "#	6 Removing	duplicate spaces"
					Loop
						find 
						Replace -c  /"  "/ " "
						Break If {Status} !=0;
					End
		
				echo "#	5 Removing	spaces next to carriage returns"
					Loop
					find 
					Replace -c  /n / " "
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  / n/ n
						Break If {Status} !=0;
					End
		
				echo "#	4 Removing spaces next to special characters"
					Loop
					find 
					Replace -c  /" ("/ "("
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /") "/ ")"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /" <"/ "<"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"< "/ "<"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /" >"/ ">"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"> "/ ">"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /" ["/ "["
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"[ "/ "["
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /" ]"/ "]"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"] "/ "]"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /" {"/ "{"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"{ "/ "{"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /" }"/ "}"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"} "/ "}"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /" /"/ "/"
						Break If {Status} !=0;
					End
#					Loop
#					find 
#					Replace -c  /"/ "/ "/"
#						Break If {Status} !=0;
#					End
					
				echo "#	3 Removing carriage returns next to special characters"
					Loop
					find 
					Replace -c  /")n"/ ")"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"n)"/ ")"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"n("/ "("
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /">n"/ ">"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"n>"/ ">"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"<n"/ "<"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"n<"/ "<"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"[n"/ "["
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"n["/ "["
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"]n"/ "]"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"n]"/ "]"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"}n"/ "}"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"n}"/ "}"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"{n"/ "{"
						Break If {Status} !=0;
					End
					Loop
					find 
					Replace -c  /"n{"/ "{"
						Break If {Status} !=0;
					End
#					Loop
#					find 
#					Replace -c  /"n/"/ "/"
#						Break If {Status} !=0;
#					End
					
				echo "#	2 Removing	duplicate carriage returns"
					Loop
						find 
						Replace -c  /nn/ n
						Break If {Status} !=0;
					End
		
				echo "#	1 Inserting	PostScript identifiers" 
					find 
					replace -c 1  "%!PS-Adobe-2.0n%%BeginProcSet "IWEM"n"
					replace -c 1 /setfont / "setfontn%%Begin ExitServern"
					find 
					replace -c 1  "nn%%End ExitServern%%EndProcSet n"

				echo
				echo "## Saving file '{newfile}'";
				close -y "{newfile}"
				echo
				echo "## BuildIWEm Done - `date -a`";
			End
End
Exit {exitstatus}
