      subroutine openf
      use molcost
      IMPLICIT REAL*8(A-H,O-X,Z),logical*1(y)
      character*1280 namel
      common/proj/ prefix,ybas,ymo,ycl,ysym,yao
      character*80 prefix,file,oneint,oneint1,comfile,comfile1
     *,traone,traone1,traint,traint1,orb1,ijcl,ijcl1,info,info1
     *,mono,mono1
     *,tete(100)
      character*80 blan40,filorb
      common/pr/nprint,mprint(10),filorb,blan40
      common /filc/jrc_oneint,jrc_comfile,jrc_traone,jrc_traint
      common /name_filc/ comfile
      common/nfil/ traint1
      namelist /smufil/ prefix,oneint,comfile,traone,traint,orb1
     *,ijcl,ijcl1,info,info1
     *,mono,mono1
      common/n_fil/ n_ijcl
      character*80 n_ijcl
      common/fin/yfin
      common/version/molcas,i_rec,i_at,i_orb,i_coo,y64
      common/cprop/isx,isy,isz,yprop
      open(75,file='BID75')
      nprint=0
      blan40='                                        '
     *//'                                        '
      molcas=0
      oneint='OneInt    '
      comfile='ComFile   '
      !
      ! Test molcas6 : on regarde si runfile existe
      !
      open(99,file=trim(prefix)//'RunFile',status='old',err=1111)
      comfile='RunFile   '
      close(99)
 1111 continue
      ijcl='IJCL      '
      traone='TraOne    '
      traint='TraInt    '
      info='Info      '
      mono='Mono      '
      oneint1=blan40
      ComFile1=blan40
      traone1=blan40
      traint1=blan40
      orb1=blan40
      ijcl1=blan40
      info1=blan40
      mono1=blan40
      yprop=.false.
      isx=0
      isy=0
      isz=0
      rewind 93
      call LECNAM('&COST ',namel,93,ierr,0)
      call namela(namel,ONEINT,'ONEINT')
      call namela(namel,COMFILE,'COMFILE')
      call namela(namel,TRAONE,'TRAONE')
      call namela(namel,TRAINT,'TRAINT')
      call namela(namel,INFO,'INFO')
      call nameli(namel,NPRINT,'NPRINT',4)
      call namela(namel,MONO,'MONO')
      call namell(namel,YPROP,'YPROP',1)
      call nameli(namel,ISX,'ISX',4)
      call nameli(namel,ISY,'ISY',4)
      call nameli(namel,ISZ,'ISZ',4)

      write(75,smufil)
      call scrinam('NAMELIST COST  ')
      !
      ! verif data
      !
      if(yprop) then
         if(isx==0.or.isy==0.or.isz==0) then
            print*,'yprop=T, isx,isy,isz must be in data'
            print*,'isx,isy,isz=',isx,isy,isz
            stop
         endif
      endif
      write(6,*) ' '
      write(6,*) '=================='
      write(6,*) 'openf   open files'
      write(6,*) '=================='
c
c--   open c files
c
c     build the name of the OneInt file
c     open file
      call colle(prefix,'OneInt',file,long)
      call nomfil3(oneint1,file,jrc_oneint)
      if(jrc_oneint.eq.-1) then
         print*,'***************************'
         print*,'(File ONEINT does not exist'
         print*,'***************************'
         yfin=.false.
         yao=.false.
      endif
c
c     build the name of the Comfile file
      call colle(prefix,trim(comfile),file,long)
c     open file
      call nomfil3(comfile1,file,jrc_comfile)
      krc=c_read(jrc_comfile,i,4)
      if(i==0) then
         y64=.true.
         ifac=2
      else
         y64=.false.
         ifac=1
      endif
c
c     build the name of the TraOne file
      if(ymo) then
         call colle(prefix,'TraOne',file,long)
         call nomfil3(traone1,file,jrc_traone)
         if(jrc_traone.eq.-1) then
            print*,'(File TRAONE does not exist'
            yfin=.false.
            ymo=.false.
         endif
      endif
c
c     build the name of the TraInt file
      if(ymo.and..not.ychol) then
         call colle(prefix,'TraInt',file,long)
c        open file
         call nomfil3(traint1,file,jrc_traint)
         if(jrc_traint.eq.-1) then
            print*,'(File TRAINT does not exist'
            yfin=.false.
            ymo=.false.
         endif
      endif
c
c--   open other files
c
c
c     build the name of the IJCL file
c     open file
      file=ijcl1
      if(ymo) then
         if(ycl) then
            if(file.eq.blan40) call colle(prefix,'IJCL',file,long)
            open(72,form='formatted',file=file,status='unknown')
            print'(2a)', ' Open file ',file(1:len_trim(file))
     *      ,' (formatted)'
         else
            if(file.eq.blan40) call colle(prefix,'ijcl',file,long)
            open(72,form='unformatted',file=file,status='unknown')
            n_ijcl=file
            print'(3a)', ' Open file ',file(1:len_trim(file))
         endif
      endif
c
c     build the name of the Info file
      file=info1
      if(file.eq.blan40) call colle(prefix,'Info',file,long)
c     open file
      if(.not.yao) then
c        if File ONEINT does not exist, try to recover an old one...'
         open(8,file=file,status='old',err=100)
         print'(3a)', ' Open file ',file(1:len_trim(file)),' (old)'
         call recovinfo(file)
         goto 102
      endif
      goto 101
 100  continue
      open(2,file=file,status='new',err=100)
      print'(3a)', ' Open file ',file(1:len_trim(file)),' (new)'
      goto 102
 101  continue
      open(2,file=file,status='unknown',err=100)
      print'(3a)', ' Open file ',file(1:len_trim(file))
      write(2,1000) '============================================='
      write(2,1000) 'File: ',file(1:len_trim(file))
      write(2,1000) 'created by molcost'
      write(2,1000) 'date: '
      write(2,1000) '============================================='
 1000 format(2a)
 102  continue
c
c     open file Mono
c
      file=mono1
      if(file.eq.blan40) call colle(prefix,'Mono',file,long)
      open(3,form='unformatted',status='unknown',file=file)
      print'(2a)', ' Open file ',file(1:len_trim(file))
      write(tete(1),1000)'============================================='
      write(tete(2),1000)'File: ',file(1:len_trim(file))
      write(tete(3),1000)'created by molcost'
      write(tete(4),1000)'date: '
      write(tete(5),1000)'============================================='
      write(3) tete(1:5)
c
c--   open orbital file
c
      if(ymo) then
         filorb=blan40
         open(80,file='INPORB',status='old',err=10)
         print*,'0pen file INPORB'
         filorb='INPORB'
         goto 11
 10      continue     
         call colle(prefix,'RasOrb',file,long)
         open(80,file=file,status='old',err=12)
         print*,'0pen file ',file(1:len_trim(file))
         filorb=file
         goto 11
 12      continue     
         call colle(prefix,'ScfOrb',file,long)
         open(80,file=file,status='old',err=13)
         print*,'0pen file ',file(1:len_trim(file))
         filorb=file
         goto 11
 13      continue     
         print*,'no orbital file found: the occupation will not be'
     *   ,' written to the listing'
 11      continue     
      endif
      write(6,*)'=================='
      write(6,*)' '
      return
      end
       subroutine colle(a,b,c,ic)
      character*1 a*(*),b*(*),c*(*)
      la=len(a)
      lb=len(b)
      lc=len(c)
      do i=1,lc
         c(i:i)=' '
      enddo
      ic=0
      do ia=1,la
         if(a(ia:ia).ne.' ') then
            ic=ic+1
            if(ic.gt.lc) then
               print*,'impossible to concatenate a and b. '
               print*,'c is not long enough '
               print*,a
               print*,b
               stop
            endif
            c(ic:ic)=a(ia:ia)
         endif
      enddo
      do ib=1,lb
         if(b(ib:ib).ne.' ') then
            ic=ic+1
            if(ic.gt.lc) then
               print*,'impossible to concatenate a and b. '
               print*,'c is not long enough '
               print*,a
               print*,b
               stop
            endif
            c(ic:ic)=b(ib:ib)
         endif
      enddo
      return
      end
       subroutine nomfil3(file1,file,jrc)
      character*1 file*(*),file1*(*)
      integer c_open
      if(file1(1:1).eq.' ') file1=file
      write(75,*)'Open  file ',trim(file1),'$'
      call scril
      open(1,file=trim(file1),form='unformatted',status='old',err=1)
      close(1)
      jrc=c_open(trim(file1)//char(0))
      !print*,'jrc',jrc
c      if(file1(1:1).eq.' ') call colle(file,char(0),file1,long)
c      lf=len(file)
c      lf1=len(file1)
c      write(75,*)'Open  file ',file1(1:long-1),'$'
c      call scril
c      open(1,file=file1(1:long-1),form='unformatted',status='old',err=1)
c      close(1)
c      jrc=c_open(file1(1:long))
      return
 1    continue
      print*,'file not opened'
      jrc=-1
      return
      end
