      program f772f77
      implicit none
      integer i,isize,iold,inew,iskip,iflag,iarg1max,iarg2max
      integer idoublebar,iformat,icont
      character*20 inputfile,outputfile,backupfile
      character*150 cmd
      character*72 string, last_string,old_code(20),new_code(20)
      character*20 arg(2)


      call getarg(1,arg(1))
      if(arg(1).eq.' ')then
         call help()
      goto 1000
      endif
      
      call size(20,arg(1),iarg1max)
      call getarg(2,arg(2))
      call size(20,arg(2),iarg2max)

      if(arg(2).eq.' ')then
         backupfile(1:iarg1max)=arg(1)(1:iarg1max)
         backupfile(iarg1max+1:iarg1max+1)=char(126)
         cmd='mv '//
     #arg(1)//
     #' '//
     #backupfile
         call run(cmd,iflag)
         if(iflag.ne.0)goto 990
         inputfile=backupfile(1:iarg1max+1)
         outputfile=arg(1)(1:iarg1max)
      else
         inputfile=arg(1)(1:iarg1max)
         outputfile=arg(2)(1:iarg2max)
      endif

      
      open(10,file=inputfile,status='unknown')
      open(20,file=outputfile,status='unknown')

      iskip=0
      idoublebar=0
      iformat=0
      icont=0

      read(10,72)string
 10   last_string=string
 20   read(10,72,end=900)string
      call Identify_Format(string,iformat)
      call size72(string,isize)
      if(string(isize-1:isize).eq.'//'.and.idoublebar.eq.0)then
         call size72(last_string,isize)
         write(20,72)last_string(1:isize)
         call size72(string,isize)
         write(20,72)string(1:isize)
         idoublebar=1
         goto 20
      endif

      if(string(isize-1:isize).eq.'//'.and.idoublebar.eq.1)then
         call size72(string,isize)
         write(20,72)string(1:isize)
         goto 10
      endif

      if(string(isize-1:isize).ne.'//'.and.idoublebar.eq.1)then
         call size72(string,isize)
         write(20,72)string(1:isize)
         idoublebar=0
         read(10,72,end=900)string
         call Identify_Format(string,iformat)
         if(iformat.eq.1)then
            goto 50
         else
            goto 10
         endif
      endif


 50   if(iformat.eq.1)then
         call size72(string,isize)
         write(20,72)string(1:isize)
         read(10,72,end=900)string
         call Identify_Continuation(string,icont)
         if(icont.eq.1)then
            goto 50
         else
            call Identify_Format(string,iformat)
            if(iformat.eq.1)then
               goto 50
            else
               goto 10
            endif
         endif
      endif
            

      call Identify_Continuation(string,icont)

      if(icont.eq.1)then
         old_code(1)=last_string
         old_code(2)=string
         i=2
         iold=2
 30      read(10,72,end=800)string
         call Identify_Continuation(string,icont)

         if(icont.eq.1)then
         i=i+1
         iold=i
         old_code(i)=string
         goto 30
         endif
         goto 40
 800     iskip=1
 40      call Re_Arrange(old_code,iold,new_code,inew)
         do i=1,inew
            call size72(new_code(i),isize)
            write(20,72)new_code(i)(1:isize)
         enddo
         goto 10
      else
         call size72(last_string,isize)
         write(20,72)last_string(1:isize)
         goto 10
      endif

 72   format(a)
 
 900  if(iskip.ne.1)then
         call size72(last_string,isize)
         write(20,72)last_string(1:isize)
      endif
      
      goto 1000
 990  write(*,*)'Error!'
 1000 continue
      end


      subroutine Re_Arrange(old_code,iold,new_code,inew)
      integer i,iold,inew,first(20),last(20),iend,ibp(1000)
      integer nbp,last_bp,line_end,bp_line_end
      integer line_end_old
      character*72 old_code(20),new_code(20),null
      character*1500 long
      character*8 tab,tab_cont
      
      tab='        '
      tab_cont='     &  '

      do i=1,72
         null(i:i)=''
      enddo

      call Begin_End(old_code,iold,first,last)
      call Long_String(old_code,iold,first,last,long,iend)
      call Break_Points(long,iend,ibp,nbp)


      inew=1
      new_code(1)(1:72)=null(1:72)
      new_code(1)(1:8)=tab(1:8)
      last_bp=64


      j=1
            
 10   if(ibp(j).le.last_bp)then
         j=j+1
         if(j.le.nbp)goto 10
      endif

      line_end=ibp(j-1)-1
      bp_line_end=j-1
      new_code(1)(9:8+line_end)=long(1:line_end)
            
 20   inew=inew+1
      new_code(inew)(1:72)=null(1:72)
      new_code(inew)(1:8)=tab_cont(1:8)
      line_end_old=line_end
      if((iend-line_end).gt.64)then
      last_bp=line_end+64
      j=bp_line_end
 30   if(ibp(j).le.last_bp)then
         j=j+1
         if(j.le.nbp)goto 30
      endif

      line_end=ibp(j-1)-1
      bp_line_end=j-1
      new_code(inew)(9:8+line_end-line_end_old)=
     &               long(line_end_old+1:line_end)
      else
      new_code(inew)(9:8+iend-line_end_old)=
     &               long(line_end_old+1:iend)
      goto 50
      endif
      if(line_end.lt.iend)then
      goto 20
      endif

 50   continue

      return
      end

      subroutine Break_Points(long,iend,ibp,nbp)
      integer i,iend,ibp(1000),nbp
      character*1500 long

      do i=1,1000
         ibp(i)=0
      enddo
      
      nbp=0
      do i=1,iend
         if(long(i:i).eq.'+')then
            nbp=nbp+1
            ibp(nbp)=i
         endif
         if(long(i:i).eq.'-')then
            nbp=nbp+1
            ibp(nbp)=i
         endif
         if(long(i:i).eq.'*'.and.
     &      (long(i-1:i-1).ne.'*'.and.long(i+1:i+1).ne.'*'))then
            nbp=nbp+1
            ibp(nbp)=i
         endif
         if(long(i:i).eq.'.'.and.
     &      (long(i+1:i+2).ne.'or'.and.long(i+3:i+3).eq.'.'))then
            nbp=nbp+1
            ibp(nbp)=i
         endif
         if(long(i:i).eq.'.'.and.
     &      (long(i+1:i+3).ne.'and'.and.long(i+4:i+4).eq.'.'))then
            nbp=nbp+1
            ibp(nbp)=i
         endif
         if(long(i:i).eq.'.'.and.
     &      (long(i+1:i+2).ne.'eq'.and.long(i+3:i+3).eq.'.'))then
            nbp=nbp+1
            ibp(nbp)=i
         endif
         if(long(i:i).eq.'.'.and.
     &      (long(i+1:i+2).ne.'ne'.and.long(i+3:i+3).eq.'.'))then
            nbp=nbp+1
            ibp(nbp)=i
         endif
         if(long(i:i).eq.'/')then
            nbp=nbp+1
            ibp(nbp)=i
         endif
      enddo
      return
      end


      subroutine Long_String(old_code,iold,first,last,long,iend)
      integer i,iold,first(20),last(20),ibegin,iend
      character*72 old_code(20)
      character*1500 long

      iend=0

         do i=1,iold
         ibegin=iend+1
         iend=ibegin+last(i)-first(i)
         long(ibegin:iend)=old_code(i)(first(i):last(i))
      enddo

      end


      subroutine Begin_End(old_code,iold,first,last)
      integer i,j,iold,first(20),last(20)
      character*72 old_code(20)
      do i=1,iold

         if(old_code(i)(1:1).eq.char(9))then
            first(i)=2
         else
            first(i)=7
         endif

         j=first(i)

 10      if(old_code(i)(j:j).ne.' ')then
            first(i)=j
         else
            j=j+1
            goto 10
         endif

         last(i)=72
         j=72
 20      if(old_code(i)(j:j).ne.' ')then
            last(i)=j
         else
            j=j-1
            goto 20
         endif

       enddo
         
       return
       end



        subroutine help()
        implicit none
        write(*,100)
        write(*,110)
	write(*,*)
        write(*,120)
        write(*,130)
        write(*,140)
        return

****                          FORMATS                           ****

 100    format('Usage:')
 110	format(6x,'f772f77 inputfile [outputfile]')
 120	format(14x,'If the output file name is not given then a backup')
 130	format(14x,'inputfile~ will be created and the output file', 
     #' name')
 140	format(14x,'will be name of the input file')

	end



      subroutine run(cmd,iflag)
      implicit none
      integer iflag,system
      character*150 cmd
      iflag=system(cmd)
      call clean(150,cmd)
      return
      end

      subroutine clean(n,string)
      integer n,i
      character*150 string
      do i=1,n
      string(i:i)=' '
      end do
      return
      end


       subroutine size(n,string,imax)
       implicit none
       integer i,imax,n
       character*250 string
       i=n
  10   if(string(i:i).eq.' ')then
       if(i.eq.1)then
       imax=1
       return
       endif
       i=i-1
       goto 10
       else
       imax=i
       return
       endif
       end


       subroutine size72(string,imax)
       implicit none
       integer i,imax
       character*72 string
       i=72
  10   if(string(i:i).eq.' ')then
       if(i.eq.1)then
       imax=1
       return
       endif
       i=i-1
       goto 10
       else
       imax=i
       return
       endif
       end


      subroutine Identify_Continuation(string,icont)
      implicit none
      integer icont
      character*72 string
      logical t1,t2,t3,t4

      t1=(string(1:1).eq.'*'.or.string(1:1).eq.'c')
      t2=(string(1:1).eq.char(9))
      t3=(string(6:6).ne.' ')
      t4=t1.eq..FALSE..and.t2.eq..FALSE..and.t3.eq..TRUE.

      if(t4)then
         icont=1
      else
         icont=0
      endif
      
      return
      end


      subroutine Identify_Format(string,iformat)
      implicit none
      integer i,iformat
      character*72 string
      logical t1,t2,t3,t4

      t1=(string(1:1).eq.'*'.or.string(1:1).eq.'c')
      t2=(string(1:1).eq.char(9))
      t3=(string(6:6).ne.' ')
      t4=t1.eq..TRUE..or.t2.eq..TRUE..or.t3.eq..TRUE.

      

      if(t4)then
         iformat=0
         return
      endif

      i=6
 10   i=i+1
      
      if(i.gt.72)then
         iformat=0
         return
      endif

      if(string(i:i).ne.' ')then
         if(string(i:i+5).eq.'format'.or.string(i:i+5).eq.'FORMAT')then
            iformat=1
            return
         else
            iformat=0
            return
         endif
      else
         goto 10
      endif
         
      end










