! This function returns true or false depending on the string you give as a parameter
! Its purpose is to centralize the debugging flags into only one external file
! so that debugging can be turned on or off easily at runtime.
! (basically as it was done in ADF...
! except that my version is much more basic (much much much more !) )

      function valid(string) result (isValid)

      use Io_module

      IMPLICIT NONE

      interface
         subroutine upcase(string)
           character(*), intent(inout) :: string
         end subroutine upcase
      end interface

      CHARACTER(*), intent(in) :: string
      logical                  :: isValid
      INTEGER(4), PARAMETER :: NMax=500

      CHARACTER(64),save :: List(NMax)
      LOGICAL,save       :: Value(NMax)
      LOGICAL,save       :: TInit=.False.
      CHARACTER(132) :: LINE, ValTMP, LString
      LOGICAL       :: ValueTMP, Found
      LOGICAL,SAVE       :: Tall=.False., FValidFile, ValAll
      INTEGER(4) :: ios, i,j,k
      INTEGER(4), SAVE ::  NEntries

      IF (.not.TInit) THEN
         NEntries=0
         TInit=.TRUE.
         INQUIRE(FILE=DebugFile,EXIST=FValidFile)
         if (FValidFile) THEN
            OPEN(IOTMP,File=DebugFile,IOSTAT=ios)

! The file exists so we will read it !
            DO While (ios==0)
               READ(IOTMP,'(A)',ERR=999,END=999) LINE
!               WRITE(*,*) 'LINE READ',trim(Line)
               if (ios==0) THEN
                  Line=AdjustL(Line)
!               WRITE(*,*) 'LINE ADJ',trim(Line)
                  call Upcase(Line)
!               WRITE(*,*) 'LINE UP',trim(Line)
                  if (Line(1:1)/='#') THEN
! This is not a comment, we go on
                     i=index(Line,' ')
! We take the first word, it will be our value
                     ValTMP=Line(1:i-1)
                     j=len_trim(line)
                     Line=adjustL((Line(i+1:j)))
!                     WRITE(*,*) 'VALID INIT ',NEntries,trim(ValTMP),
!     &   ' - ',trim(Line)
                     if (ValTMP(1:1)=='.') ValTmp=ValTMP(2:)
                     if (ValTMP(1:1)=='F') ValueTMP=.False.
                     if (ValTMP(1:1)=='T') ValueTMP=.True.
                     if (Line(1:1)=='*') THEN
                        ValAll=ValueTMP
                        TAll=.True.
                     ELSE
                        if (Tall) ValueTMP=ValAll
                        NEntries=NEntries+1
                        Value(NEntries)=ValueTMP
                        Line=AdjustL(Line)
                        List(NEntries)=Line(1:64)
                     END IF
                  END IF
               END IF
            END DO
 999     CONTINUE
         CLOSE(IOTMP)
!         WRITE(*,*) "DBG Valid, " // Trim(DebugFile) // "read, with "
!    &    ,NEntries
!         DO I=1,NEntries
!            WRITE(*,*) List(I),Value(I)
!         END DO
       END IF
      END IF

      IF (.NOT.FValidFile) THEN
         isValid=.FALSE.
         Return
      END IF

! The INIT file has been read !
      LString=trim(adjustL(String))
      Call UPcase(LString)
      Found=.false.
      Do I=1,NEntries
         if (trim(List(I)).eq.trim(LString)) THEN
            Found=.TRUE.
            isValid=Value(I)
!            WRITE(*,*) 'zoeurezoiru'
         END IF
!         WRITE(*,*) I,' : ',trim(list(I)),' - ',trim(LSTRING),' *'
      END DO

!      WRITE(*,*) 'Found:',Found,'isval',isvalid
!      WRITE(*,*) 'Debug is ',isvalid," for ",Trim(LString)

      if (.NOT.Found) THEN
         IF (Tall) THEN
            WRITE(*,*) 'The option ',String, &
                 ' is not recognized'
            WRITE(*,*) 'Assigning value ',ValAll
            isValid=ValAll
         ELSE
            isValid=.FALSE.
         END IF
         NEntries=Nentries+1
         List(Nentries)=Trim(LString)
         Value(Nentries)=Valall
      END IF

      END function valid
