!23456789a123456789b123456789c123456789d123456789e123456789f123456789g12
!
! Copyright D.J. Jeffery, 2015jan01.
!
! A fortran-95 program to create report forms from laboratory exercises. 
! The compile command is f95 report.f.
! The run command is a.out. 
!
!-----------------------------------------------------------------------
!
      program report
      implicit none
!
      character :: afile_in*180     ! Input laboratory exercise.
      character :: afile_out*180    ! Output laboratory exercise. 
!
      integer :: i,j,k,l,m,n
      integer :: ians
      integer :: icopy
      integer :: ipre
      integer :: itell
!
      character :: atmp*512
      character :: record*512
!
      print*,'Input the laboratory exercise name.'
      read(*,'(a)') afile_in 
      afile_in=adjustl(afile_in)              ! To eliminate leading blanks.
      open(unit=1,file=afile_in,status='old',action='read')
      afile_out=afile_in(1:len_trim(afile_in)-4)//'_report.php'
      write(*,'(a,1x,a)') afile_in(1:len_trim(afile_in)),                          &
     &                    afile_out(1:len_trim(afile_out))
!      write(*,'(2i5)') len_trim(afile_in),len_trim(afile_out)
      open(unit=2,file=afile_out,status='unknown',action='write')
      open(unit=3,file='../devos/'//afile_out,status='unknown',                    &
     &     action='write')
!      
      icopy=0
      itell=0           ! itell goes to 1, when the program is writing out tasks.
      do
        read(1,'(a)',end=100) record
        atmp=adjustl(record)
        if(atmp(1:10) .eq. '<! ISTART:') then
            icopy=1
            if(itell .eq. 1) then
               write(2,'(/a/)') '<li>'      ! Numbering for the tasks. 
               write(3,'(/a/)') '<li>'      ! Numbering for the tasks. 
            end if
          else if(atmp(1:8) .eq. '<! IEND:') then
            icopy=0
            if(itell .eq. 0) then           ! This for the preamble before the tasks. 
               itell=1
               write(2,'(a/a//a)')                                                 &
     &          '<h1>Name:</h1>',                                                  &
     &          '<h1>Partner Names:</h1><br><br>','</p>'
               write(2,'(/a/)') '<ol>'
               write(3,'(a/a//a)')                                                 &
     &          '<h1>Name:</h1>',                                                  &
     &          '<h1>Partner Names:</h1><br><br>','</p>'
               write(3,'(/a/)') '<ol>'
            end if
        end if
!        write(*,'(i5,1x,a)') icopy,record(1:len_trim(record))
        if(icopy .eq. 1) then
           i=index(record,'<li>')  ! Eliminates non-task <li>'s.  Task uses <li >'s.
           if(i .eq. 0) then
              atmp=record(1:len_trim(record))
            else
              atmp=record(i+4:len_trim(record))
           end if
!           write(*,'(i5,1x,a)') icopy,atmp
!           write(*,'(i5,1x,a)') icopy,atmp(1:len_trim(atmp))
           write(2,'(a)') atmp(1:len_trim(atmp))
           ipre=index(record,'<! Answer pre')
           ians=index(record,'<! Answer:')
           if(ians .ne. 0) ipre=0
           if(ipre .eq. 0) then
              if(ians .ne. 0) atmp=record(ians+2:len_trim(record))
              write(3,'(a)')  atmp(1:len_trim(atmp))
           end if
         end if
      end do
!
  100 continue
      close(unit=3)
      close(unit=2)
      close(unit=1)
!
      end program report
!
!23456789a123456789b123456789c123456789d123456789e123456789f123456789g12
