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

   ATTENDANCES PROGRAM FOR FOOTBALL REXX SUITE
  ---------------------------------------------
                   Copyright  Mark Naughton 1999


Version    Date     History
--------------------------------------------------------------------------
 1.0       281199   First release.

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

Procedure
---------

1. Check file exists.
2. Open '.sflearn' file and read in teams and attendances.
3. Write arrays out to temp file and sort.
4. Display table.
5. Read temp file and display. Exit.

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

version      = 1
input_file   = '.sflearn'
input2_file  = '.df'
attd         = '*ATD='
rfnm         = '**'
separator    = '*'
teams.       = '???'
higha.       = '???'
lowa.        = '???'
average.     = '???'
totals.      = '???'
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 :    (Attendances)"
   say
   say "Cannot read 'Data/Football.locale' for the locale settings."
   exit
end

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

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

if exists(league_file || input2_file) = 0  then do
   say
   say at_error
   say
   say fl_canfd"'"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(separator,line) = 0 then do
         line = strip(line)
         if counter = 0 then do
            teams.1 = line
            counter = 1
         end
         else do
            counter       = counter + 1
            teams.counter = line
         end
      end
   end
   close(datafile)
end
else do
   say
   say at_error
   say
   say fl_canop"'"league_file||input2_file"'"fl_foread
   exit
end

do i=1 to counter
   games.i   = 0
   higha.i   = 0
   lowa.i    = 9999999
   totals.i  = 0
   average.i = 0
end

lowa = 9999999
lowm = ""
hia  = 0
him  = ""

if open(datafile,league_file || input_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      if pos(rfnm,line) > 0 then
         lname = delstr(line,1,3)
      if pos(separator,line) = 0 then do
         name = strip(substr(line,1,30))
         match = line
         do i=1 to counter
            if name == teams.i then do
               mkr = i
            end
         end
         notin = -1
      end
      if notin = -1 & pos(attd,line) > 0 then do
         parse var line "*ATD=" attend
         games.mkr = games.mkr + 1
         if attend > higha.mkr then higha.mkr = attend
         if attend < lowa.mkr then  lowa.mkr  = attend
         totals.mkr = totals.mkr + attend
         if attend > hia then do
            hia = attend
            him = strip(substr(match,1,30))||txt_versus||strip(substr(match,41,30))
         end
         if attend < lowa then do
            lowa = attend
            lowm = strip(substr(match,1,30))||txt_versus||strip(substr(match,41,30))
         end
         notin = 0
      end
   end
   close(datafile)
end
else do
   say
   say at_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
         if teams.i ~= "" then do
            if totals.i > 0 then
               average.i = trunc(totals.i / games.i)
            else
               average.i = 0
            if lowa.i = 9999999 then lowa.i = 0
            writeln(datafile,left(higha.i,7)" "left(teams.i,30,' ')"  "left(higha.i,7)"  "left(lowa.i,7)"  "left(average.i,7)"   "left(totals.i,7)"  ("games.i")")
         end
      end
      close(datafile)
   end
   else do
      say
      say at_error
      say
      say fl_canctem
      exit
   end
   address command 'Exec/Sort7Chars '
end

say
say center(at_txt1"'"lname"'",78)
say "-------------------------------------------------------------------------------"
say
say
say at_txt2
say "-------------------------------------------------------------------------------"
say
i = 0
if counter = 1 then do
   average.1 = trunc(totals.1 / games.1)
   if lowa.1 = 9999999 then lowa.1 = 0
   say left(teams.1,30,' ')"  "left(higha.1,7)"  "left(lowa.1,7)"  "left(average.1,7)"   "left(totals.1,7)"  ("games.1")"
   say
end
else do
   if open(datafile,"RAM:Football.tempcup",'r') then do
      do while ~eof(datafile)
         line = readln(datafile)
         if line ~= "" then do
            aline = strip(substr(line,7))
            if i = 0 then do
               say upper(aline)
               i = 1
            end
            else
               say aline
         end
      end
      close(datafile)
   end
   else do
      say
      say at_error
      say
      say at_txt3
      exit
   end
   address command 'delete >NIL: RAM:Football.tempcup'
end

maxm = length(him)
if length(lowm) > maxm then maxm = length(lowm)

if him ~= "" then do
   say
   say
   say left(at_txt4,12,' ')"  :  "left(him,maxm,' ')"  :  "hia
end
if lowm ~= "" then do
   say
   say
   say left(at_txt5,12,' ')"  :  "left(lowm,maxm,' ')"  :  "lowa
end
say
say "-------------------------------------------------------------------------------"

exit