/* ListLoad.rexx */

options results; address MrMIDI; signal on syntax; parse arg mode

ASLFR_DoPatterns	= 0x8008002E;	ASLFR_InitialDrawer	= 0x80080009
ASLFR_InitialFile	= 0x80080008;	ASLFR_InitialPattern	= 0x8008000A
MUIA_List_Active	= 0x8042391c;	MUIA_List_Entries	= 0x80421654
MUIA_Window_Sleep	= 0x8042e7db;	MUIV_List_Active_Top	= -2
MUIV_List_Insert_Active = -1;		MUIV_List_Insert_Bottom = -3
MUIV_List_Insert_Sorted = -2

window ID WIND1 ATTRS MUIA_Window_Sleep 1
window ID WIND2 ATTRS MUIA_Window_Sleep 1
RC = 0
if mode = 1 then do
  aslrequest ATTRS ASLFR_InitialDrawer '"ENVARC:"' ASLFR_InitialFile,
    '"MrMIDI.list"' ASLFR_InitialPattern '"#?.list"' ASLFR_DoPatterns 1,
    TITLE '"Load a list"'
  temp_list = result
  end
else if mode = 2 then do
  aslrequest ATTRS ASLFR_InitialDrawer '"ENVARC:"' ASLFR_InitialFile,
    '"MrMIDI.list"' ASLFR_InitialPattern '"#?.list"' ASLFR_DoPatterns 1,
    TITLE '"Include a list"'
  temp_list = result
  end
else temp_list = "ENV:MrMIDI.list"
if RC = 0 then do
  if open(1, temp_list, 'r') then do
    if mode = 1 then call Clear.rexx
    list ID SLIST ATTRS MUIA_List_Entries; entries = result
    do forever
      temp = readln(1)
      if eof(1) then leave
      if entries = 0 then
        list ID SLIST INSERT POS MUIV_List_Insert_Bottom STRING temp
      else do
        list ID SLIST ATTRS MUIA_List_Entries; temp_max = result
        skip = 0
        do i = 0 to temp_max - 1
          list ID SLIST POS i
          if temp = result then do
            skip = 1
            break
          end
        end
        if ~skip then
          list ID SLIST INSERT POS MUIV_List_Insert_Active STRING temp
      end /* of if entries */
    end /* of do forever */
    call close(1)
    list ID SLIST ATTRS MUIA_List_Entries; temp_max = result
    setvar max temp_max
    text ID MAX LABEL "\033b\033r"right(temp_max, 3)
    temp_total_time = 0
    do i = 0 to temp_max - 1
      list ID SLIST POS i
      parse var result dummy','temp_song_time','dummy2
      temp_total_time = temp_total_time + left(temp_song_time, 2) * 60 + right(temp_song_time, 2)
    end
    temp_string = right(temp_total_time % 3600, 2, 0)||":"||,
      right(temp_total_time // 3600 % 60, 2, 0)":"right(temp_total_time // 60, 2, 0)
    setvar total_time temp_string
    text ID TOTAL LABEL "\033b\033r"temp_string
    if entries = 0 then list ID SLIST ATTRS MUIA_List_Active MUIV_List_Active_Top
  end /* of if open */
  else do
    temp_string = "Could not open the file '"temp_list"'."
    request ID WARN GADGETS '"OK"' temp_string
  end
end /* of if RC */
window ID WIND2 ATTRS MUIA_Window_Sleep 0
window ID WIND1 ATTRS MUIA_Window_Sleep 0
return

syntax:
  request ID WARN GADGETS '"OK"' '"\033b\033cError 'RC' in line 'SIGL':\033n\012\033c'errortext(RC)'"'
  window ID WIND2 ATTRS MUIA_Window_Sleep 0
  window ID WIND1 ATTRS MUIA_Window_Sleep 0
  return
