/* ***********************************************************************

     HTML_table.rexx v1.1 (17-6-99) for THE FOOTBALL REXX SUITE
    -----------------------------------------------------------
     by Kevin Lambert (amiga4000@bizonline.co.uk)

    -This script will generate an HTML file of the normal league table.

    -To use HTML_table.rexx you can run it by using the 'Run Script' button
     on the Football main panel. Select HTML_table.rexx from the file
     requester and away you go. A file (????tab.html) will be written to RAM:
     but this location  can be changed. View the file with any browser that
     supports tables.


    -Look through the script if you want to change some Options :-)


     Version    Date     History
    --------------------------------------------------------------------------
      1.0       270599   First release.
      1.1       170699   Fixed inherited bug from League where if all teams
                         had zero points, the promotion bar was still
                         displayed.(Kindly altered by Mark Naughton)
                         HTMLtable.rexx name changed to HTML_table.rexx
                         Some changes to work with Football v2.4

************************************************************************** */
ARG league_file

htmlpath     = "Ram:"                /* Full Path for the html table file*/
lname        = league_file
version      = 1
league_file  = "Data/" || league_file
input_file   = '.stats'
input2_file  = '.sf'
input3_file  = '.df'
output_file  = 'Data/League.output'
title        = '*LEAGUE_NAME='
points_win   = '*POINTS_PER_WIN='
points_drw   = '*POINTS_PER_DRW='
points_lse   = '*POINTS_PER_LSE='
points_gls   = '*POINTS_PER_GLS='
releg        = '*RELEGATION='
playother    = '*PLAY_OTHER='
promoted     = '*PROMOTED='
team_counter = '*COUNTR='
team         = '*TEAM='
played       = '*PLY='
won          = '*WIN='
drawn        = '*DRW='
lost         = '*LST='
goalsf       = '*GOF='
goalsa       = '*GOA='
points       = '*PTS='
separator    = '*'
teams.       = '???'
teams2.      = '???'
m_ply.       = '???'
m_win.       = '???'
m_drw.       = '???'
m_lost.      = '???'
m_gof.       = '???'
m_goa.       = '???'
m_pts.       = '???'
teams_ctr    = 0
playo        = 2
reg          = 2
ptsgls       = 0
ptswin       = 2
ptsdrw       = 1
ptslse       = 0
promo        = 0
not_played   = '__   __'


if exists(league_file || input_file) = 0  then exit
if exists(league_file || input2_file) = 0 then exit
if exists(league_file || input3_file) = 0 then exit
open(stats,htmlpath||lname||"tab.html","w")

tcount = 0
if open(datafile,league_file || input3_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(title,line) > 0 then        league_title = delstr(line,1,13)
      if pos(points_win,line) > 0 then   ptswin=delstr(line,1,16)
      if pos(points_drw,line) > 0 then   ptsdrw=delstr(line,1,16)
      if pos(points_lse,line) > 0 then   ptslse=delstr(line,1,16)
      if pos(points_gls,line) > 0 then   ptsgls=delstr(line,1,16)
      if pos(releg,line) > 0 then        reg = delstr(line,1,12)
      if pos(playother,line) > 0 then    playo = delstr(line,1,12)
      if pos(promoted,line) > 0 then     promo = delstr(line,1,10)
      if pos(separator,line) = 0 then do
         line = strip(line)
         tcount       = tcount + 1
         teams.tcount = line
      end
   end
   close(datafile)
end
else do
   say
   say "ERROR :    (League)"
   say
   say "Unable to open '"league_file || input3_file"' file."
   exit
end

if open(datafile,league_file || input_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(team,line) > 0 then
         teams_ctr = teams_ctr + 1
      if pos(played,line) > 0 then  m_ply.teams_ctr  = delstr(line,1,5)
      if pos(won,line) > 0 then     m_won.teams_ctr  = delstr(line,1,5)
      if pos(drawn,line) > 0 then   m_drw.teams_ctr  = delstr(line,1,5)
      if pos(lost,line) > 0 then    m_lost.teams_ctr = delstr(line,1,5)
      if pos(goalsf,line) > 0 then  m_gof.teams_ctr  = delstr(line,1,5)
      if pos(goalsa,line) > 0 then  m_goa.teams_ctr  = delstr(line,1,5)
      if pos(points,line) > 0 then  m_pts.teams_ctr  = delstr(line,1,5)
   end
   close(datafile)
end
else do
   say
   say "ERROR :    (League)"
   say
   say "Unable to open '"league_file || input_file"'."
   exit
end

if open(datafile,league_file || input2_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(separator,line) = 0 then do
         if pos(not_played,line) = 0 then do
            home_team = strip(substr(line,1,30))
            goals_for = substr(line,32,2)
            goals_aga = substr(line,37,2)
            away_team = strip(substr(line,41,30))

            do i=1 to teams_ctr
               if home_team = teams.i then do
                  m_ply.i = m_ply.i + 1
                  m_gof.i = m_gof.i + goals_for
                  m_goa.i = m_goa.i + goals_aga
                  if goals_for = goals_aga then do
                     m_drw.i = m_drw.i + 1
                     m_pts.i = m_pts.i + ptsdrw
                  end
                  if goals_for < goals_aga then do
                     m_lost.i= m_lost.i + 1
                     m_pts.i = m_pts.i + ptslse
                  end
                  if goals_for > goals_aga then do
                     m_won.i = m_won.i + 1
                     m_pts.i = m_pts.i + ptswin
                  end
                  m_pts.i = m_pts.i + (goals_for * ptsgls)
               end
            end
            do i=1 to teams_ctr
               if away_team = teams.i then do
                  m_ply.i = m_ply.i + 1
                  m_gof.i = m_gof.i + goals_aga
                  m_goa.i = m_goa.i + goals_for
                  if goals_for = goals_aga then do
                     m_drw.i = m_drw.i + 1
                     m_pts.i = m_pts.i + ptsdrw
                  end
                  if goals_for < goals_aga then do
                     m_won.i = m_won.i + 1
                     m_pts.i = m_pts.i + ptswin
                  end
                  if goals_for > goals_aga then do
                     m_lost.i= m_lost.i + 1
                     m_pts.i = m_pts.i + ptslse
                  end
                  m_pts.i = m_pts.i + (goals_aga * ptsgls)
               end
            end
         end
      end
   end
   close(datafile)
end
else do
   say
   say "ERROR :    (League)"
   say
   say "Unable to open '"league_file || input2_file"'."
   exit
end

if open(outfile,output_file,"w") then do
   do i=1 to teams_ctr
      line = teams.i
      line = insert(" ",line,length(line)+1,30-length(line))
      mp = right(m_ply.i,3)
      mw = right(m_won.i,3)
      md = right(m_drw.i,3)
      ml = right(m_lost.i,3)
      mgf = right(m_gof.i,5)
      mga = right(m_goa.i,5)
      mpts = right(m_pts.i,7)
      writech(outfile,line"   "mp" "mw" "md" "ml" "mgf" "mga"  "mpts"      ")
      itemp=mgf-mga
      if itemp>0 then
         itemp=insert("+",itemp,0,1)
      itemp=right(itemp,4)
      writeln(outfile,itemp)
   end
   close(outfile)
end
else do
   say
   say "ERROR :    (League)"
   say
   say "Unable to update the League. Cannot write to '"output_file"'."
   exit
end

address command 'Exec/footsort '

if open(datafile2,output_file,'r') then do
   do i=1 to tcount
      line = readln(datafile2)
      teams.i  = line
      teams2.i = line
   end
   close(datafile2)
end
else do
   say
   say "ERROR :    (League)"
   say
   say "Cannot read '"output_file"'."
   exit
end

ctr = 0
do while swapctr > 0
   swapctr=9
   do i=1 to tcount-1                               /* was 64,4 and 77,4 */
      swapdone=0
      j = i + 1
      if strip(substr(teams.i,64,7)) = strip(substr(teams.j,64,7)) then do
         goaldiffa = strip(substr(teams.i,77,5))
         goaldiffb = strip(substr(teams.j,77,5))
         if goaldiffa < goaldiffb then do
            teams.i = teams2.j
            teams.j = teams2.i
            teams2.i= teams.i
            teams2.j= teams.j
            swapdone= 1
            ctr = ctr + 1
         end
      end
      if swapdone = 1 then
         break
   end
   if ctr = 0 then
      swapctr = 0
   else
      ctr = 0
end

posreg   = 0
pospro   = 0
if reg > 0 then do             /* this bit of code checks to see which teams are relegated */
   wherereg = tcount - reg
   regpts   = strip(substr(teams.wherereg,64,7))         /* was 64,4 */
   do i=tcount to 1 by -1
      b = (((tcount-1) * playo) - strip(substr(teams.i,35,3))) * ptswin  /* was 36,2 */
      c = strip(substr(teams.i,64,7)) + b
      if c < regpts then posreg = i                 /* calculates the max points avail */
      if c = regpts & strip(substr(teams.i,35,3)) = ((tcount-1) * playo) then
         posreg = i
   end
   if posreg < (wherereg+1) & posreg ~= 0 then      /* was 0 - set to reqd pos if higher */
      posreg = wherereg + 1
                                                    /* Bug fix 0305 - relegation bar problems */
end

/* ---------------------------------------------- */


if promo > 0 then do          /* this bit of code checks to see which teams are promoted */
   wherepro = promo + 1
   propts   = strip(substr(teams.wherepro,64,7))         /* was 64,4 */
   do i=1 to tcount
      if strip(substr(teams.i,35,3)) > 0 then
         b = (((tcount-1) * playo) - strip(substr(teams.i,35,3))) * ptswin
      else
         b = 0
      c = strip(substr(teams.i,64,7)) + b
      if c > propts then pospro = i                 /* calculates the max points avail */
   end
   if pospro > (wherepro-1) & pospro ~= 0 then
      pospro = wherepro - 1                         /* was 0 - set to reqd pos if lower */
end


/* -------------------------------HTML options--------------------------------------- */



cs   =  2                             /* Cellspacing              */
cp   =  2                             /* Cellpadding              */
br   =  1                             /* Border size              */
pb   =  #779977                       /* Page Background colour   */
bg   =  #55ee55                       /* top team colours         */
fc   =  #000000                       /* Default table font colour*/
pc   =  #eeeeee                       /* Points Highlight colour  */
tw   =  450                           /* Table width              */


call writeln(stats,"<HTML>")
call writeln(stats,"")
call writeln(stats,"<!-- Created with HTMLtable.rexx (v1.0) Script for football -->")
call writeln(stats,"<!--        A football tracking program for the Amiga       -->")
call writeln(stats,"")
call writeln(stats,"<HEAD>")
call writeln(stats,"<TITLE>")
call writeln(stats,lname" League Table")
call writeln(stats,"</TITLE>")
call writeln(stats,"")
call writeln(stats,"<META NAME=""author""      CONTENT=""Kevin Lambert (amiga4000@bizonline.co.uk)"">")
call writeln(stats,"<META NAME=""generator""   CONTENT=""HTMLtable.rexx"">")
call writeln(stats,"")
call writeln(stats,"</HEAD>")
call writeln(stats,"")
call writeln(stats,"<BODY BGCOLOR="""pb""">")
call writeln(stats,"<DIV ALIGN=""center"">")
call writeln(stats,"<TABLE CELLSPACING="""cs""" CELLPADDING="""cp""" BORDER="""br"""BGCOLOR=""#ccddcc"" WIDTH="""tw""">")
call writeln(stats,"<TR><TH COLSPAN=""10"" BGCOLOR=""#aaaaee"">"center(league_title,88)"</TH></TR>")
call writeln(stats,"<TR ALIGN=""center"" BGCOLOR=""#aaaaee""><TD>Pos</TD><TD>Team</TD><TD>Pld</TD><TD>Won</TD><TD>Drw</TD><TD>Los</TD><TD>Gf</TD><TD>Ga</TD><TD>Points</TD><TD>Gdiff</TD></TR>")
call writeln(stats,"<TR><TD HEIGHT=""1"" BGCOLOR=""#000000"" COLSPAN=""10""></TD></TR>")

do i=1 to tcount
if i > 1 then bg = "#ccddcc"                         /* table Background colour */
   if strip(substr(teams.i,76,5)) = 0 then teams.i=overlay("",teams.i,76,5," ")
   if i >= posreg & posreg~=0 then teams.i = upper(teams.i)
   if i >= posreg & posreg~=0 then bg = "#ff5555"    /* possible relegation background colour */
   if i >= posreg & posreg~=0 then fc = "#ffffff"    /* possible relegation font colour*/
   if i<10 then do
      if i = 1 then teams.i = upper(teams.i)
   end
      teams.i=insert("</FONT></TD></TR>",teams.i,84)
      teams.i=insert("</FONT></TD><TD BGCOLOR="""BG""" ALIGN=""CENTER"" VALIGN=""MIDDLE""><FONT COLOR="""fc""">",teams.i,71)
      teams.i=insert("</FONT></TD><TD BGCOLOR="""PC""" ALIGN=""CENTER"" VALIGN=""MIDDLE""><FONT COLOR=""#000000"">",teams.i,62)
      teams.i=insert("</FONT></TD><TD BGCOLOR="""BG""" ALIGN=""CENTER"" VALIGN=""MIDDLE""><FONT COLOR="""fc""">",teams.i,56)
      teams.i=insert("</FONT></TD><TD BGCOLOR="""BG""" ALIGN=""CENTER"" VALIGN=""MIDDLE""><FONT COLOR="""fc""">",teams.i,50)
      teams.i=insert("</FONT></TD><TD BGCOLOR="""BG""" ALIGN=""CENTER"" VALIGN=""MIDDLE""><FONT COLOR="""fc""">",teams.i,46)
      teams.i=insert("</FONT></TD><TD BGCOLOR="""BG""" ALIGN=""CENTER"" VALIGN=""MIDDLE""><FONT COLOR="""fc""">",teams.i,42)
      teams.i=insert("</FONT></TD><TD BGCOLOR="""BG""" ALIGN=""CENTER"" VALIGN=""MIDDLE""><FONT COLOR="""fc""">",teams.i,38)
      teams.i=insert("</FONT></TD><TD BGCOLOR="""BG""" ALIGN=""CENTER"" VALIGN=""MIDDLE""><FONT COLOR="""fc""">",teams.i,35)
      teams.i=insert("</FONT></TD><TD BGCOLOR="""BG""" ALIGN=""LEFT"" VALIGN=""MIDDLE""><FONT COLOR="""fc""">",teams.i,0)
      teams.i=insert("<TR><TD BGCOLOR="""BG""" ALIGN=""CENTER"" VALIGN=""MIDDLE""><FONT COLOR="""fc""">"i")",teams.i,0)
      teams.i=space(teams.i,1)
            call writeln(stats,teams.i)
      if reg ~= 0 then do
      if i = (tcount-reg) & posreg~=0 then do       /* was posreg */
         call writeln(stats,"<TR bgcolor=""#FF0000""><TD colspan=""10"" bgcolor=""#FF0000"" height=1></TD></TR>")
      end
   end
   if promo ~= 0 then do
      if i = (promo) & pospro~=0 then do        /* was pospro */
         call writeln(stats,"<TR bgcolor=""#00cc00""><TD colspan=""10"" bgcolor=""#00cc00"" height=1></TD></TR>")
      end
   end
end

call writeln(stats,"</TABLE><BR>")
call writeln(stats,"<small>Created using HTML_table rexx script for Football 1999</small><BR>")
call writeln(stats,"</DIV></BODY>")
call writeln(stats,"</HTML>")
   say
   say
   say"           Operation Successful "
   say"           -------------------- "
   say
   say"HTML_table has created the file:"
   say
   say htmlpath||lname||"tab.html"
   say
   say"This file can be viewed with your fave web Browser"
   say"For details on how to use this script look at the script"
   say"itself with a text editor such as ed...... You can change"
   say"colours,spacing......from within the script"
   say
   say"For Updates Visit the Football Support Web Site at:"
   say
   say"    http://www.blue-shantung.demon.co.uk"

exit
