	FUNCTION SET_KAP_VERS, DATE, VERSION, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS
;
; Name        :	SET_KAP_VERS()
;
; Purpose     :	Sets version number of last KAP file read for given date
;
; Explanation :	This procedure sets the version number of the last KAP file
;		read for a given date.
;
; Use         :	Result = SET_KAP_VERS( DATE, VERSION )
;
;		IF NOT SET_KAP_VERS( DATE, VERSION ) THEN ...
;
; Inputs      :	DATE	= The date that the user wishes to look up the version
;			  for.  This can be in any of the standard CDS time
;			  formats.
;
;		VERSION	= The version number.  This must be a non-negative
;			  integer number.
;
; Opt. Inputs :	None.
;
; Outputs     :	The result of the function is a logical value representing
;		whether or not the operation was successful, where 1 is
;		successful and 0 is unsuccessful.
;
; Opt. Outputs:	None.
;
; Keywords    :
;       ERRMSG    = If defined and passed, then any error messages will be
;                   returned to the user in this parameter rather than
;                   depending on the MESSAGE routine in IDL.  If no errors are
;                   encountered, then a null string is returned.  In order to
;                   use this feature, ERRMSG must be defined first, e.g.
;
;                       ERRMSG = ''
;                       Result = SET_KAP_VERS( ERRMSG=ERRMSG, ... )
;                       IF ERRMSG NE '' THEN ...
;
;
; Calls       :	DATATYPE, DBOPEN, DBBUILD, DBCLOSE, TRIM
;
; Common      :	None.
;
; Restrictions:	Only this routine can be used to add KAP version information to
;		the database.  Modifying the database by hand could corrupt its
;		integrity.
;
;		!PRIV must be 2 or greater to use this routine.
;
; Side effects:	None.
;
; Category    :	Planning, Databases.
;
; Prev. Hist. :	None.
;
; Written     :	William Thompson, GSFC, 24 May 1995
;
; Modified    :	Version 1, William Thompson, GSFC, 24 May 1995
;
; Version     :	Version 1, 24 May 1995
;-
;
	ON_ERROR, 2
;
;  Initialize RESULT to represent non-success.  If the routine is successful,
;  this value will be updated below.
;
	RESULT = 0
;
;  Check the number of parameters.
;
	IF N_PARAMS() NE 2 THEN BEGIN
	    MESSAGE = 'Syntax:  Result = SET_KAP_VERS( DATE, VERSION )'
	    GOTO, HANDLE_ERROR
	ENDIF
;
;  Make sure that the user has privilege to write into the database.
;
	IF !PRIV LT 2 THEN BEGIN $
	    MESSAGE = '!PRIV must be 2 or greater to write into the database'
	    GOTO, HANDLE_ERROR
	ENDIF
;
;  Check the input parameters.
;
	IF N_ELEMENTS(DATE) NE 1 THEN BEGIN
	    MESSAGE = 'DATE must be a scalar'
	    GOTO, HANDLE_ERROR
	ENDIF
;
	IF N_ELEMENTS(VERSION) NE 1 THEN BEGIN
	    MESSAGE = 'VERSION must be a scalar'
	    GOTO, HANDLE_ERROR
	END ELSE IF VERSION NE FIX(VERSION) THEN BEGIN
	    MESSAGE = 'VERSION must be an integer'
	    GOTO, HANDLE_ERROR
	ENDIF
;
;  Make sure that the version number is a short integer.
;
	VERS = FIX(VERSION)
;
;  Convert the date to internal format, and extract the Modified Julian Date.
;
	UTC = ANYTIM2UTC(DATE)
	MJD = UTC.MJD
;
;  Get the current version for the date in question, and check to make sure
;  that it is less than the new value.
;
	CUR_VERS = LAST_KAP_VERS(UTC)
	IF CUR_VERS GE VERS THEN BEGIN
	    MESSAGE = 'New version number must be larger than previous value'
	    GOTO, HANDLE_ERROR
	ENDIF
;
;  Open the database for write access.
;
	DBOPEN, 'kap_version', 1
;
;  If necessary, add a new entry.
;
	IF CUR_VERS EQ -1 THEN BEGIN
	    DBBUILD, MJD, VERSION, STATUS=STATUS
	    IF STATUS EQ 0 THEN BEGIN
		MESSAGE = 'Write to state database was not successful'
		GOTO, HANDLE_ERROR
	    ENDIF
;
;  Otherwise, find the entry for the date in question and update the version
;  number.
;
	END ELSE BEGIN
	    ENTRY = DBFIND('MJD=' + TRIM(MJD), /SILENT)
	    DBUPDATE, ENTRY(0), 'version', VERS
	ENDELSE
;
;  Signal success.
;
	RESULT = 1
	GOTO, FINISH
;
;  Error handling point.
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'SET_KAP_VERS: ' + MESSAGE $
		ELSE MESSAGE, MESSAGE, /CONTINUE
;
;  Close the database, and return whether the routine was successful or not.
;
FINISH:
	DBCLOSE
;
	RETURN, RESULT
	END
