-- poging om alle info bestaden via een programma te maken
-- Er ziten zo min mogelijke check's in dit om het CGI te gaan gebruiken .
-- Aangenomen moet dus worden dat de mapping mnr op data correct is .
-- Het aantal aftebeelden velden wordt gegeven door .txt (en niet door .mnr) .
-- hum er wordt nu gekeken naar wie van beiden het kleinst is .
-- Tweede parameter kan de mnr file zijn .
-- Er kan een MASK als derde parameter worden opgeven .
-- Dit bepaald de doorsnede tussen mnr and data velden . 
-- Een - is weglaten 0 afbeelden (indien aanwezig) 1 afbeelden en moet aanwezig zijn .
-- Het idee is dat mask[i] mnr[i] data[i] -> afbeelding 
-- Als de 1 in mask[i] geen data[i] (of geen mnr[i]) heeft wordt het gehele record NIET afgebeeld .
 
include readf.e
sequence filename
sequence fileform
sequence filemask = {}
-- get arg of the command line
sequence record
integer fileout
record = getenv("QUERY_STRING")
-- look for CGI or local generator
if integer(record)
then record = command_line()
     if length(record)>=3
     then filename=record[3]
          if length(record)>=4
          then fileform=record[4]
               if length(record)=5
               then filemask = record[5]
               end if
          else fileform=filename
          end if
     else puts(1,"Use genhtml data_filename [format_filename [file_mask]] (without .ext)\n") abort(1)
     end if
     fileout = open(fileform&".htm","w")
     if fileout<0 then puts(1,"Cann't open file .htm\n") abort(1) end if
else fileout = find('+',record)
     if fileout
     then filename = record[1..fileout-1]
          fileform = record[fileout+1..]
     else filename = record
          fileform = record
     end if
     fileout = find('+',fileform)
     if fileout
     then filemask = fileform[fileout+1..]
          fileform = fileform[1..fileout-1]
     end if
     fileout = 1
end if 

-- open file's
integer filetxt=open(filename&".txt","r")
integer filemnr=open(fileform&".mnr","r")
-- check if the file's are there
if filemnr<0 then puts(1,"Cann't open file "&fileform&".mnr\n") abort(1) end if
if filetxt<0 then puts(1,"Cann't open file "&filename&".txt\n") abort(1) end if
-- lees beschrijving
sequence mnr={}
record=readf(filemnr,',')
while sequence(record) do
  -- skip empty record's
  if length(record) then mnr=append(mnr,record) end if
  record=readf(filemnr,',')
end while
close(filemnr)
-- ende lees beschrijving

-- first record is title
-- next  record is sub title
-- --    record is discription of sub title block
-- last  record is html close

-- the data file first line is the title line
-- the data file next line  is a sub title 
-- -- next lines are the sub title block 
-- -- UNTIL the record length equals the sub title block .
-- after that a new sub title block will be read if not there take the previeus one .

-- this means that no line of the sub title block should be equal to the
-- length of the sub title .

-- a subtitution is giving by a $ , it may be repeated .
function subdollar(sequence mnr;text)
integer location
-- indien geen sequence maak er een sequence van . n.l. $ -> {$}
if not sequence(mnr) then mnr={mnr} end if
location=find('$',mnr)
if integer(text) then text=sprintf("%d",{text}) end if
while location do
 mnr=mnr[1..location-1]&text&mnr[location+1..-1]
 location = find('$',mnr)
end while
return mnr
end function

-- declarations 
integer  titleindex = 1
-- stel de subtitle is standard de lengte van de eerste subtitle .
integer lengthsubtitle
integer lengthmnrdata
integer lfilemask = 0

procedure makehtml(integer lengte)
sequence result = {}
 if lfilemask
 then for i = 1 to lfilemask do
      if filemask[i] != '-'
      then if i>lengte             -- bescherm tegen ongelijke veld lengtes
           then if filemask[i]='1'
                then result = {}
                     exit
                end if
           else if filemask[i] = '0'
                then result &= subdollar(mnr[titleindex][i],record[i])
                elsif filemask[i]='1' 
                     then if length(record[i])
                     then result &= subdollar(mnr[titleindex][i],record[i])
                     else result = {} 
                          exit
                     end if
                elsif and_bits(filemask[i],record[i]) -- getal AND {} = true
                then result &= mnr[titleindex][i]
                else result = {}
                     exit
                end if
           end if
      end if
      end for
      if length(result) then puts(fileout,result&'\n') end if
 else for i = 1 to lengte do 
--puts(1,record[i]&'\n') ?record[i] ? i ?titleindex ?mnr
        --puts(fileout,subdollar(mnr[titleindex][i],record[i]))
          result=subdollar(mnr[titleindex][i],record[i])
          if length(result) then puts(fileout,result&'\n') end if
      end for
 end if
end procedure

-- lees data
 -- read first line of the data file
 record = readf(filetxt,',')
 if not sequence(record) 
 then puts(1,"Error in .txt file No Title record\n") abort(1) 
 end if
 -- sub $ with title
 makehtml(length(mnr[titleindex]))
 -- next record must be a sub title
 record = readf(filetxt,',')
 lengthsubtitle = length(record)
 while sequence(record) do
   if titleindex+1 < length(mnr) -- was != .
   then titleindex+=1
   else titleindex-=1
   end if
   makehtml(lengthsubtitle)
   titleindex+=1
   lengthmnrdata = length(mnr[titleindex])
   record = readf(filetxt,',')  
   -- should be a data record
   lfilemask = length(filemask) -- zet mask on
   while sequence(record) and ( length(record) != lengthsubtitle ) do
     if lengthmnrdata>length(record)
     then makehtml(length(record))
     else makehtml(lengthmnrdata)
     end if
     record = readf(filetxt,',')
   end while
   lfilemask = 0               -- zet mask off
 end while
 -- close data file
 close(filetxt)
 -- close html
 puts(fileout,mnr[-1][1]) -- sluit html af .
 close(fileout)