;+
; Project     :	SOHO - CDS
;
; Name        :	INIT_CP_DB
;
; Purpose     :	Initializes databases/directories used by CPT software.
;
; Explanation :	This makes a duplicate copy of the 
;               databases used by CPT. CPT runs from the duplicate copy. 
;               The commprep database provides a mirror 
;               of what is loaded on the CDS. The series_id, raster_id, dexwin_id and vdswin_id
;               databases relate the internal CDHS ID to the planning databases IDs.
;               It allows the databases to be copied or created anew.
;               If commprep recreated then deletes all the CVT files so that CVT files
;               are a mirror of what is currently loaded.
;               If keyword set new=1 then initializes the commprep database.
;               If keyword set new=2 then also initialzies the series_id, raster_id, dexwin_id and
;               vdswin_id databases. 
;               To provide some measure of safety first copies databases before
;               initializing them into a directory labelled by date.
;               NB ZDBASE may be a list of directories to be searched for the official
;               versions of these databases.
;               The files in 'name' .dbd .dbx .dbh .dbf are the empty databases.
;               The files in 'dname' .* are the ones used by the program.
;               The files in 'rname' .* are the official current ones.
;
; Use         : <init_cp_db, new>
;
; Inputs      :	new = integer flag indicating whether to copy or create databases.
;                     0 : copy all three databases
;                     1 : create commprep database anew
;                     2 : create commprep, series_id, raster_id, dexwin_id and vdswin_id databases anew.
;
; Opt. Inputs : None.
;
; Outputs     : None.
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       :	find_with_def, dbcreate, concat_dir.
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	None.
;
; Written     :	0.0, Martin Carter, RAL, 14/10/94
;
; Modified    :	0.1, Martin Carter, RAL, 28/11/94.
;                    Removed deletion of  TTC and LTB files.
;               0.2, Martin Carter, RAL, 6/2/95
;                    Changed so that only deletes CVT files in
;                    CVT file directory rather than everything.
;               0.3  MKC, 6/3/95
;                    Added printout of files deleted.
;               0.4  MKC, 27/3/95
;                    Changed so that looks for dbd file for directory.
;               0.5  MKC, 18/4/95
;                    Split tableID database into two : seriesID and rasterID.
;                    Changed directory logic.
;               0.6  MKC, 31/5/95
;                    Changed series_ID and rasterID databases to series_id and raster_id
;               0.7, MKC, 25/9/95
;                    Added creation of local databases for commprep, series_id and raster_id
;                    to cope with possible cpt crashes. Routine now always called.
;               0.8, MKC, 28/9/95
;                    Only delete CVT files if commprep recreated.
;               0.9, MKC, 23/10/95
;                    Renamed environment variables, used concat_dir.
;               1.0, MKC 3/11/95
;                    Modified printouts. Modified so that instead of using
;                    dbcreate copies initialized database files and uses 
;                    start_db.pro .
;               1.1, MKC, 21/11/95
;                    Added dexwin and vdswin datbases
;               1.2, MKC, 24/11/95
;                    Changed so that CVT file shave .dt extension
;               1.3, MKC, 29/11/95
;                    Removed dexwin database.
;               1.4, MKC, 18/1/96
;                    Used single command for copying and moving.
;               1.5, MKC, 31/1/96
;                    Copies databases when re-initializing them.
;               1.6, MKC, 19/2/96
;                    Created dexwin_id database.
;               1.7, MKC, 23/2/96
;                    Changed so that when using new=2 creates separate old version of
;                    databases each time rather than just old_database.* .
;           
; Version     :	Version 1.7, 23/2/96
;-
;**********************************************************

PRO start_db, database, logical 

  ; find and make backup copy of database.dbd file

  file = find_with_def ( database + '.dbd', '$ZDBASE', /NOCURRENT )
    
  IF file EQ '' THEN MESSAGE, 'No ' + database + ' database descriptor file found'

  ; get directory of original database

  loc = STRPOS ( file, database + '.dbd' )

  directory = STRMID ( file, 0, loc ) ; result may be a NULL string

  ; put working copy of database into d database

  IF logical THEN BEGIN

    ; get working copy from initial files

    PRINT,'Initializing ' + database + ' database'
    PRINT, ''

    SPAWN, 'cp ' + directory + database + '.dbf ' + directory + 'd' + database + '.dbf ;' + $
           'cp ' + directory + database + '.dbx ' + directory + 'd' + database + '.dbx ;' + $
           'cp ' + directory + database + '.dbh ' + directory + 'd' + database + '.dbh'

    ; save existing r database in case of mistaken command

    PRINT,'Saving ' + database + ' database'
    PRINT, ''

    ; get current time

    get_utc, utc, /CCSDS

    SPAWN, 'cp ' + directory + 'r' + database + '.dbf ' + directory + database + '_' + utc + '.dbf ;' + $
           'cp ' + directory + 'r' + database + '.dbx ' + directory + database + '_' + utc + '.dbx ;' + $
           'cp ' + directory + 'r' + database + '.dbh ' + directory + database + '_' + utc + '.dbh'

  ENDIF ELSE BEGIN

    ; get working copy from r database 

    PRINT,'Copying ' + database + ' database'
    PRINT, ''

    SPAWN, 'cp ' + directory + 'r' + database + '.dbf ' + directory + 'd' + database + '.dbf ;' + $
           'cp ' + directory + 'r' + database + '.dbx ' + directory + 'd' + database + '.dbx ;' + $
           'cp ' + directory + 'r' + database + '.dbh ' + directory + 'd' + database + '.dbh'

  ENDELSE

END

PRO init_cp_db, new

  ; check argument defined

  IF N_ELEMENTS(new) EQ 0 THEN new = 0

  ; test whether to remove existing macro table files

  IF ( new GT 0 ) THEN BEGIN

    files =  concat_dir ( '$CDS_CP_CVTFILES_W', '*.dt' )

    PRINT,'Removing any existing macro table files'
    PRINT, ''

    SPAWN, 'rm ' + files

  ENDIF

  ; deal with databases

  start_db, 'commprep', new GT 0 

  start_db, 'series_id', new GT 1 

  start_db, 'raster_id', new GT 1

  start_db, 'dexwin_id', new GT 1

  start_db, 'vdswin_id', new GT 1

  PRINT, 'FINISHED INITIALIZATION'
  PRINT, ' '

END

