 subroutine hupdate_all(n, ds, dgrad, Hess)

  use Path_module, only :  hesupd,Hinv
  use Io_module, only : IoOut

  IMPLICIT NONE

  INTEGER, PARAMETER :: KINT=KIND(1)
  INTEGER, PARAMETER :: KREAL=KIND(1.0D0)

  INTEGER(KINT), INTENT(IN) :: n
  real(KREAL), INTENT(IN) :: ds(n), dgrad(n)
  REAL(KREAL),INTENT(INOUT) :: Hess(n:n)

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! This subroutine is a drive for the update of the Hessian 
! in the Quasi-Newton optimization step.
! We propose mostly update of the inverse Hessian.
!
! input:
!  - n: number of coordinates
!  - ds: GeomOld-Geom (expressed in the optimization coord.)
!  - dgrad: GradOld-Grad  (expressed in the optimization coord.)
!
!  input/output:
!  - Hess: old Hessian in input, replaced by updated Hessian on output
!
!!!!!!!!!!!!!!!!
!
! For now, we propose :
! - For update of inverser Hessian:
!      - BFGS
!
! - For update of hessian:
!      - Bofill (combination of Murtagh-Sargent and Powell-symmetric-Broyden
!      - Murtagh-Sargent
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



  interface
     function valid(string) result (isValid)
       logical                  :: isValid
       character(*), intent(in) :: string
     end function valid

  end interface

  ! ======================================================================
  LOGICAL :: Debug

  ! ======================================================================

  debug = valid ('DEBUG HESS') .OR. Valid('DEBUG HUPDATE')

  if (debug) WRITE(*,*) "================================= Entering Hupdate_all ==============="

  SELECT CASE (Hinv)
     CASE (.TRUE.)
        SELECT CASE (HesUpd)
           CASE ("BFGS")
              Call Hinvup_BFGS(n,ds,dgrad,hess)
           CASE DEFAULT
              WRITE(*,*) Trim(HesUpd) // " not known: using BFGS"
              Call Hinvup_BFGS(n,ds,dgrad,hess)
         END SELECT
     CASE (.FALSE.)
        SELECT CASE (HesUpd)
           CASE ("BOFILL")
              Call Hupdate_Bofill(n,ds,dgrad,hess)
           CASE ("MS")
              Call Hupdate_MS(n,ds,dgrad,hess)
           CASE DEFAULT
              WRITE(*,*) Trim(HesUpd) // " not known: using Bofill"
              Call Hupdate_Bofill(n,ds,dgrad,hess)
           END SELECT
    END SELECT

end subroutine hupdate_all
