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

  UPDATE SCHEDULE FILE PROGRAM FOR FOOTBALL REXX SUITE
  ----------------------------------------------------
                   Copyright  Mark Naughton 1998


Version    Date     History
--------------------------------------------------------------------------

 1.0       011098   Created. Found problem where the updates are not
                    successfully made - fixed. Updated messages.
 1.1       050599   Added code to check the schedule file to see if it
                    contains the right number of matches for the number of
                    teams playing a specified number of times. Added
                    support for playing an odd number of times and for
                    schedules for teams playing more than twice.

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

Procedure
---------

1. Check files exist. Get schedule definition filename from '.df' file.
2. Read specified schedule definition file. Check schedule contains the
   right number of matches before proceeding.
3. Read schedules and store dates/weeks.
4. Give error if the number of teams does not equal the number of lines in
   the schedule file.
5. Write the dates/weeks to a file then use an external program to sort
   them, writing back to the file.
6. Read dates into array and delete file.
7. Format dates array with season start date.
8. Format and write '.sf' files.
9. Read all data from Learn (without '*') into an array. Close file.
10.Open Schedule file and read into an array. Close file.
11.Update Schedule array with data from Learn but only if a match hasn't
   been played.
12.Write to Schedule file. Close file. Exit.

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

version      = 1
league_file  = "Data/" || strip(league_file)
input_file   = '.df'
input2_file  = '.schd'
input3_file  = '.sflearn'
output_file  = '.sf'
title        = '*LEAGUE_NAME='
autosched    = "*AUTOSCHD="
playother    = '*PLAY_OTHER='
teams.       = '???'
dateweeks.   = '???'
tdateweeks.  = '???'
schedules2.  = '???'
schedules4.  = '???'
schedules6.  = '???'
schedules8.  = '???'
schedules0.  = '???'
selines.     = '???'
sdlines.     = '???'
autos        = 0
tcount       = 0
ttc          = 0
separator    = '*'
not_played   = '__   __'
months       = "January February March April May June July August September October November December"


if exists(league_file || input_file) = 0 then exit

tcount = 0
if open(datafile,league_file || input_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      line = strip(line)
      if pos(separator,line) > 0 then do
         if pos(autosched,line) > 0 then do
            autofile = delstr(line,1,10)
            autos = 1
         end
         if pos(title,line) > 0 then do
            parse var line "*LEAGUE_NAME=" league_title
            league_title = strip(league_title)
         end
         if pos(playother,line) > 0 then
            playo = delstr(line,1,12)

       end
       if pos(separator,line) = 0 & line ~="" then do
            tcount = tcount + 1
            teams.tcount = line
       end
   end
   close(datafile)
end
else do
   say
   say "ERROR :    (UpdateScheduleScores)"
   say
   say "Cannot open '"league_file || input_file"' for reading."
   exit
end
                                          /* Automatic Schedule creation */
if autos = 1 then do

   if exists("Data/"autofile||input2_file) = 0 then do
      say
      say "ERROR :    (UpdateScheduleScores)"
      say
      say "Cannot find 'Data/"autofile||input2_file"'."
      exit
   end

   ct = 0
   sct2= 0
   sct4= 0
   sct6= 0
   sct8= 0
   sct0= 0
   weeks = 0
   dates = 0
   sch = 1


   if open(datafile,"Data/"autofile||input2_file,'r') then do
      mkct = ((tcount - 1) * playo) * (tcount/2)
      kmct = 0
      do while ~eof(datafile)
         line = readln(datafile)
         if line ~= '' & pos(separator,line) = 0 then do
            counter = words(line)
            do i=1 to counter
               if word(line,i) ~= 0 then
                  kmct = kmct + 1
            end
         end
      end
      close(datafile)
   end
   else do
      say
      say "ERROR :    (UpdateScheduleScores)"
      say
      say "Cannot find 'Data/"autofile||input2_file"' for reading."
      say
      say
      say "The definition file, '"league_file || input_file"' has been created."
      exit
   end
   if mkct ~= kmct then do
      say
      say "ERROR :    (UpdateScheduleScores)"
      say
      say "UpdateScheduleScores has found that the schedule file 'Data/"autofile||input2_file"'"
      say "does not contain the correct number matches for "tcount" teams to play each other"
      say playo" times. It contains "kmct" matches, where as it should contain "mkct" matches."
      say "You will have to edit the schedule file or change the settings required when"
      say "recreating the league or before running this program again."
      exit
   end

   if open(datafile,"Data/"autofile||input2_file,'r') then do
      do while ~eof(datafile)
         line = readln(datafile)
         if pos("*WEEKS",line) then do
            weeks=1
         end
         if pos("*DATES=",line) then do
            startdate=substr(line,8,8)
            dates = 1
         end
         if pos("*NEXT",line) > 0 then sch = sch + 1
         if line ~= '' & pos(separator,line) = 0 then do
            if sch = 1 then do
               sct2 = sct2 + 1
               schedules2.sct2 = line
            end
            if sch = 2 then do
               sct4 = sct4 + 1
               schedules4.sct4 = line
            end
            if sch = 3 then do
               sct6 = sct6 + 1
               schedules6.sct6 = line
            end
            if sch = 4 then do
               sct8 = sct8 + 1
               schedules8.sct8 = line
            end
            if sch = 5 then do
               sct0 = sct0 + 1
               schedules0.sct0 = line
            end
            counter = words(line)
            do i=1 to counter
               sdate = word(line,i)
               if sdate = 0 then
                  iterate
               if ct = 0 then do
                  ct = ct + 1
                  dateweeks.ct = sdate
               end
               else do
                  ij = 0
                  do j=1 to ct
                     if sdate = dateweeks.j then do
                        ij = 1
                        leave
                     end
                  end
                  if ij = 0 then do
                     ct = ct + 1
                     dateweeks.ct = sdate
                  end
               end
            end
         end
      end
      close(datafile)
   end
   else do
      say
      say "ERROR :    (UpdateScheduleScores)"
      say
      say "Cannot find 'Data/"autofile||input2_file"' for reading."
      exit
   end
   sct = (sct2 + sct4 + sct6 + sct8 + sct0) / sch

   if counter ~= sct then do
      say
      say "ERROR :    (UpdateScheduleScores)"
      say
      say "The schedule definition file specified only has "sct" scheduled"
      say "teams whereas there are "tcount" teams in the league."
      say
      say "Creation of files aborted. Re-specify correct schedule."
      say
      say "Definition file    :  "league_file||input_file
      say "Schedule Def. file :  Data/"autofile||input2_file
      say
      exit
   end

   if open(datafile2,"RAM:schd.temp",'w') then do
      do j=1 to ct
         writeln(datafile2,dateweeks.j)
      end
      close(datafile2)
   end
   else do
      say
      say "ERROR :    (UpdateScheduleScores)"
      say
      say "Cannot create temporary file in RAM:."
      exit
   end

   if weeks = 1 then
      address command 'sort RAM:schd.temp RAM:schd.temp'
   if dates = 1 then
      address command 'Exec/SortWkDts'

   if open(datafile2,"RAM:schd.temp",'r') then do
      do j=1 to ct
         dateweeks.j = readln(datafile2)
      end
      close(datafile2)
   end
   else do
      say
      say "ERROR :    (UpdateScheduleScores)"
      say
      say "Cannot find temporary file in RAM:."
      exit
   end

   address command 'delete >NIL: RAM:schd.temp'

   if dates = 1 then do
      do j=1 to ct
         if dateweeks.j = startdate then do
            do i=1 to j-1
               tdateweeks.i = dateweeks.i
            end
            k = 1
            do i=j to ct
               dateweeks.k = dateweeks.i
               k = k + 1
            end
            do i=1 to j-1
               dateweeks.k = tdateweeks.i
               k = k + 1
            end
            leave
         end
      end
   end

   if open(outfile,league_file || output_file,"w") then do
      writeln(outfile,"*")
      writeln(outfile,"**" league_title)
      writeln(outfile,"*")
      writeln(outfile,"*")

      matches = 0
      do i=1 to ct
         if weeks = 1 then
            writeln(outfile,"*Week: "dateweeks.i)
         if dates = 1 then do
            mnth = substr(dateweeks.i,3,2)
            ndate= substr(dateweeks.i,5,4)||mnth||substr(dateweeks.i,1,2)
            weekd= date('w',ndate,'s')
            writeln(outfile,"*Date: "weekd" "substr(dateweeks.i,1,2)" "word(months,mnth)" "substr(dateweeks.i,5,4))
         end
         writeln(outfile,"*")
         do k=1 to counter
            do j=1 to counter
               do l=1 to sch
                  if l=1 then sdate = word(schedules2.k,j)
                  if l=2 then sdate = word(schedules4.k,j)
                  if l=3 then sdate = word(schedules6.k,j)
                  if l=4 then sdate = word(schedules8.k,j)
                  if l=5 then sdate = word(schedules0.k,j)
                  if dateweeks.i = sdate then do
                     writech(outfile,left(teams.k,30))
                     writeln(outfile," __   __ " teams.j)
                     matches = matches + 1
                     leave
                  end
               end
            end
         end
         writeln(outfile,"*")
      end
      writeln(outfile,"*")
      close(outfile)
   end
   else do
      say
      say "ERROR :    (UpdateScheduleScores)"
      say
      say "Unable to write to '"league_file || output_file"'."
      exit
   end
end
else do
   say
   say "ERROR :    (UpdateScheduleScores)"
   say
   say "This definition file, "league_file||input_file", does not use a"
   say "schedule definition file. It is not automatically scheduled so"
   say "this will not work. Use 'UpdateScores' instead."
   say
   exit
end

secount = 0
if open(datafile,league_file||input3_file,'r') then do
   do while ~eof(datafile)
      line = readln(datafile)
      line = strip(line)
      if pos(separator,line) = 0 & line ~= "" then do
         secount         = secount + 1
         selines.secount = line
      end
   end
   close(datafile)
end
else do
   say
   say "ERROR :    (UpdateScheduleScores)"
   say
   say "Cannot open '"league_file||input3_file"' for re-reading."
   exit
end

sdcount = 0
if open(datafile3,league_file || output_file,'r') then do
   do while ~eof(datafile3)
       line = readln(datafile3)
       line = strip(line)
       if line ~= "" then do
          sdcount         = sdcount + 1
          sdlines.sdcount = line
       end
   end
   close(datafile3)
end
else do
   say
   say "ERROR :    (UpdateScheduleScores)"
   say
   say "Cannot open '"league_file || output_file"' for reading."
   exit
end

i    = 0
done = 0

do i=1 to secount
   shome = strip(substr(selines.i,1,30))
   saway = strip(substr(selines.i,41,30))
   do j=1 to sdcount
      home  = strip(substr(sdlines.j,1,30))
      away  = strip(substr(sdlines.j,41,30))
      if shome = home & saway = away & pos(not_played,sdlines.j) > 0 then do
         sdlines.j = selines.i
         leave
      end
   end
end


/*
do j=1 to sdcount
   shome = strip(substr(sdlines.j,1,30))
   saway = strip(substr(sdlines.j,41,30))
   do i=1 to secount
      home  = strip(substr(selines.i,1,30))
      away  = strip(substr(selines.i,41,30))
      if shome = home & saway = away & pos(not_played,sdlines.j) > 0 then do
         sdlines.j = selines.i
         k = k + 1
         leave
      end
   end
   if k = secount then leave
end
*/

if open(datafile3,league_file || output_file,'w') then do
   do j=1 to sdcount
      writeln(datafile3,sdlines.j)
   end
   close(datafile3)
end
else do
   say
   say "ERROR :    (UpdateScheduleScores)"
   say
   say "Cannot open '"league_file || output_file"' for writing."
   exit
end

say
say "SUCCESS :    (UpdateScheduleScores)"
say
say "This program has taken the schedule definition file,"
say "'Data/"autofile||input2_file"' and the results from '"league_file||input3_file"'"
say "and has recreated '"league_file||output_file"'."
say

exit