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

   MAN OF THE MATCH PROGRAM FOR FOOTBALL REXX SUITE
  --------------------------------------------------
                   Copyright  Mark Naughton 1999


Version    Date     History
--------------------------------------------------------------------------
 1.0       170699   First release.
           250899   Added error msg to file check.
           280899   Converted to use locale. Some error messages, before
                    reading the locale, will still be in English.
           141099   Added team name to player.

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

Procedure
---------

1. Check file exists.
2. Open '.sflearn' file and read in players, storing them and if the name
   appears more than once, increment the number of awards per match.
3. Write players out to temp file and sort.
4. Display table.
5. Read temp file and display players with total merits. Exit.

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

version      = 1
input_file   = '.sflearn'
homemm       = '*HM='
awaymm       = '*AM='
mmnm         = '**'
separator    = '*'
teams.       = '???'
wins.        = '???'
lines.       = '???'
counter      = 0


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 :    (ManOfTheMatch)"
   say
   say "Cannot read 'Data/Football.locale' for the locale settings."
   exit
end

dfordir = locdir"Football.locale_data"
locdir = locdir"User/ManOfTheMatch.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 :    (ManOfTheMatch)"
   say
   say "Cannot find '"locdir"' to read locale settings."
   exit
end

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

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

if open(datafile,league_file || input_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(mmnm,line) > 0 then
         lname = delstr(line,1,3)
      if pos(separator,line) = 0 then
         match_plyd = line
      if pos(homemm,line) > 0 then do
         name = delstr(line,1,4)
         notin = 0
         do i=1 to counter
            if pos(name,teams.i) > 0 then do
               wins.i = wins.i + 1
               notin = 1
            end
         end
         if notin = 0 then do
            counter = counter + 1
            teams.counter = name" ("strip(substr(match_plyd,1,30))")"
            wins.counter  = 1
         end
      end
      if pos(awaymm,line) > 0 then do
         name = delstr(line,1,4)
         notin = 0
         do i=1 to counter
            if pos(name,teams.i) > 0 then do
               wins.i = wins.i + 1
               notin = 1
            end
         end
         if notin = 0 then do
            counter = counter + 1
            teams.counter = name" ("strip(substr(match_plyd,41,30))")"
            wins.counter  = 1
         end
      end
   end
   close(datafile)
end
else do
   say
   say mm_error
   say
   say fl_canop"'"league_file||input_file"'"fl_foread
   exit
end

if counter > 1 then do
   if open(datafile,"RAM:Football.tempcup",'w') then do
      do i=1 to counter
         writeln(datafile,left(wins.i,4)"     "teams.i)
      end
      close(datafile)
   end
   else do
      say
      say mm_error
      say
      say fl_canctem
      exit
   end
   address command 'Exec/Sort4Chars '
end


say
say center(mm_txt1"'"lname"'",78)
say "-------------------------------------------------------------------------------"
say
say
say mm_txt2
say "-------------------------------------------------------------------------------"
say
i = 0
if counter = 1 then do
   say right("1)",4," ")"   "left(wins.1,4)"     "upper(teams.1)
   say
end
else do
   ll = 1
   if open(datafile,"RAM:Football.tempcup",'r') then do
      do while ~eof(datafile)
         line = readln(datafile)
         if line~="" then do
            if i = 0 then do
               say right(ll")",4," ")"   "upper(line)
               i = 1
            end
            else
               say right(ll")",4," ")"   "line
            ll = ll + 1
         end
      end
      close(datafile)
   end
   else do
      say
      say mm_error
      say
      say mm_txt3
      exit
   end
   address command 'delete >NIL: RAM:Football.tempcup'
end
say
say "-------------------------------------------------------------------------------"

exit