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

   VIEW TEAM SCHEDULE PROGRAM FOR FOOTBALL REXX SUITE
  ----------------------------------------------------
                   Copyright  Mark Naughton 1997


Version    Date     History
--------------------------------------------------------------------------
 1.0       091297   First release.
           151297   Tidied display.
           210499   Added support for CloseCup. History file would not be
                    found as it did not have the date appended to the
                    filename.
           250899   Added error msg to file check.
           280899   Converted to use locale. Some error messages, before
                    reading the locale, will still be in English.

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

Procedure
---------

1. Check file exists. If it doesn't, format a new filename from the
   original, and then check this file.
2. Open '.cfh' file and read in winners, storing them and if the name
   appears more than once, increment the number of wins for that team.
3. Read '.cfh' file in and store 'needed' lines.
4. Write teams for winners out to temp file and sort.
5. Check array and set marker if third place is found.
6. Display message. Display winners, runnersup, third place and fourth.
7. Read temp file and display winners with total wins. Exit.

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

version      = 1
input_file   = '.cfh'
first        = '*WINNER='
second       = '*RUNNERUP='
third        = '*THIRD='
fourth       = '*FOURTH='
cupnm        = '** History for '
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 :    (Cup_ViewHistory)"
   say
   say "Cannot read 'Data/Football.locale' for the locale settings."
   exit
end

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

if exists(league_file || input_file) = 0  then do
   parse var league_file nlf "_" .
   if exists(nlf || input_file) = 0 then do
      say
      say cvh_error
      say
      say fl_canfd"'"league_file || input_file"'."
      exit
   end
   league_file = nlf
end

if open(datafile,league_file || input_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(cupnm,line) > 0 then
         cupname = delstr(line,1,15)
      if pos(first,line) > 0 then do
         name = delstr(line,1,8)
         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
            wins.counter  = 1
         end
      end
   end
   close(datafile)
end
else do
   say
   say cvh_error
   say
   say fl_canop"'"league_file||input_file"'"fl_foread
   exit
end

linect = 0
if open(datafile,league_file || input_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos('**',line) = 0 & length(line) > 2 & line ~= '' then do
         linect = linect + 1
         lines.linect = line
      end
   end
   close(datafile)
end
else do
   say
   say cvh_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 cvh_error
      say
      say fl_canctem
      exit
   end
   address command 'Exec/Sort4Chars '
end

thirdp = 0
do i=1 to linect
   if pos(third,lines.i) > 0 then do
      thirdp = 1
      leave
   end
end

say
say center(cvh_txt1""cupname,78)
say "-------------------------------------------------------------------------------"
say

do i=1 to linect
   name = delstr(lines.i,1,8)
   say cvh_txt2""upper(name)
   i = i + 1
   name = delstr(lines.i,1,10)
   say cvh_txt3""name
   if thirdp = 1 then do
      i = i + 1
      name = delstr(lines.i,1,7)
      say cvh_txt4""name
      i = i + 1
      name = delstr(lines.i,1,8)
      say cvh_txt5""name
      say
   end
   else
      say
end
say
say
say cvh_txt6
say "-------------------------------------------------------------------------------"
say
i = 0
if counter = 1 then do
   say left(wins.1,4)"     "upper(teams.1)
   say
end
else do
   if open(datafile,"RAM:Football.tempcup",'r') then do
      do while ~eof(datafile)
         line = readln(datafile)
         if i = 0 then do
            say upper(line)
            i = 1
         end
         else
            say line
      end
      close(datafile)
   end
   else do
      say
      say cvh_error
      say
      say cvh_txt7
      exit
   end
   address command 'delete >NIL: RAM:Football.tempcup'
end
say "-------------------------------------------------------------------------------"

exit