/* Mode=Run */
/* ***********************************************************************

   CUP BOOKINGS PROGRAM FOR FOOTBALL REXX SUITE
  ----------------------------------------------
                   Copyright  Mark Naughton 2000


Version    Date     History
--------------------------------------------------------------------------
 1.0       040100   First release.
           230100   Fixed bug where incorrect data was being picked up.
           040600   Ported from Bookings.

**************************************************************************

Procedure
---------

1. Read locale settings and data.
2. Read teams and league name from df file.
3. Read sflearn file and collate all cards attributed to each player,
   adding them to the list as we go. Sort players on highest total of cards
   where yellow=1pt and red=2pt. Add cards to array for each team.
4. Sort teams with cards and then display as the fair play table.
5. Read sort temporary file and display players, then exit.

************************************************************************** */
PARSE ARG league_stuff

version      = 1
title        = '*CUP_TITLE='
input_file   = '.cfstats'
input2_file  = '.cf'
separator    = '*'
homet        = '*HT='
awayt        = '*AT='
teams.       = '???'
tm_ct        = 0
players.     = '???'
games.       = '???'
prcards.     = '???'
rcards.      = '???'
pycards.     = '???'
ycards.      = '???'
counter      = 0
wanted       = '*HY= *HR= *HYR *AY= *AR= *AYR'
yellowc      = 'Y='
redc         = 'R='
yredc        = 'YR='
barrier      = 0      /* all players with more than this combined total are in the table */
                      /* as the table can be quite large and the program fail */
                      /* 3 equates to one yellow and one red or three yellows */


league_file = "Data/" || league_stuff

if open(datafile,"Data/Football.locale",'r') then do
   line = readln(datafile)
   locdir = strip(line)
   close(datafile)
end
else do
   say
   say "ERROR :    (Cup_Bookings)"
   say
   say "Cannot read 'Data/Football.locale' for the locale settings."
   exit
end

locdir = locdir"User/Cup_Bookings.data"

if open(datafile,"ENV:FootballRXPath",'r') then do
   line = readln(datafile)
   rxdir = strip(line)
   close(datafile)
end
else
   rxdir = "SYS:Rexxc/"

if exists(locdir) > 0 then do
  address command rxdir'rx 'locdir
  VarCount = getclip('VarCount')
  do i = 1 to VarCount
    interpret getclip('var.'i)
  end
end
else do
   say
   say "ERROR :    (Cup_Bookings)"
   say
   say "Cannot find '"locdir"' to read locale settings."
   exit
end

if exists(league_file || input_file) = 0  then do
   say
   say bk_error
   say
   say bk_one"'"league_file || input_file"'."
   exit
end

if exists(league_file || input2_file) = 0  then do
   say
   say bk_error
   say
   say bk_one"'"league_file || input2_file"'."
   exit
end

if open(datafile,league_file || input2_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(title,line) > 0 then
         league_title = strip(substr(line,12,35))
      if pos(separator,line) = 0 & line ~= "" then do
         line = strip(line)
         if tm_ct = 0 then do
            teams.1 = line
            tm_ct = 1
         end
         else do
            tm_ct       = tm_ct + 1
            teams.tm_ct = line
         end
      end
   end
   close(datafile)
end
else do
   say
   say bk_error
   say
   say bk_two"'"league_file||input2_file"'"bk_three
   exit
end

do i=1 to tm_ct
   ycards.i = 0
   rcards.i = 0
end
do i=1 to 1000
   pycards.i = 0
   prcards.i = 0
end

yellred = 0
if open(datafile,league_file || input_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(homet,line) > 0 then
         home_team = strip(substr(line,5,30))
      if pos(awayt,line) > 0 then
         away_team = strip(substr(line,5,30))

      if pos(separator,line) > 0 then do
         sfp = substr(line,1,4)
         if pos(sfp,wanted) > 0 then do
            if pos('=',sfp) = 0 then
               name = delstr(line,1,5)
            else
               name = delstr(line,1,4)
            name = strip(name)
            if pos(yellowc,line) > 0 & pos(yredc,line) = 0 then do
               if pos("*H",line) > 0 then
                  searchfp = home_team
               else
                  searchfp = away_team
               do ii=1 to tm_ct
                  if pos(searchfp,teams.ii) > 0 then
                     ycards.ii = ycards.ii + 1
               end
               plyfnd = 0
               do f=1 to counter
                  if pos(name" ("searchfp")",players.f) > 0 then do
                     pycards.f = pycards.f + 1
                     plyfnd = 1
                     leave
                  end
               end
               if plyfnd = 0 then do
                  counter = counter + 1
                  players.counter = name" ("searchfp")"
                  pycards.counter = pycards.counter + 1
               end
               yellred = 1
            end
            if pos(yredc,line) > 0 then do
               if pos("*H",line) > 0 then
                  searchfp = home_team
               else
                  searchfp = away_team
               do ii=1 to tm_ct
                  if pos(searchfp,teams.ii) > 0 then do
                     ycards.ii = ycards.ii + 1
                     rcards.ii = rcards.ii + 1
                  end
               end
               plyfnd = 0
               do f=1 to counter
                  if pos(name" ("searchfp")",players.f) > 0 then do
                     prcards.f = prcards.f + 1
                     pycards.f = pycards.f + 1
                     plyfnd = 1
                     leave
                  end
               end
               if plyfnd = 0 then do
                  counter = counter + 1
                  players.counter = name" ("searchfp")"
                  prcards.counter = prcards.counter + 1
                  pycards.counter = pycards.counter + 1
               end
               yellred = 1
            end
            if pos(redc,line) > 0 & pos(yredc,line) = 0 then do
               if pos("*H",line) > 0 then
                  searchfp = home_team
               else
                  searchfp = away_team
               do ii=1 to tm_ct
                  if pos(searchfp,teams.ii) > 0 then
                     rcards.ii = rcards.ii + 1
               end
               plyfnd = 0
               do f=1 to counter
                  if pos(name" ("searchfp")",players.f) > 0 then do
                     prcards.f = prcards.f + 1
                     plyfnd = 1
                     leave
                  end
               end
               if plyfnd = 0 then do
                  counter = counter + 1
                  players.counter = name" ("searchfp")"
                  prcards.counter = prcards.counter + 1
               end
               yellred = 1
            end
         end
      end
   end
   close(datafile)
end
else do
   say
   say bk_error
   say
   say bk_two"'"league_file||input_file"'"bk_three
   exit
end

t = 0
if counter > 1 then do
   if open(datafile,"RAM:Football.tempcup",'w') then do
      do i=1 to counter
         wins = pycards.i + (prcards.i * 2)
         if wins > barrier then do
            writeln(datafile,left(wins,5)"     "left(players.i,35,' ')" "left(pycards.i,6)"  "prcards.i)
            t = t + 1
         end
      end
      close(datafile)
   end
   else do
      say
      say bk_error
      say
      say bk_four
      exit
   end
   if t > 1 then address command 'Exec/Sort5Chars '
   if counter > 1 then counter = t
   address command 'c:Rename >NIL: RAM:Football.tempcup as RAM:Football.tempcup2'
end

say
say center(bk_txt1"'"league_title"'",78)
say "-------------------------------------------------------------------------------"
say
say

if yellred > 0 then do
   if open(datafile,"RAM:Football.tempcup",'w') then do
      do ii=1 to tm_ct
         writeln(datafile,left(ycards.ii+(rcards.ii*2),4)"     "left(teams.ii,30)"   "right(ycards.ii,4)"   "right(rcards.ii,4))
      end
      close(datafile)
   end
   else do
      say
      say bk_error
      say
      say bk_four
      exit
   end
   address command 'Exec/Sort4Chars '

   ii = 0
   if open(datafile,"RAM:Football.tempcup",'r') then do
      do while ~eof(datafile)
         line = readln(datafile)
         if line ~= "" then do
            parse var line total rest
            ii = ii + 1
            ycards.ii = strip(rest)"   "right(total,5)
         end
      end
      close(datafile)
      say
   end
   else do
      say
      say bk_error
      say
      say bk_txt3
      say
   end
   address command 'delete >NIL: RAM:Football.tempcup'

   say
   say bk_fplay
   say bk_fplay_ln
   say bk_fplay1
   say bk_fplay1_ln
   do ii=1 to tm_ct
      say right(ii,3)") "ycards.ii
   end
   say
   say
end

say
say
if counter > 0 then say bk_txt2
say "-------------------------------------------------------------------------------"
if counter = 0 then do
   address command 'delete >NIL: RAM:Football.tempcup2'
   exit
end
i = 0
if counter = 1 then do
   say left(players.1,35,' ')" "left(ycards.i,6)"  "left(rcards.i,4)
   say
end
else do
   if open(datafile,"RAM:Football.tempcup2",'r') then do
      do while ~eof(datafile)
         line = readln(datafile)
         line = delword(line,1,1)
         if line ~= "" then do
            if i = 0 then do
               say upper(line)
               i = 1
            end
            else
               say line
         end
      end
      close(datafile)
   end
   else do
      say
      say bk_error
      say
      say bk_txt3
      exit
   end
   address command 'delete >NIL: RAM:Football.tempcup2'
end
say "-------------------------------------------------------------------------------"

exit