/*   17 June 1997:

 A front end to the GIF_TEXT addon -- this will take a request generated
by MKGIFTXT.HTM, and return a link to GIF_TEXT. 

Note that this will work either as a cgi-bin script (for a generic, os/2
cgi-bin-compatabile server,or as an addon for sre-filter. It will
detect how it's being called, and respond accordingly.

*/

parse arg  ddir, tempfile, reqstrg,list,verb ,uri,user, ,
          basedir ,workdir,privset,enmadd,transaction,verbose, ,
         servername,host_nickname,homedir

signal on syntax name err1
signal on error name err1

/* check for CGI-BIN call */
is_cgi=0
if verb="" then do    /* is it cgi-bin? */
   method = value("REQUEST_METHOD",,'os2environment')
   if method="" then do
        say " Sorry, this is a web server utility. "
        exit
   end /* do */
   is_cgi=1
   list=value("QUERY_STRING",,'os2environment') 
end
else do
   parse var uri . '?' list
end
list=list||'&MESSAGE.GIF'
crlf='0d0a'x

if is_cgi=0 then
   img0='<IMG src="/GIF_TEXT?'||list||'">'
else
   img0='<IMG src="/cgi-bin/GIF_TEXT?'||list||'">'

iv=upper(img0)
parse var iv . 'SLIDE=' goon '&' .
if goon<>'' then 
   extramess='<b>Note...</b> images created with <em>color slides</eM> may take require a few minutes to be generated '
else
  extramess=""

img=img0
fimg2="" ; tmp=""
 do until img=""
      parse var img a1 '&' img
        if tmp="" then
           tmp=a1
        else
          tmp=tmp'&'a1
        if length(tmp)>80 then do
            if img<>""  then tmp=tmp'&'
            fimg2=fimg2'<br>'||fixit(tmp)
            tmp=""
        end
end /* do */
if tmp<>"" then fimg2=fimg2'<br>'||fixit(tmp)
fimg=fimg2

retmess=' Return to <a href="mkgiftxt.htm">GIF_TEXT input form </a> <p>'

foo1='<HTML><head><TITLE>Generating A Graphical Message</title></head>'crlf
foo1=foo1||'<body><h2>Generating a graphical message </h2> ' crlf||extramess||'<p>'
foo1=foo1||' This image is generated from:<br><code>'||fimg||'<p>'crlf
foo1=foo1||img0||'<p><hr>'||retmess

foo1=foo1||'</body></html>'

if is_cgi=1 then do
  Say "Content-type: text/html"
  Say
  call charout,foo1
  return
end

exp=value(enmadd||'FIX_EXPIRE',,'os2environment')
if exp>0 then
   a=sref_expire_response(0.1,length(foo1),'text/html')

'var type text/html name foo1 '


return 0

fixit:procedure
parse arg adesc

adesc=a_replacestrg(adesc,'&','&amp;','ALL')
adesc=a_replacestrg(adesc,'<','&lt;','ALL')
adesc=a_replacestrg(adesc,'>','&gt;','ALL')
adesc=a_replacestrg(adesc,'"','&quot;','ALL')
return adesc


a_replacestrg:

exactmatch=0
backward=0 ; doall=0

parse arg astring ,  target   , putme , type , exactmatch

type = translate(type)
if type="BACKWARD" then backward="YES"
if type="ALL" then doall="YES"

iat=1
joelen=length(target)
joelen2=length(putme)

doagain:                /* here if doall=yes */
 if exactmatch="YES" then do
    if   backward="YES" then
        joe= lastpos(target,astring)
    else
        joe= pos(target,astring,iat)
 end
 else do
   if   backward="YES" then
        joe= lastpos(translate(target),translate(astring))
    else
        joe= pos(translate(target),translate(astring),iat)
 end
 if joe=0 then
         return astring

 astring=delstr(astring,joe,joelen)
 if putme<>' ' then
    astring=insert(putme,astring,joe-1)

 if doall="YES" then do
     iat=joe+joelen2
     signal doagain
 end
/* else, all done */
 return astring



err1:
say " error in mkgiftxt at " sigl '( ' rc
