!***********************************************************************
! This file is part of OpenMolcas.                                     *
!                                                                      *
! OpenMolcas is free software; you can redistribute it and/or modify   *
! it under the terms of the GNU Lesser General Public License, v. 2.1. *
! OpenMolcas is distributed in the hope that it will be useful, but it *
! is provided "as is" and without any express or implied warranties.   *
! For more details see the full text of the license in the file        *
! LICENSE or in <http://www.gnu.org/licenses/>.                        *
!                                                                      *
! Copyright (C) 1993, Markus P. Fuelscher                              *
!***********************************************************************

subroutine ClsFls_RASSCF()
!***********************************************************************
!                                                                      *
!     Close files.                                                     *
!                                                                      *
!----------------------------------------------------------------------*
!                                                                      *
!     written by:                                                      *
!     M.P. Fuelscher                                                   *
!     University of Lund, Sweden, 1993                                 *
!                                                                      *
!----------------------------------------------------------------------*
!                                                                      *
!     history: none                                                    *
!                                                                      *
!***********************************************************************

#ifdef _HDF5_
use mh5, only: mh5_close_file
use RASWfn, only: wfn_fileid
#endif
use general_data, only: ITERFILE, JOBIPH, JOBOLD, LUDAVID, LUINTM, LUQUNE
use Definitions, only: iwp

implicit none
integer(kind=iwp) :: iRC
logical(kind=iwp) :: DoCholesky

!----------------------------------------------------------------------*
!     Start                                                            *
!-------------------------------------- -------------------------------*
! Local print level (if any)
!---  close the JOBOLD file -------------------------------------------*
if ((JOBOLD > 0) .and. (JOBOLD /= JOBIPH)) then
  call DaClos(JOBOLD)
  JOBOLD = -1
else if (JOBOLD > 0) then
  JOBOLD = -1
end if
!---  close the JOBIPH file -------------------------------------------*
if (JOBIPH > 0) then
  call DaClos(JOBIPH)
  JOBIPH = -1
end if
#ifdef _HDF5_
if (wfn_fileid /= 0) then
  call mh5_close_file(wfn_fileid)
  wfn_fileid = 0
end if
#endif
!---  close the ORDINT file -------------------------------------------*
call DecideOnCholesky(DoCholesky)
if (.not. DoCholesky) then
  iRc = -1
  call ClsOrd(iRc)
  if (iRc /= 0) call WarningMessage(1,'Failed to close the ORDINT file.')
end if
!---  close the file carrying the transformed two-electron integrals --*
call DaClos(LUINTM)
!---  close the DAVID file carrying temporary CI and sigma vectros ----*
call DaClos(LUDAVID)
!---  open the file carrying the hessian update vectors ---------------*
call DaClos(LuQune)
!---  close the file for storage of informations on CI-iterations
close(ITERFILE)
!----------------------------------------------------------------------*
!     Exit                                                             *
!----------------------------------------------------------------------*

end subroutine ClsFls_RASSCF
