/* modify the colors used in the SRE-http configurator routines */

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

if verb="" then do
   say " This SRE-http add-on is NOT meant to be run from the command line."
   say " It can be invoked by using CONFIGUR.HTM "
   exit
end  /* Do */

/* chekc for needed_privs superuser */
if wordpos('SUPERUSER',upper(privset))=0 then do
    'header add WWW-Authenticate: Basic Realm=<CONFIGURE>'  /* challenge */
     return sref_response('unauth', "You do not have Superuser privileges ",tempfile,servername)
end

addon_dir=value(enmadd||'ADDON_DIR',,'os2environment')

 call lineout tempfile, '<!doctype html public "-//IETF//DTD HTML 2.0//EN">'

  call lineout tempfile, "<html><head><title>SRE-http: Modify Configurator Colors  </title>"
  call lineout tempfile, "</head><body>"
  call lineout tempfile,' <h1>Modify Configurator Colors </h1> <hr>'

do until list=""                /* get user input */
   parse var list a1 '&' list
   parse var a1 a1a '=' a1b
   ook='!'||strip(upper(a1a))
   stuff.ook=strip(upper(a1b))
end /* do */

/* check custom colors for proper syntax */
checks="!CONFIG !CONFIG0 !CONFIG2 !CONFIG3 "
checks2="!CONFIGC !CONFIG0C !CONFIG2C !CONFIG3C "

mess.1='Configurator Introduction'
mess.2='Simple mode configurator'
mess.3='Intermediate mode configurator'
mess.4='Expert mode configurator'

do mm=1 to 4
   ae=strip(word(checks,mm))
   is1=stuff.ae
   if is1 <>3 then iterate
   ill=strip(word(checks2,mm))
   af=left(stuff.ill,6)
   woo=verify(af,'0123456789ABCDEF')
   if woo>0 then do
      call lineout tempfile,' <h2>Error in Color Entry </h2> <hr>'
      call lineout tempfile,'<b>'af '</b>, your color entry for the ' mess.mm ', is invalid '
      call lineout tempfile, ' <bR> The entry must be contain six hex (0123456789ABCDEF) digits '
      call lineout tempfile,'</body></html>'
      call lineout tempfile
     fpp=sref_gos('FILE ERASE TYPE text/html NAME' tempfile)
      return 'Error in FIXCOLOR'
   end
end /* do */


/* now change the colors
DEFAULTS:
INTRO: '10e581'
SIMPLE :'3DEF5F'
INTERMEDIATE: '2DD52F'
ADVANCED: '40C541'
*/


/* FIRST: the configurator intro  */

usec='CCCCCC'
if stuff.!config=1 then usec='10e581 '
if stuff.!config=3 then usec=stuff.!configc
useme='<BODY bgcolor="#'||usec||'" > '

call htmlfix 'CONFIGUR.HTM'

call lineout tempfile,'<br> Configurator introduction color: ' usec ' ( <font color="#'||usec||'"> XXXXXXXXXXXXXXXXXXXXXXX </font><p>'


/* SECOND:the simple mode configurator  */

usec='BBBBBB'
usec="3def5f" 
if stuff.!config0=3 then usec=stuff.!config0c
useme='<BODY bgcolor="#'||usec||'" > '

call htmlfix 'CONFIG0.HTM'
call cmdfix 'SREFCONF.CMD'

call lineout tempfile,' <br> Simple  mode Configurator color: '  usec ' ( <font color="#'||usec||'"> XXXXXXXXXXXXXXXXXXXXXXX </font><p>'


/*  THIRD: the intermediate mode configurator */
usec='DDDDDD'
if stuff.!config2=1 then usec='2dd52f'
if stuff.!config2=3 then usec=stuff.!config2c
useme='<BODY bgcolor="#'||usec||'" > '

call htmlfix 'CONFIG2.HTM'

call cmdfix 'CONFIG2.CMD'
call cmdfix 'VIRTCFG.CMD'
call cmdfix 'ALIASCFG.CMD'
call cmdfix 'ACCESCFG.CMD'
call cmdfix 'USERCFG.CMD'
call cmdfix 'HTACCFG.CMD'

call lineout tempfile,' <br> Intermediate mode Configurator color: '  usec ' ( <font color="#'||usec||'"> XXXXXXXXXXXXXXXXXXXXXXX </font><p>'

/* FOURTH: the expert mode configurator */

usec='aaaaaa'
if stuff.!config3=1 then usec='40C541'
if stuff.!config3=3 then usec=stuff.!config3c
useme='<BODY bgcolor="#'||usec||'"> '
call htmlfix 'CONFIG3.HTM'
call lineout tempfile,' <br> Expert mode Configurator color: '  usec ' ( <font color="#'||usec||'"> XXXXXXXXXXXXXXXXXXXXXXX </font>'


call lineout tempfile,'</body></html>'
call lineout tempfile
fpp=sref_gos('FILE ERASE TYPE text/html NAME' tempfile)
return 'FIXCOLOR completed'


/********** change bgcolor in html file, use global useme */
htmlfix:
parse arg bfile

afile=strip(translate(ddir,'\','/'),'t','\')'\'||bfile
if stream(afile,'c','query exists')='' then do
      call lineout tempfile,'<br> Configurator document file,'afile', could not be found '
      return 0
end  /* Do */
foo=charin(afile,1,chars(afile))
parse var foo t1 '<BODY ' . '>' t3
woops=t1||useme||t3
foo=sysfiledelete(afile)
op=charout(afile,woops,1)
 call lineout tempfile,' <br> Successfully modified (HTML document): ' afile

return 1


/********* part 3 fixer (uses global usec )*/
cmdfix:
parse arg ado

/* 3rd part 4: the intermediate mode configurator 
The files are expected to be in the addon directory.
If not there, we give up */
afile=strip(translate(addon_dir,'\','/'),'t','\')'\'ado
if stream(afile,'c','query exists')='' then do
      call lineout tempfile,' <br> Configurator file, 'afile', could not be found '
      return 0
end  /* Do */
foo=charin(afile,1,chars(afile))
crlf='0d0a'x

oops=pos('USECOLOR=',upper(foo))
oop2=pos(crlf,foo,oops)
t1=left(foo,oops-1)
t3=substr(foo,oop2+2)
woops=t1||"USECOLOR='"||usec||"'"||crlf||t3
foo=sysfiledelete(afile)
op=charout(afile,woops,1)
  call lineout tempfile,' <br> Successfully modified (configuration file): ' afile
return 0

