! **********************************************************************
! Program Version Number
!
! format of version no. is version.release with release no. in the 2dp
! Version numbers start at 12 to provide compatibility with the
! Heidelberg MCTDH package as it is based on version 11.0 of that code.
!
! e.g. 12.01 is version 1 release 1 and
!      14.06 will be version 3 release 6  etc.
!
! patchlevel is used to define a set of patch files distributed to
! add to a distribution.
!
! progname    : name of program ("$0")
! *                                                                    *
! * paths is a module generated during the installation with the       *
! *       paths for inout files etc.                                   *
! * cdate is a module that is generated during compilation with        *
! *       the date and time of compilation                             *
! *                                                                    *
! **********************************************************************

!***********************************************************************
! CDATE contains:
! dateline = character string with time and date of compilation
! incsurf = character string with list of included PESs
! gitrev = character string with GIT version no. 
! parcode = 0 serial
!           1 OMP parallelised
!           2 MPI parallelised
!***********************************************************************

      module versions

use cdate
use paths

      private

      real(kind=8), parameter :: projver = 13.01d0
      integer, parameter      :: patchlevel=0
      real(kind=8), save      :: progver

      character(len=1024), save :: progname
      integer, save             :: lprogname

      public :: progname, progver, projver, lprogname
      public :: wrversion, getrevno

      contains

! **********************************************************************
! *           SUBROUTINE WRVERSION                                     *
! *                                                                    *
! * WRVERSION writes the Program version and the compile time to a     *
! * file or to screen.                                                 *
! *                                                                    *
! **********************************************************************
      subroutine wrversion(unit)

      implicit none

      integer             :: version,release,i,ierror
      integer, intent(in) :: unit
      real(kind=8)        :: tiny = 1.0d-9

      call mygetarg(0,progname)
      lprogname=index(progname,' ')-1
      write(unit,'(/,2a)') 'Program name: ', progname(1:lprogname)

!-----------------------------------------------------------------------
! Get revision no. and write version to log file
!-----------------------------------------------------------------------
      progver=projver
      progver=progver+tiny
      call getrevno(progver,version,release)

      write(unit,'(/,80a1)') ('-',i=1,80)
      write(unit,'(a)') '****** Package version ******'
      write(unit,'(/,a,i5)') 'Program Version : ', version
      write(unit,'(  a,i5)') 'Release         : ', release
      if (patchlevel.gt.0) then
        write(unit,'(a,i5)') 'Patch level     : ', patchlevel
      endif
      write(unit,*)
      write(unit,*)'GIT version: '//gitrev

      call rempath(progname,lprogname,ierror)
      if(progname(1:8) .eq. 'quantics')  then
         write(unit,'(/,a80)',err=666) incsurf
         i=index(oppathdef,' ')-1
         if(i .le. 55) then
            write(unit,'(/a,a55)') 'Default operator-path: ',oppathdef
         else
            write(unit,'(/a,/,a)') 'Default operator-path: ',oppathdef
         endif  
      elseif(progname(1:6) .eq. 'potfit')  then
         write(unit,'(/,a80)',err=666) incsurf
      endif
      write(unit,'(80a1)') ('-',i=1,80)

      write(unit,'(/,a80)',err=666) dateline
      if (parcode .eq. 0) then
         write(unit,*) 'Serial Code'
      else if (parcode .eq. 1) then
         write(unit,*) 'OpenMP parallelisation'
      else if (parcode .eq. 2) then
         write(unit,*) 'MPI parallelisation'
      endif

      write(unit,*)

      return

!-----------------------------------------------------------------------
! Output error message
!-----------------------------------------------------------------------
 666  continue
      write(6,'(a)') ' Routine : wrversion'
      write(6,'(a,i3)') ' Error in writing versions-info to unit',unit
      stop

      end subroutine wrversion

!**********************************************************************
!
! GETREVNO
!
! from the program version number, extract the release number
!
!#######################################################################

      subroutine getrevno(progver,version,release)

      implicit none

      integer,intent(out)     ::version,release
      real(kind=8),intent(in) ::progver
      real(kind=8)            ::minver

! progver 12 is version 1 to retain compatibilty with the HD MCTDH package
      version = int(progver) - 11

      minver=real(progver - int(progver))
      minver=minver*100
      release=int(minver)

      return
      end subroutine getrevno


      end module versions



! Wrapper to call wrversion module from a F77

      subroutine wrversionf77(unit)

      use versions

      integer, intent(in) :: unit

      call wrversion(unit)

      end subroutine wrversionf77

