;$Id: psterm.pro,v 1.4 2002/09/16 20:43:56 jmspark Exp $
;-------------------------------------------------------------
;+
; NAME:
;       PSTERM
; PURPOSE:
;       Terminate postscript plotting and send plots to printer.
; CATEGORY:
; CALLING SEQUENCE:
;       psterm
; INPUTS:
; KEYWORD PARAMETERS:
;       Keywords:
;         /NOPLOT terminates without plotting.
;         /QUIET turns off psterm messages.
;         /LAST plots existing idl.ps file (usually last plot).
;         /NOSTAMP suppresses default user name and time stamp.
;         /CLEAR_COMMENT  clears comment after listing it.
;	  /VMS	The default is to print to 'lp' on a local unix printer.
;		Set /VMS if you have different printers and stuff
; OUTPUTS:
; COMMON BLOCKS:
;       ps_com
; NOTES:
; MODIFICATION HISTORY:
;       R. Sterner, 2 Aug, 1989.
;       R. Sterner, 23 May 1990 --- converted to VMS.
;       R. Sterner, 23 Jul, 1990 --- added /nostamp.
;       R. Sterner, 20 Dec, 1990 --- added comment to time stamp.
;	P. Schleger, 4. Oct, 1993 --- Added VMS option, so that the default is
;			the regular behaviour for a unix system
;
; 2002.08.29 by Sungil Park
; - Modified to accomodate 'PRINTER' device.
;
; Copyright (C) 1989, Johns Hopkins University/Applied Physics Laboratory
; This software may be used, copied, or redistributed as long as it is not
; sold and this copyright notice is reproduced on each copy made.  This
; routine is provided as is without any express or implied warranties
; whatsoever.  Other limitations apply as described in the file disclaimer.txt.
;-
;-------------------------------------------------------------

pro psterm, q, help=hlp, noplot=nop, quiet=qt, last=lst, $
	  nostamp=nostmp, clear_comment=clear, printer = printer

	common ps_com,dname,xthick,ythick,pthick,pfont,num,comm,pfile

	if keyword_set(hlp) then begin
	  print,' Terminate postscript plotting and send plots to printer.'
	  print,' psterm'
	  print,'   No arguments.  Plots are sent to the printer that'
	  print,'   was selected with psinit.  See psinit.'
	  print,' Keywords:
	  print,'   /NOPLOT terminates without plotting.'
	  print,'   /QUIET turns off psterm messages.'
	  print,'   /LAST plots existing idl.ps file (usually last plot).'
	  print,'   /NOSTAMP suppresses default user name and time stamp.'
	  print,'   /CLEAR_COMMENT  clears comment after listing it.'
	  return
	endif

	; If qflg is set, print out comments.
	qflg = not keyword_set(qt)

	; Just print out 'idl.ps' if /last.
	; if keyword_set(lst) then goto, skip

	if !d.name ne 'PS' and !d.name ne 'PRINTER' then begin
	  if qflg then print,' Not in postscript or printer mode.'
	  return
	endif

	if not keyword_set(nostmp) then begin
	   	xyouts,0,0,/dev,orient=90,size=.4,getenv('USER')+' '+$
	   		systime(0) + ' ' + comm
	endif

	case !d.name of
		'PRINTER':	device, /close_document
		'PS': device, /close
		else: message, 'Cannot be true.', /continue
	endcase

	if qflg then message, 'Output generated.', /continue

	; Close the document only. Do not plot.
	if keyword_set(nop) or keyword_set(printer) then goto, reset

	; Only when the device is 'PS'.
	if qflg then print,' Sending plot to postscript printer . . .'

	cd, current=dir			; Should work for both.
	dir = dir(0)
	IF keyword_set(VMS) THEN $
	    getsysnams,'IDL_PSPRINTERS',npr,cmds,plist $
	ELSE BEGIN
		npr=2
		case !version.os_family of
			'Windows': begin
				cmd_def = 'print '
				printer_def = 'Default Windows Printer'
			end
			else: begin
				cmd_def = 'lpr '
				printer_def = 'Default Unix Printer'
			endelse
		endcase

		cmds=[ cmd_def, 'lpr -Pspins']
		plist=[ printer_def, 'SPINS printer']
	END
	if npr le 0 then begin
	  print,' Error in psterm: check that the environmental variable'
	  print,'   or symbol IDL_PSPRINTERS names a file describing the'
	  print,'   postscript printers on the system.'
	  return
	endif
	if (num lt 0) or (num gt npr-1) then begin
	  print,' Error in psterm: selected printer number not available.'
	  return
	endif
	if pfile eq '' then begin		; Use default plot file.
        ;fname = filename(dir,'idl.ps',/nosym)	; Sys independent name.
        fname=STRCOMPRESS(dir+'idl.ps',/REMOVE_ALL)
	print,dir,fname

        fname=dir+'idl.ps'

        endif else begin			; Use specified plot file.
	  fname = pfile
	endelse

	if qflg then print, cmds[num] + ' ' + fname
	spawn, cmds(num) + ' ' + fname

	if qflg then print,' Plot sent to ' + plist(num)

reset: ; Go back to previous settings.

	set_plot, dname
	!x.thick = xthick
	!y.thick = ythick
	!p.thick = pthick
	!p.font = pfont
	if keyword_set(clear) then comm = ''

	return
end