      subroutine interac
      use mod_dolo
      use mod_rotat
      character(len_datlist) AA
      character(300) LO80
      character(300) namel
      character car
      logical lex,yw,yr
      logical OK
      common/pr/nprint
      yw=.false.
      call titre('LOCAL ORBITALS#',40)
      print*,' '
      AA=' '
      dataOK=.true.
      call init_rotat(norb,natom,atom,coor)
!
!------------------------------------------------
! Open work files
! Initializations
!------------------------------------------------
!
      open(94,form='formatted',status='unknown',file='TMPAA')
      open(96,form='formatted',status='unknown',file='TMPCROC')
      open(92,form='formatted',status='unknown',file='BLABLA')
      form_dolo='(/,'' DOLO>>>>>>>  '',a)'
      form_dolo1='('' DOLO>>>>>>>  '',a)'
      form_dolo0=form_dolo
      form_dolo10=form_dolo1
      ! OLD_DATA exists?
      inquire(FILE=trim(CurrDir)//'OLD_DATA',EXIST=lex)
      iunit5=5
      if(lex) then
         print*,'found file OLD_DATA'
         iunit5=91
         open(91,file=trim(CurrDir)//'OLD_DATA',status='old')
         call list_old_data
      endif
!
!----------------------------------------
! create OLD_DATA_version
! where all input data are stored
!----------------------------------------
!
      if(interactive) then
      k=0
         do
            k=k+1
            write(AA,*) k
            aa=adjustl(aa)
            inquire(FILE=trim(CurrDir)//'OLD_DATA'//'_'
     *      //trim(aa),EXIST=lex)
            if(lex) cycle
            open(90,file=trim(CurrDir)//'OLD_DATA'//'_'
     *      //trim(aa),status='NEW')
            exit
         enddo
      endif
!
!------------------------------------------------
! General loop on data
!------------------------------------------------
!
      do
         AA=' '
         nbdat0=nbdat0+1
!
!------------------------------------------------
! Read one Data from iunit5
! iunit5==5 : interactive data
! iunit5==91 : read from OLD_DATA
!------------------------------------------------
!
         if(iunit5==5) then
!
!------------------------------------------------
! Read Data from Input File
! interactive=T : idolo is running
! interactive=F : dolo is running
! note: (iunit5==5 and interactive=F) is not possible
!------------------------------------------------
!
 5          continue
            if(interactive) then
               write(6,form_dolo) 'Enter a Data'
               write(6,form_dolo1) '...or...'
               write(6,form_dolo1) '"X": End of Program, Exit'
               write(6,form_dolo1)
     *         '"P": Verify/Modify Preceeding data'//
     *         ',  Manipulate LO''s'
               call wdata
               read(5,'(a)',err=1) lo80
               l=len_trim(lo80)
               call fnormaligo(lo80,l)
               if(lo80(1:1)=='*'.or.lo80(1:1)=='#') cycle
            else
               lo80='X'
            endif
            print*, ' '
            print*, '*************************************************'
!
!------------------------------------------------
! Data 'P': enter "modify LO's" menu
!------------------------------------------------
!
            if(lo80=='P') then
               call modifflo
               goto 5
!
!------------------------------------------------
! Data 'X': end program
!------------------------------------------------
!
            else if (lo80=='X') then
               dataok=.true.
               call fin
               if(dataok) return
               if(interactive)write(6,form_dolo1) '"X": exit anyway'
               write(6,*)'(quit Program without saving Orbitals!)'
               if(interactive)write(6,form_dolo1) 'other: continue'
               if(interactive) then
                  read(5,'(a)',err=1) lo80
               else
                  lo80='x'
               endif
               l=len_trim(lo80)
               call fnormaligo(lo80,l)
               if(lo80(1:1)=='X') return
            else if (lo80=='NOINTERAC') then
               dataok=.true.
               call fin
               if(dataok) return
               write(6,form_dolo1) 'Program ends with warnings'
               call displaywarnings(OK)
               return
            endif
! write data on 90 (next "OLD_DATA" file)
            call wdata
         else
!
!---------------------------------------------------------
! read data from file OLD_DATA (file 91) (iunit5==91)
!---------------------------------------------------------
!
            read(91,'(a)',end=4,err=1) lo80
            print*, ' '
            print*, '*************************************************'
            l=len_trim(lo80)
            call fnormaligo(lo80,l)
         endif
!
!------------------------------------------------
! At this stage, a data has been read, from OLD_DATA or not
!------------------------------------------------
!
! skip if blank or commented line
         if(lo80(1:1)=='*'.or.lo80(1:1)=='#') cycle
         if(len_trim(lo80)==0) cycle
! go to treatment of the data
         goto 2
 4       continue
!
!------------------------------------------------
! We are at the end of the OLD_DATA file
! We must now read next data on file 5
!------------------------------------------------
!
         iunit5=5
         call list_old_data
         cycle
 1       continue
         stop ' Error on file ''OLD_DATA'''
         cycle
 2       continue
!
!------------------------------------------------
! Treat the data:
! Data on several lines?
!------------------------------------------------
!
         AA=trim(AA)//lo80
         l=len_trim(AA)
         do while(AA(l:l)=='&')
            call ligne_suite5(AA,l,iunit5)
         enddo
         dat0(nbdat0)=AA
!
!------------------------------------------------
! print data line
!------------------------------------------------
!
         print'(a)',trim(AA)
!
!------------------------------------------------
! decode data line AA
!------------------------------------------------
!
         call maj(AA,l)
         if(AA(1:5)=='CORE_'.or.AA(1:6)=='SIGMA_'.or.AA(1:6)=='PI_'
     *   .or.AA(1:5)=='DIFF_')
     *   then
!
! CORE_ DIFF_ SIGMA_ PI_
!
            open(87,file='BID87')
            if((AA(1:5)=='CORE_'.or.AA(1:6)=='DIFF_')
     *      .and.index(aa,'[')==0) then
               if(index(aa,'2P')/=0)
     *         call repl(aa,'2P(',"2P[X Y Z](",yrepl)
               if(index(aa,'3D')/=0)
     *         call repl(aa,'3D(',"3D[2- 1- 0 1+ 2+](",yrepl)
               if(index(aa,'4F')/=0)
     *         call repl(aa,'4F(',"4F[3- 2- 1- 0 1+ 2+ 3+](",yrepl)
               if(index(aa,'5G')/=0)
     *         call repl(aa,'5G(',
     *         "5G[4- 3- 2- 1- 0 1+ 2+ 3+ 4+](",yrepl)
               if(index(aa,'6H')/=0)
     *         call repl(aa,'6H(',
     *         "6H[5- 4- 3- 2- 1- 0 1+ 2+ 3+ 4+ 5+](",yrepl)
               if(index(aa,'7I')/=0)
     *         call repl(aa,'7I(',
     *         "7I[6- 5- 4- 3- 2- 1- 0 1+ 2+ 3+ 4+ 5+ 6+](",yrepl)
            endif
            call croc2(aa,yecr,'BO',ncroc)
            rewind 87
            if(ncroc==0) then
               ncroc=1
               yr=.false.
            else
               yr=.true.
            endif
            rewind 87
            do icroc=1,ncroc
               if(yr)read(87,'(a300)') aa
               !print*,'aa',trim(aa)
               in_progress=AA(1:4)
               origtmp=trim(AA)
               idatlist0=idatlist0+1
               datlist0(idatlist0)=trim(AA)
               !print*,'DATLIST02',idatlist0,trim(datlist0(idatlist0))
! Call to datoto: datoto generates basic data and write them to file 94
               call datoto(aa)
! call storedat: store data of file 94 in table datlist
               call storedat(AA)
! decodedat: read individual data (O_LAB AT basis ...)
!               print*,'ideb_datlist,ifin_datlist',
!     *         ideb_datlist,ifin_datlist
               do k=ideb_datlist,ifin_datlist
                  if(datlist(k)(1:4)=='SUBS') then
                     call proj_dir(datlist(k))
                     call wdata
                  else
                     call decodedat(k,k)
                  endif
               enddo
               origtmp=' '
               in_progress=' '
            enddo
            close(87,status='delete')
         else if(AA(1:6)=='DELOC_') then
            print'(a)',trim(AA)
!
! DELOC_
!
            origtmp='DELOC_'
            idatlist0=idatlist0+1
            datlist0(idatlist0)=trim(AA)
            form_dolo='(/,'' DOLO/DELOC_>>>>>>>  '',a)'
            form_dolo1='('' DOLO/DELOC_>>>>>>>  '',a)'
            ideb=idatlist+1
            call deloc(AA)
! call storedat: store data of file 94 in table datlist
            call storedat(AA)
            ifin=idatlist
            if(yw)print*,'idatlist',idatlist
! decodedat: read individual data (O_LAB AT basis ...)
            call decodedat(ideb_datlist,ifin_datlist)
            nwk=0
            do k=fnorb,1,-1
               if(fdat1(k)<ideb) exit
               nwk=nwk+1
               iwk(nwk)=k
            enddo
            call reor(nwk,iwk)
            print*,'Orbitals generated by DELOC_ '
            call scripto6(flo(:,iwk(1:nwk)),norb,nwk,norb,nwk,orbi,6,
     *      focvir(iwk(1:nwk)),flab(iwk(1:nwk)),focc(iwk(1:nwk))
     *      ,fsym(iwk(1:nwk)),fdat1(iwk(1:nwk)),iwk(1:nwk))
         else if(AA(1:3)=='BB_') then
!
! BB_
!
            origtmp='BB_'
            idatlist0=idatlist0+1
            datlist0(idatlist0)=trim(AA)
            form_dolo='(/,'' DOLO/BB_>>>>>>>  '',a)'
            form_dolo1='('' DOLO/BB_>>>>>>>  '',a)'
            open(87,file='BID87')
            open(86,file='BID86')
            call croc2(aa,yecr,'BO',ncroc)
            rewind 87
            if(ncroc==0) then
               ncroc=1
               yr=.false.
            else
               yr=.true.
            endif
            print'(/,a)', 'bond_generated data:'
            do icroc=1,ncroc
               if(yr)read(87,'(a300)') aa
               ideb=idatlist+1
            enddo
            call bb(AA,itest)
         else if(AA(1:5)=='BOND_') then
!
! BOND_
!
            origtmp='BOND_'
            idatlist0=idatlist0+1
            datlist0(idatlist0)=trim(AA)
            form_dolo='(/,'' DOLO/BOND_>>>>>>>  '',a)'
            form_dolo1='('' DOLO/BOND_>>>>>>>  '',a)'
            open(87,file='BID87')
            open(86,file='BID86')
            call croc2(aa,yecr,'BO',ncroc)
            rewind 87
            if(ncroc==0) then
               ncroc=1
               yr=.false.
            else
               yr=.true.
            endif
            print'(/,a)', 'bond_generated data:'
            do icroc=1,ncroc
               if(yr)read(87,'(a300)') aa
               ideb=idatlist+1
! call bond: generates basic data and write them to file 94
               call bond(AA,itest)
               if(itest==1) cycle
! call storedat: store basic data of file 94 in table datlist
               call storedat(AA)
               ifin=idatlist
               !print*,'ideb,ifin',ideb,ifin
               if(yw)print*,'idatlist',idatlist
! decodedat: read individual data (O_LAB AT basis ...)
               call decodedat(ideb_datlist,ifin_datlist)
               !write(6,form_dolo) 'Display orbitals (Y/N) ?'
               !read(5,'(a1)') reponse
               !call maj(reponse,1)
               !if(reponse=='Y') then
               nwk=0
               do k=fnorb,1,-1
                  if(fdat1(k)<ideb) exit
                  nwk=nwk+1
                  iwk(nwk)=k
               enddo
               call reor(nwk,iwk)
               if(yw)
     *         print*,'Orbitals generated by BOND_ before substraction'
               if(yw)
     *         call scripto6(flo(:,iwk(1:nwk)),norb,nwk,norb,nwk,orbi,6,
     *         focvir(iwk(1:nwk)),flab(iwk(1:nwk)),focc(iwk(1:nwk))
     *         ,fsym(iwk(1:nwk)),fdat1(iwk(1:nwk)),iwk(1:nwk))
               !endif
!               rewind 94
!               do
!                  read(94,'(a)',end=10) AA
!                  print*,'bond_subs ',trim(AA)
!                  call proj_dir(AA)
!                  call wdata
!               enddo
               do k=ideb_datlist,ifin_datlist
                  if(datlist(k)(1:4)=='SUBS') then
                     !print*,'bond_subs ',trim(datlist(k))
                     call proj_dir(datlist(k))
                     call wdata
                  endif
               enddo
 10            continue
               nwk=0
               do k=fnorb,1,-1
                  if(fdat1(k)<ideb) exit
                  nwk=nwk+1
                  iwk(nwk)=k
               enddo
               call reor(nwk,iwk)
               if(yw)
     *         print*,'Orbitals generated by BOND_ after substraction'
               if(yw)
     *         call scripto6(flo(:,iwk(1:nwk)),norb,nwk,norb,nwk,orbi,6,
     *         focvir(iwk(1:nwk)),flab(iwk(1:nwk)),focc(iwk(1:nwk))
     *         ,fsym(iwk(1:nwk)),fdat1(iwk(1:nwk)),iwk(1:nwk))
               call bond2(ideb,ifin)
               nwk=0
               do k=fnorb,1,-1
                  if(fdat1(k)<ideb) exit
                  nwk=nwk+1
                  iwk(nwk)=k
               enddo
               call reor(nwk,iwk)
               if(interactive.or.nprint>0)
     *         print*,'Orbitals generated by BOND_: renamed'
               if(interactive.or.nprint>0)
     *         call scripto6(flo(:,iwk(1:nwk)),norb,nwk,norb,nwk,orbi,6,
     *         focvir(iwk(1:nwk)),flab(iwk(1:nwk)),focc(iwk(1:nwk))
     *         ,fsym(iwk(1:nwk)),fdat1(iwk(1:nwk)),iwk(1:nwk))
               origtmp=' '
               form_dolo='(/,'' DOLO/>>>>>>>  '',a)'
               form_dolo1='('' DOLO/>>>>>>>  '',a)'
            enddo
            close(87,status='delete')
            close(86,status='delete')
         else if(AA(1:4)=='ROT_') then
            call rotat1(AA)
            idatlist0=idatlist0+1
            datlist0(idatlist0)=trim(AA)
            call wdata
         else if(AA(2:2)=='_') then
            idatlist0=idatlist0+1
            datlist0(idatlist0)=trim(AA)
            !print*,'DATLIST04',idatlist0,trim(datlist0(idatlist0))
            idatlist=idatlist+1
            datlist(idatlist)=AA
            call decodedat(idatlist,idatlist)
         else if(AA(1:4)=='SUBS') then
            idatlist0=idatlist0+1
            datlist0(idatlist0)=trim(AA)
            !print*,'DATLIST0',idatlist0,trim(datlist0(idatlist0))
            idatlist=idatlist+1
            datlist(idatlist)=AA
            call proj_dir(AA)
         else if(AA(1:3)=='REN') then
            idatlist0=idatlist0+1
            datlist0(idatlist0)=trim(AA)
            idatlist=idatlist+1
            datlist(idatlist)=AA
            dataok=.true.
            call renamelo_dir(AA)
            if(dataok) call wdata
         else if(AA(1:2)=='CD') then
            dataok=.true.
            call canceldata(AA)
            if(dataok) call wdata
         else if(AA(1:)=='C') then
            dataok=.true.
            call cancello(AA)
            if(dataok) call wdata
         else if(AA(1:1)=='>') then
!            idatlist0=idatlist0+1
!            datlist0(idatlist0)=trim(AA)
!            idatlist=idatlist+1
!            datlist(idatlist)=AA
            call changecat3(aa)
            if(dataok) call wdata
            if(dataok) print*,'dataok'
         else
            print*,trim(AA)
            write(6,form_dolo) 'wrong data'
         endif
!         print*,'DATLIST0'
!         do k=1,idatlist0
!            print*,trim(datlist0(k))
!         enddo
      enddo
      call scripto(flo,norb,norb,norb,norb,orbi)
      return
      end
       subroutine list_old_data
      use mod_dolo
      character(80) lo80
!      print*,' '
!      print*,'=======================OLD_DATA======================='
!      rewind 91
!       k=0
!       do
!          k=k+1
!          read(91,'(a80)',end=3), lo80
!          print'(2x,a)',trim(lo80)
!       enddo
!  3    continue
!       rewind 91
!      print*,'======================================================'
      return
      end
