function send( pj )
%SEND Send output file to hardcopy device.
%   The output file is in the format specified by the device argument to 
%   the PRINT command. The command used to send the file to the output device 
%   is operating system specific. The command is stored in the PrintJob object.
%
%   Ex:
%      SEND( pj );
%
%   See also PRINT, PRINTOPT.

%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.5 $  $Date: 2000/08/15 13:55:05 $

lprcmd = pj.PrintCmd;

%Does something exist to send to the printer?
if ~exist( pj.FileName )
    error('No file to send to printer.')
end

if strcmp( computer, 'PCWIN' )
    cmd = sprintf(strrep(lprcmd,'\','\\'), pj.FileName);
    if pj.DebugMode
        disp( ['PRINT debugging: print command = ''' cmd '''.'] )
    end
    [s, r] = privdos(pj,cmd);
    delete(pj.FileName);
    
else
    if strncmp( lprcmd, 'lp ', min(length(lprcmd),3) ) 
        notBSD = 1;
    else
        notBSD = 0;
    end
    if ~isempty( pj.PrinterName )
        %If user specified a printer, add it to the printing command
        if notBSD
          cmdOption = '-d';
        else
          cmdOption = '-P';
        end
        lprcmd = [ lprcmd ' ' cmdOption pj.PrinterName ];
    end
    
    if pj.DebugMode
        disp( ['PRINT debugging: print command = ''' lprcmd ' ' pj.FileName '''.'] )
    end
    dberror = disabledberror;
    try
      [s, r] = unix([lprcmd ' ' pj.FileName]);
    catch
      % Try unix with one argument if last call fails
      s = unix([lprcmd ' ' pj.FileName]);
      r = '';
    end
    enabledberror(dberror);
      
    if notBSD
        % SGI and SOL2 without Berkley printing extensions do not
        % have a 'delete when done' option, so used copy option and
        % now delete the temporary file we made.
        delete(pj.FileName)
    end
end

if s & ~isempty(r)
    error( sprintf('Problem sending file to output device, system returned error :\n%s', r) )
end
