function print(arg1,arg2,arg3,arg4,arg5)
%PRINT  Print graph or save graph to file.
%       PRINT <filename> saves the current Figure window as PostScript
%       or a printer specific format, as specified by PRINTOPT. If
%       a filename is specified, the output is written to the designated file,
%       overwriting it if it already exists. If the specified filename does not
%       include an extension, an appropriate one is appended.
%       If the filename is omitted, the Figure is sent directly to the
%       printer as specified in PRINTOPT.
%
%       PRINT <filename> -f<system name> prints the specified SIMULINK
%       	system.
%
%       Syntax: PRINT [ -ddevice] [ -options ] <filename>
%
%       Available Windows device options are:
%          -dwin    - Send figure to currently installed printer in monochrome
%          -dwinc   - Send figure to currently installed printer in color
%          -dmeta   - Send figure to clipboard in Metafile format
%          -dbitmap - Send figure to clipboard in bitmap format
%          -dsetup  - Bring up Print Setup dialog box, but do not print
%
%       Other Windows options are:
%          -v       - Verbose mode, bring up the Print dialog box
%                     which is normally suppressed.
%
%       Available Postscript devices are:
%          -dps    - PostScript for black and white printers
%          -dpsc   - PostScript for color printers
%          -dps2   - Level 2 PostScript for black and white printers
%          -dpsc2  - Level 2 PostScript for color printers
%
%          -deps   - Encapsulated PostScript (EPSF)
%          -depsc  - Encapsulated Color PostScript (EPSF)
%          -deps2  - Encapsulated Level 2 PostScript (EPSF)
%          -depsc2 - Encapsulated Level 2 Color PostScript (EPSF)
%
%       Additional GhostScript devices are:
%          -dlaserjet - HP LaserJet
%          -dljetplus - HP LaserJet+
%          -dljet2p   - HP LaserJet IIP
%          -dljet3    - HP LaserJet III
%          -dcdeskjet - HP DeskJet 500C with 1 bit/pixel color
%          -dcdjcolor - HP DeskJet 500C with 24 bit/pixel color and
%                           high-quality color (Floyd-Steinberg) dithering
%          -dcdjmono  - HP DeskJet 500C printing black only
%          -ddeskjet  - HP DeskJet and DeskJet Plus
%          -dpaintjet - HP PaintJet color printer
%          -dpjetxl   - HP PaintJet XL color printer
%          -dbj10e    - Canon BubbleJet BJ10e
%          -dln03     - DEC LN03 printer
%          -depson    - Epson-compatible dot matrix printers (9- or 24-pin)
%          -deps9high - Epson-compatible 9-pin, interleaved lines 
%                          (triple resolution)
%          -depsonc   - Epson LQ-2550 and Fujitsu 3400/2400/1200 color printers
%          -dgif8     - 8-bit color GIF file format
%          -dpcx16    - Older color PCX file format (EGA/VGA, 16-color)
%          -dpcx256   - Newer color PCX file format (256-color)
%
%       Other options are:
%          -append   - Append the graph to file, rather than overwriting
%          -epsi     - Add 1-bit deep bitmap preview (EPSI format)
%          -Pprinter - Specfiy the printer to use
%          -fhandle  - Handle Graphics handle of figure to print
%          -fname    - Name of SIMULINK system window to print
%
%       See also PRINTOPT, ORIENT.

%       Modified 1 March 93
%       Copyright (c) 1984-93 by The MathWorks, Inc.

%
% ---------------------------------------------- User modifiable values.

%maximum for width or height of preview window, in pixels
maxPrevSize = 256; 

% ---------------------------------------------- End user modifiable values.
%

%
% List of all supported devices used for input validation.
%
% The first column contains the device name, the second column contains the
% default filename extension, the third column indicates what type of output
% device is employed, and the fourth indicates Monochrome or Color device.
%
device_table = [
%Postscript device options
	'ps      '      'ps '   'PS'	'M' 
	'psc     '      'ps '   'PS'	'C'
	'ps2     '      'ps '   'PS'	'M'
	'ps2c    '      'ps '   'PS'	'C'
	'psc2    '      'ps '   'PS'	'C'
	'eps     '      'eps'   'EP'	'M'
	'epsc    '      'eps'   'EP'	'C'
	'eps2    '      'eps'   'EP'	'M'
	'eps2c   '      'eps'   'EP'	'C'
	'epsc2   '      'esp'   'EP'	'C'
%GhostScript device options
	'laserjet'      'jet'   'GS'	'M'
	'ljetplus'      'jet'   'GS'	'M'
	'ljet2p  '      'jet'   'GS'	'M'
	'ljet3   '      'jet'   'GS'	'M'
	'cdeskjet'      'jet'   'GS'	'C'
	'cdjcolor'      'jet'   'GS'	'C'
	'cdjmono '      'jet'   'GS'	'M'
	'deskjet '      'jet'   'GS'	'M'
	'paintjet'      'jet'   'GS'	'C'
	'pjetxl  '      'jet'   'GS'	'C'
	'bj10e   '      'jet'   'GS'	'M'
	'ln03    '      'ln3'   'GS'	'M'
	'epson   '      'ep '   'GS'	'M'
	'eps9high'      'ep '   'GS'	'M'
	'epsonc  '      'ep '   'GS'	'C'
	'gif8    '      'gif'   'GS'	'C'
	'pcx16   '      'pcx'   'GS'	'C'
	'pcx256  '      'pcx'   'GS'	'C'
% Microsoft Windows-specific options
	'win     '      '   '   'MW'	'M'
	'winc    '      '   '   'MW'	'C'
	'meta    '      '   '   'MW'	'C'
	'bitmap  '      '   '   'MW'	'C'
	'setup   '      '   '   'MW'	'M'
];

options = [
	'v     '
	'epsi  '
	'append'
];

devices = device_table(:, 1:8);
extensions = device_table(:, 9:11);
classes = device_table(:, 12:13);
colorDevs = device_table(:, 14 );

comp = computer;
ghostScriptDevice = [];
printplot = 0;
num_opt_args = 0;
filename = [];
printer = [];
hasPreview = 0;
prtSim = 0;
window = get( 0, 'Children' );
dev = [];

[lprcmd, defaultDevice ] = printopt;

for i=1:nargin
	cur_arg = eval(['arg', num2str(i)]);
	if (cur_arg(1) ~= '-')
		if isempty(filename)
			filename = cur_arg;
		else
			error( [ 'Multiple inputs that look like filenames: ''' ...
					filename ''' and ''' cur_arg '''' ] );
		end

	elseif (cur_arg(2) == 'd')
		%
		% verify device given is supported, and only one given
		% device proper starts after '-d', if only '-d'
		% we echo out possible choices
		%
		if ~isempty( dev )
			error( [ 'Multiple inputs that look like device names: ''' ...
					dev ''' and ''' cur_arg ''''] );
		end

		match = 0;
		wasError = 0;
		if ( size(cur_arg, 2) > 2 )
			wasError = 1;
			% Find index of device in table, used a lot later on.
			for devIndex = 1:size(devices,1)
				if strcmp( cur_arg(3:size(cur_arg,2)), ...
							deblank(devices(devIndex,:)) )
					dev = deblank( cur_arg );
					break;
				end
			end
			if isempty(dev)
				disp('Illegal device specified.');
			end
		end
		if isempty(dev)
			disp('Supported devices are:')
			if (comp(1:2) =='PC')
				disp(devices);
			else
				% Find first non PC specific device
				for i = size( classes, 1 ) : -1 : 1
					if ~strcmp( classes(i,:), 'MW' )
						break;
					end
				end
				disp( devices( 1 : i, : ) )
			end
			if wasError
				error(' ');
			else
				return;
			end
		end
	elseif (cur_arg(2) == 'P')
		printer = cur_arg(3:length(cur_arg));
		lprcmd = [lprcmd,' -P',printer];

	elseif (cur_arg(2) == 'f')
		if ( exist('open_system') ~= 5 )
			error('SIMULINK is not available in this version of MATLAB.');
		end
		window = cur_arg( 3:length(cur_arg) );
		%SIMULINK model names are alphanumeric
		if ~( all( window > 47 ) & all( window < 58 ) )
			prtSim = 1;
			if ( exist( window ) == 4 )
				openedSimWindow = 0;
			else
				eval( window );
				openedSimWindow = 1;
			end
		else
			window = eval( window );
		end

	else
		%
		% verify any given options are supported
		%
		match = 0;
		if size(cur_arg,2) > 1
			op = cur_arg(2:size(cur_arg,2));
			for r = 1:size(options,1)
				c = min(size(op,2), size(options(r,:),2));
				if strcmp(op(1:c), options(r,1:c))
					if ( strcmp( op, 'epsi' ) )
						hasPreview = 1;
						%
						% HARDCOPY code actually wants to see 'preview'.
						%
						cur_arg = '-preview';
					end
					match = 1;
					break;
				end
			end
		end
		if ~match
			error(['Illegal option ''' op ''' given.'])
		end
		num_opt_args = num_opt_args + 1;
		eval(['opt_arg', num2str(num_opt_args), ' = cur_arg;']);
	end
end

% If no device given, use default from PRINTOPT
if isempty( dev )

	% Find device name in list of devices to set the
	% appropriate filename extension.
	for devIndex = 1 : size(devices,1)
		%skip '-d'
		d = defaultDevice( 3 : size(defaultDevice,2) );
		if strcmp( devices(devIndex, 1 : size(d,2)), d )
			dev = defaultDevice;
	   		break;
		end
	end
	if isempty( dev )
		error(['PRINTOPT specifies an unknown device type '''...
			defaultDevice ''''])
	end
end

devClass = classes(devIndex, :);
if ( devClass == 'GS' )
	%
	% Remember actual device, get MATLAB to produce PostScript for later
	% conversion by GhostScript
	%
	ghostScriptDevice = deblank(devices(devIndex,:));
	if ( colorDevs(devIndex ) == 'C' )
		dev = '-dpsc';
	else
		dev = '-dps';
	end
end

extension = deblank(extensions( devIndex, : ));

if ~prtSim
	%window is array of handles of all figure windows
	if isempty( window )
		error( 'No figure window to print' );
	else
		window = window(1);
	end
 
	% Create Handle Graphics objects on screen if not already there;
	% but, if going to change colors and bring it to front
	% for EPSI preview, do not drawnow now, but after color changes.
	if ~( hasPreview & ...
		(strcmp( 'on', get(window,'InvertHardcopy') ) ...
		| colorDevs( devIndex ) == 'M' ) )
	        drawnow
	end
end

if isempty(filename)
	%EPS files shouldn't go to printer, generate file on disk
	if ( devClass == 'EP' )
		if prtSim
			filename = window;
		else
			filename = [ 'figure' int2str( window ) ];
		end
		tellUserFilename = 1;
	else
		% Generate a unique name for temporary file
		filename = tempname;
		if (devClass ~= 'MW')
			printplot = 1;
		end
	end
else
	tellUserFilename = 0;

	% Hack, if user specifies a filename while device is -dwin
	% or -dwinc, either because the user gave that device or, more
	% likely, its the default, and since the filename is useless
	% with Windows driver anyway, we'll assume the user really wants
	% a PostScript file. This is because 'print foo' is easier
	% to type then 'print -dps foo' and probably more commonly
	% meant if a filename is given.
	if (devClass == 'MW') & (strcmp( dev, '-dwin' ) | strcmp( dev, '-dwinc' ))
		if ( colorDevs(devIndex ) == 'C' )
			dev = '-dpsc';
		else
			dev = '-dps';
		end
		extension = 'ps';
	end
end

% Append appropriate extension to filename if it doesn't have
% one, and we've determined a good one.
if ~any(filename == '.') & ~isempty( extension )
	filename = [ filename '.' extension ];
end

if tellUserFilename
	disp( 'Encapsulated PostScript files can not be sent to printer.' );
	disp( [ 'File saved to disk under name ''' filename '''.' ] );
end

if (strcmp('setup', dev(3:size(dev,2))))
    hardcopy( window , filename, dev)
    return
end

if ~prtSim
	% Invert B&W color properties of Figure and child objects
	if strcmp( 'on', get(window,'InvertHardcopy') )
		invertedColor = 1;
		cinvert( window );
	else
		invertedColor = 0;
	end

	% if not color, set lines and text to a color contrasting background
    if ( colorDevs(devIndex ) == 'M' )
		lineTextColors = blt( window, get(window,'color') );
	else
		lineTextColors = [];
	end

	% If including preview, and changed any of the colors, update figure
	% raise it to top of stacking order for screen capture.
	% Resize the figure if its larger then the biggest preview allowed
	% while maintaining its current aspect ratio.
	if hasPreview
		%
		% Get current size info in pixel coordinates.
		%
		origUnits = get( window, 'units' );
		set( window, 'units', 'Pixels' );
		frect = get( window,'pos');
		fullW = frect(3);
		fullH = frect(4);

		if fullW > maxPrevSize | fullH > maxPrevSize
			%
			% One dimension needs to shrink more then the other.
			%
			scale = min( [ maxPrevSize/fullW  maxPrevSize/fullH ] );
			w = fix( fullW * scale );
			h = fix( fullH * scale );
			set( window, 'Position', [ frect(1) frect(2)+fullH w h ] )
		end

		% Bring figure to front
		figure( window );

		% Render Handle Graphics objects in new state for EPSI preview.
		drawnow

	end % hasPreview
end     % ~prtSim

if ( devClass == 'GS' )
	orig_filename = filename;
	filename = [tempname '.ps'];
end

if (num_opt_args == 0)
	hardcopy( window, filename, dev)
elseif (num_opt_args == 1)
	hardcopy( window, filename, dev, opt_arg1)
elseif (num_opt_args == 2)
	hardcopy( window, filename, dev, opt_arg1, opt_arg2)
elseif (num_opt_args == 3)
	hardcopy( window, filename, dev, opt_arg1, opt_arg2, opt_arg3)
elseif (num_opt_args == 4)
	hardcopy( window, filename, dev, opt_arg1, opt_arg2, opt_arg3, opt_arg4)
end


if ~prtSim
	% set color of lines and text back to what they were
	if ~isempty( lineTextColors )
		blt( window, get(window,'color'), lineTextColors);
	end

	% Invert back the W&B color properties of Figure and child objects
	if invertedColor
		cinvert( window );
	end

	% Reconstruct figure the way it was.
	if hasPreview 

		% Reconstruct figure the way it was.
		if w ~= fullW | h ~= fullH
			set( window, 'Position', frect )
		end
		set( window, 'units', origUnits );

		if w ~= fullW | h ~= fullH | invertedColor | ~isempty(lineTextColors)
			drawnow;
		end

	elseif ((invertedColor | ~isempty( lineTextColors )) & ~strcmp(dev, '-dbitmap'))
		%
		% Discard all the object invalidations that occured as a result of
		% changing colors. All objects are back to their previous state,
		% but they don't know that.
		%
		drawnow('discard')
	end % hasPreview

else
	if ( openedSimWindow )
		eval('close_system( window );');
	end
end % ~prtSim

if ( devClass == 'GS' )
	rsp_file = [tempname '.rsp'];
	rsp_fid = fopen (rsp_file, 'w');
	if (rsp_fid < 0)
		error('Unable to create response file')
	end
	fprintf(rsp_fid, '-dNOPAUSE -q \n');
	fprintf(rsp_fid, '-I%s/ghostscript/ps_files\n', matlabroot);
	fprintf(rsp_fid, '-I%s/ghostscript/fonts\n', matlabroot);
	fprintf(rsp_fid, '-sDEVICE=%s\n', ghostScriptDevice);
	fprintf(rsp_fid, '-sOutputFile=%s\n', orig_filename );

	fclose(rsp_fid);
	if (comp(1:2) =='PC')
		dos( [ matlabroot '\ghostscr\bin\gs386 @' rsp_file ' ' filename ' < NUL |' ] );
	else    
		status = unix( [ matlabroot '/ghostscript/bin/' getenv('ARCH') '/gs @' rsp_file ' '...
			 filename ' < /dev/null > /dev/null' ] );
		if (status)
			error('GhostScript returned nonzero error status')
		end
	end

	delete(rsp_file)
	delete(filename)
	filename = orig_filename;
end

if (printplot)
	if (comp(1:2) == 'PC')
		if (strcmp(lprcmd(1:4), 'COPY'))
			cmd = sprintf(lprcmd, filename);
			dos(cmd);
		else
			dos([lprcmd ' ' filename]);
		end
	else
		unix([lprcmd ' ' filename]);
	end
end


