	PRO PURGE_CDHSSTATE
;+
; Project     :	SOHO - CDS
;
; Name        :	PURGE_CDHSSTATE
;
; Purpose     :	Remove "TO BE DELETED" items from cdhsstate
;
; Category    :	Class5, Operations, Planning, Database
;
; Explanation :	Removes all instances of any entry in the database which is
;		marked "TO BE DELETED".
;
; Syntax      :	PURGE_CDHSSTATE
;
; Examples    :	
;
; Inputs      :	None.
;
; Opt. Inputs :	None.
;
; Outputs     :	None.
;
; Opt. Outputs:	None.
;
; Keywords    :	None.
;
; Calls       :	
;
; Common      :	None.
;
; Restrictions:	!PRIV must be 3 or greater
;
; Side effects:	None.
;
; Prev. Hist. :	None.
;
; History     :	Version 1, 30-Apr-1996, William Thompson, GSFC
;
; Contact     :	WTHOMPSON
;-
;
	ON_ERROR, 2
;
;  Make sure that !PRIV is set correctly.
;
	IF !PRIV LT 3 THEN MESSAGE, '!PRIV must be 3 or greater'
;
;  Make sure that one has write privilege for the sealed databases.
;
	IF NOT PRIV_ZDBASE(/SEALED) THEN MESSAGE,	$
		'You have no privilege to write into sealed databases'
;
;  Open the cdhsstate database for write access.
;
	DBOPEN, 'cdhsstate', 1
;
;  Read in the entry number, the date, the mnemonic, the comment, and the
;  parameters PNUMBER and NUMBERP for each entry.  Sort them by date in reverse
;  order (most recent first).
;
	ENTRIES = DBSORT(-1, 'date,mnemonic,pnumber,numberp',REVERSE=[1,0,0,0])
	DBEXT, ENTRIES, 'entry,date,mnemonic,pnumber,numberp,comment',	$
		ENTRY, DATE, MNEMONIC, PNUMBER, NUMBERP, COMMENT
;
;  Define an array to store whether or not the entry should be deleted.
;
	DELETE = INTARR(N_ELEMENTS(ENTRY))
;
;  For each entry, determine whether or not it should be deleted.  Ignore any
;  entries which are the same as the previous.
;
	LAST_MNEMONIC = ''
	LAST_PNUMBER  = -1
	LAST_NUMBERP  = -1
	FOR I=0,N_ELEMENTS(ENTRY)-1 DO BEGIN
	    IF (MNEMONIC(I) NE LAST_MNEMONIC) OR		$
		    (PNUMBER(I) NE LAST_PNUMBER) OR	$
		    (NUMBERP(I) NE LAST_NUMBERP) THEN BEGIN
		IF TRIM(COMMENT(I)) EQ 'TO BE DELETED' THEN BEGIN
		    PRINT, 'Deleting ' + TRIM(MNEMONIC(I)) + ', ' +	$
			    TRIM(PNUMBER(I)) + '/' + TRIM(NUMBERP(I))
		    W = WHERE((MNEMONIC EQ MNEMONIC(I)) AND	$
			    (PNUMBER EQ PNUMBER(I)) AND		$
			    (NUMBERP EQ NUMBERP(I)))
		    DELETE(W) = 1
		ENDIF
		LAST_MNEMONIC = MNEMONIC(I)
		LAST_PNUMBER  = PNUMBER(I)
		LAST_NUMBERP  = NUMBERP(I)
	    ENDIF
	ENDFOR
;
;  Delete the selected entries.
;
	IF TOTAL(DELETE) NE 0 THEN DBDELETE, ENTRY(WHERE(DELETE))
;
	DBCLOSE
	RETURN
	END
