c FLOG - A FORTRAN weblog
c
c I am a programmer, but in order to keep with FORTRAN tradition, I
c pretended not to be one while writing this program.

      program flog
      character qstr*100
      character mode*6
      integer eid
      integer page
c     Length of a string
      integer l
c     End query string parse
      logical endp

      mode = 'main'
      eid = -1
      page = 1

c Get the query string, and jump to the appropriate function depending
c on the mode.
      call getenv('QUERY_STRING', qstr)
      l = length(qstr)
      i = 1
      endp = .false.
   10 n = index(qstr(i:), '&')
      if (n .eq. 0) then
          n = l + 1
          endp = .true.
      else
          n = i + n - 1
      end if
      m = index(qstr(i:n-1), '=')
      if (m .ne. 0) then
          m = i + m - 1
          if (qstr(i:m-1) .eq. 'mode') then
              mode = qstr(m+1:n-1)
          else if (qstr(i:m-1) .eq. 'eid') then
              read(qstr(m+1:n-1), '(I8)') eid
          else if (qstr(i:m-1) .eq. 'page') then
              read(qstr(m+1:n-1), '(I4)') page
          end if
      end if
      if (endp) goto 20
      i = n+1
      goto 10

   20 continue
c     write(*,*) mode, eid
      if (mode .eq. 'main') then
          call printp(page)
      else if (mode .eq. 'read') then
          call header()
          call htmls('read entry')
          call enread(eid)
          call htmle()
      end if
      end

c PRINTP - Display a page (ten entries) from a start position
      subroutine printp(page)
      integer page
      integer entries(10)

      call getes((page-1) * 10 + 1, entries)
      call header()
      call htmls('FLOG')
      call fprint('intro')
      do 100, I=1,10
          if (entries(I) .ne. 0) call enread(entries(I))
  100 continue
      call htmle()
      end

c ENREAD - read and print a blog entry
      subroutine enread(eid)
      integer eid
      character fname*20
      character buf*256
      character title*100
      character date*30
      integer l

      write(buf, '(I8)') eid
      do 200, I=1,8
          if (buf(I:I) .ne. ' ') then
              fname = 'entry/' // buf(I:8) // '.entry'
              goto 210
          end if
  200 continue

  210 open(UNIT=1, STATUS='OLD', FILE=fname, ERR=299)
      read(1, '(A)') title
      read(1, '(A)') date
      call entrys(eid, title, date)
  220 read(1, '(A)', END=230, ERR=299) buf
      write(*, '(A)') buf(:length(buf))
      goto 220
  230 close(UNIT=1)
      call entrye()
      return
  299 write(*,*) 'Error reading entry ', eid
      end

c GETES - return a list of ten eids from the start specified
      subroutine getes(start, entries)
      integer start
      integer entries(10)
      integer dump

      do 300, I=1,10
          entries(I) = 0
  300 continue

      open(unit=2, status='OLD', file='index', ERR=399)

      do 310, I=1,start-1
  310 read(2, '(I8)', END=399) dump

      read(2, '(I8)', END=399) entries
  399 close(unit=2)
      end
