program cntbmp;
{
    A CGI in pascal. Meant to be a web-counter

    HTML query (different variables) not implemented yet. Need to reintegrate
    cgi_get again. The first argument will become the variable name.}

uses strings,
     baseunix;

const max_data = 1000;

type datarec = record
  name,value : string;
  end;

  bmpheadtype = packed record  {Headers for BMP. Putting them in one record is easier}
           { bit map file header }
           bftype: word;                   { "BM" or $4D42 }
           bfsize: longint;                { size of file in bytes }
           bfreserved1: word;
           bfreserved2: word;
           bfoffbits: longint;             { ^ where graphic data begins }


           { bit map information header }
           bisize: longint;                { length of this header, $28 }
           biwidth: longint;               { pixel width }
           biheight: longint;              { pixel height }
           biplanes: word;                 { = 1 }
           bibitcount: word;               { color bits per pixel }
           bicompression: longint;         { = 0 for no compression }
           bisizeimage: longint;           { = bfsize - bfoffbits }
           bixpelspermeter: longint;       { x pixels per meter }
           biypelspermeter: longint;       { y pixels per meter }
           biclrused: longint;             { \ I have never seen these }
           biclrimportant: longint;        { / two used for anything }
         end;


        { 10 characters (character 0..9) , 12x8 dimension
            (some VGA font with four colums zeros deleted)}

CONST Font : ARRAY[0..119] OF BYTE= (
                          0,24,36,36,66,66,66,36,36,24,0,0,
                          0,24,40,72,8,8,8,8,8,8,0,0,
                          0,60,66,2,4,8,16,32,64,126,0,0,
                          0,60,66,2,2,28,2,2,66,60,0,0,
                          0,4,12,20,36,68,254,4,4,4,0,0,
                          0,126,64,64,124,66,2,2,66,60,0,0,
                          0,28,34,64,64,92,98,66,66,60,0,0,
                          0,126,2,2,4,4,8,8,16,16,0,0,
                          0,60,66,66,66,60,66,66,66,60,0,0,
                          0,60,66,66,70,58,2,2,68,56,0,0);

VAR 
    Thousands,                   {Counter digits}
    Hundreds,
    Tenths,
    one     : LONGINT;
    hdr     : bmpheadtype;      {BMP header}
    Rows    : LONGINT;          {Generic counter, and counter for
                                 Rows of picture}
    BytePtr : ^BYTE;            {Used to output header}
    nrdata  : longint;
    p       : PChar;
    literal,
    aname   : boolean;
    F       : file;
    Count   : LONGINT;
    Dummy   : LONGINT;

begin

if StrComp(fpgetenv('REQUEST_METHOD'),'GET')<>0 then
   begin
   Writeln ('Content-type: text/html');
   Writeln;
   Writeln ('This script should be referenced with a METHOD of GET');
   halt(1);
   end;

   Writeln ('Content-type: image/bmp');
   Writeln;
   p:=fpgetenv('QUERY_STRING');

   Assign (f,'count');
   {$I-}
   reset(F,1);
   {$I+}
   IF IORESULT<>0 THEN
    BEGIN
    Assign(F,'count');
    rewrite(F);
    Close(F);
    Assign(F,'count');
    reset(F,1);
    Count:=0;
    END
   ELSE
    BEGIN
     Blockread(F,Count,4,Dummy);
     Seek(F,0);
    END;
    INC(Count);
    BlockWrite(F,Count,4);
    Close(F);

 {Set up BMP-header}


 FillChar(hdr,SIZEOF(Bmpheadtype),0);
 WITH Hdr DO
   BEGIN
    bftype:= $4D42;                 { "BM" or $4D42 }
    bfsize:= 54+8+4*12;             { size of file in bytes }
    bfreserved1:= 0;
    bfreserved2:= 0;
    bfoffbits:= $3E;            { where graphic data begins }
    bisize:= 40;                    { length of this header }
    biwidth:= 32;                  { pixel width }
    biheight:= 12;                 { pixel height }
    biplanes:= 1;                   { =1 }
    bibitcount:= 1;                 { color bits per pixel }
    bicompression:= 0;              { =0 for no compression }
    bisizeimage:= bfsize-bfoffbits;
    bixpelspermeter:= 0;
    biypelspermeter:= 0;
    biclrused:= 0;
    biclrimportant:= 0;
  end;

 {Output BMP-header to standard out}

 BytePtr:=@Hdr;
 FOR Rows:=0 TO 14+40-1 DO
   write(CHR(Byteptr[Rows]));

 {Output colors/palette}

 Write(#255,#255,#255,#0,#0,#0,#0,#0);

 { Split up into digits}

 Thousands:=Count DIV 1000; Count:=Count MOD 1000;
 Hundreds:=Count DIV 100; Count:=Count MOD 100;
 Tenths:=Count DIV 10; One:=Count MOD 10;
 IF Thousands>9 THEN Thousands:=9;

 { Print bitmapdata}

 FOR Rows := 11 DOWNTO 0 DO { Rows loop}
  BEGIN
   {Construct bitmap line}
   Write(CHR(Font[12*Thousands+Rows]));
   Write(CHR(Font[12*Hundreds+Rows]));
   Write(CHR(Font[12*Tenths+Rows]));
   Write(CHR(Font[12*One+Rows]));
  END;
end.
