#!/usr/bin/rexx
/* -------------------------------------------------------------------------- */
/* REXX script SAMPLE for evaluation of WEB server's guest                  - */
/* -------------------------------------------------------------------------- */
srv_name =  value('SERVER_NAME',, environment)  /* Get the name of the server */
ret_data =  value('QUERY_STRING',, environment)  /* Get the data returned     */
client_addr =  value('REMOTE_ADDR',, environment)  /* Get the remote address  */

parm = .parmdir~new(ret_data)          /* create parameter directory         */

say "Content-type: text/html"
say
say "<html>"
say '<body bgcolor="#ffffff">'

/* My standard header ------------------------------------------------------- */
say "<table width='70%'><tr>",
    "<td><img src=/orexx.gif align=top>",
    "<td><p align="center"><font size=+2 color=#008000><b>CGI powered by Object REXX</b></font>",
    "<br><font size=+1 color=#808080><b>The Scripting Language</b></font>",
    "</table>"
say "<head> <title> Object REXX visitors' book</title> </head>",
    "<h1> Visitors' book of Object REXX on server '"srv_name"': </h1>",
    "<br> <b>Date:</b> "date()"; <b>Time:</b> "time();

/* Show the visitors' book entries  ----------------------------------------- */
vbook_name = "/home/nobody/vbook.txt";
--trace ?r
    ifile = .stream~new(vbook_name)         /* Open the visitors' book file   */
    ifile~open
--  say 'ifile ='ifile
    if ifile~lines() = 0 then
      do
       say " *** Error: file "vbook_name" does not exist!"
       exit(99)
    end

    msgcnt   = 0;                                 /* Entry counter            */
    maxmsg   = 11;                                /* Number of last entries   */
    stemlcnt = 0;                                 /* Stem line counter        */
    endlcnt  = ifile~lines() +1;                  /* Save max lines of file   */
    lastmsg. = '';                                /* Init of stem variable    */
    enemail  = '';

    do lcnt = 1 to endlcnt
        if msgcnt = maxmsg then leave             /* Number of last entries   */
        inline = ifile~linein(endlcnt - lcnt+1)   /* Get line starting from   */
                                                  /* end of file              */
        if (inline~substr(1,5) = 'END*=') |,      /* Filter END of an entry   */
           (inline~substr(1,6) = 'ENDCNT') then
          do
            if (inline~substr(1,6) = 'ENDCNT') then /* Les than max msg found */
              maxmsg = msgcnt;
            else
              do
                msgcnt = msgcnt +1;
                stemlcnt = stemlcnt + 1;
            end
            if msgcnt <> maxmsg then
              lastmsg.stemlcnt = '</table>';      /* Put END tag              */
            iterate
         end

        if (inline~substr(1,7) = 'E-MAIL=') then  /* Filter EMAIL of an entry */
          do
            parse var inline 'E-MAIL=' enemail .
            iterate
        end

        if (inline~substr(1,6) = 'RXUSE=') then   /* Filter RXUSE of an entry */
           iterate

        if (inline~substr(1,4) = 'CNT=') then     /* Get the DATE of an entry */
          do
            parse var inline 'CNT=' entrynum 'DATE=' endate '@' entime .
            stemlcnt = stemlcnt + 1;
            lastmsg.stemlcnt = '<table border=1 width=95%><tr><td width="68%"',
            " align='left'>Entry #<font size=+1 color=#ff0000>"entrynum"</td>",
            "<td></font>dated:"date('N',strip(endate),'O')" at "entime" o'clock</td></tr>"
            iterate
        end
        if (inline~substr(1,6) = 'TITEL=') then   /* Get the titel of an entry*/
          do
            parse var inline 'TITEL=' entitel 'FNAM=' enfname . 'SNAM=' ensname
            if entitel = 'FNAM=' then entitel = ' ';
            if ensname = '' & enfname = '' then ensname = '-NO-NAME-';
            stemlcnt = stemlcnt + 1;
            lastmsg.stemlcnt = '<tr><td  align="center"> 'entitel'  'enfname,
            ' 'ensname' wrote:</td>'
            if enemail <> '' then
              do
                lastmsg.stemlcnt = lastmsg.stemlcnt||,
                  '<td><a align="right" href="mailto:'enemail'">mail answer</a></td>'
                enemail = '';
            end
            else
              do
                lastmsg.stemlcnt = lastmsg.stemlcnt||,
                  '<td>no e-mail address</td>'
            end
            lastmsg.stemlcnt = lastmsg.stemlcnt||'</tr>'
            iterate
        end
        parse var inline 'TEXT=' entext
        if entext <> '' then
          do
            stemlcnt = stemlcnt + 1;
            lastmsg.stemlcnt = '<tr><td colspan=2> > 'entext,
            '</td></tr>'
         end
        else
          if inline <> '' then
            do
              stemlcnt = stemlcnt + 1;
              lastmsg.stemlcnt = '<tr><td colspan=2> > 'inline,
            '</td></tr>'
          end
    end
    ifile~close                             /* Close the visitors' book file  */
    lastmsg.0 = stemlcnt;

--trace ?r
    do i = 1 to lastmsg.0                   /* Display the entries            */
        msgline = lastmsg.0 - i +1;
        say lastmsg.msgline;

    end

/*
else
  do
     * Display the message for NOD data has been received ------------------- *
    say "<p>",
      '<img align="right" src="/orxok.gif"><b><font color="#ff0000"> No comment has been entered :-(</font></b>',
      '<p> Please go',
      '<a align="center" href="http://'srv_name'/rxguest"> BACK, </a>',
      'if you like to enter a comment.'

end */
/* Footing of WEB page ------------------------------------------------------ */
say "<p>",
    "<hr>"

say "</body> </html>"

exit

::requires "myhtml.cls"                /* load myHtml class                  */
