/* Insert.rexx */

options results; address MrMIDI; signal on syntax

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
list ID SLIST ATTRS MUIA_List_Entries; entries = result
popasl ID FPATH; fpath = result
temp_name = TmpFile('PIPE:MIDI_List.')
RC = 0
address command 'RequestFile >'temp_name' DRAWER "'fpath'" PATTERN "#?.(mid|midi|kar)"
  TITLE "Select MIDI files" MULTISELECT NOICONS'
if RC = 0 then do
  if open(1, temp_name, 'r') then do
    do while ~eof(1)
      if readch(1, 1) = '"' then do
        temp_list = ''
        do until temp_char = '"'
          temp_char = readch(1, 1)
          if eof(1) then break
          if temp_char ~= '"' then temp_list = temp_list||temp_char
        end
        if exists(temp_list) then do
          popasl ID PATH; pathname = result
          temp_name = TmpFile('PIPE:MIDI_Temp.')
          address command 'ReadMIDI "'temp_list'" >'temp_name
          if open(2, temp_name, 'r') then do
            null = readln(2)
            format = readln(2)
            total_tracks = readln(2)
            division = readln(2)
            temp_song_time = readln(2)
            call close(2)
            if left(null, 4) = 'MIDI' then do
              parse var format dummy': 'format
              parse var total_tracks dummy': 'total_tracks
              parse var division dummy': 'division
              parse var temp_song_time dummy': 'temp_song_time
              if index(temp_list, ',') = 0 then do
                if entries = 0 then
                  list ID SLIST INSERT POS MUIV_List_Insert_Bottom,
                    STRING temp_list','temp_song_time','format','total_tracks','division
                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
                    parse var result temp_name','dummy
                    if temp_list = temp_name then do
                      skip = 1
                      break
                    end
                  end
                  if ~skip then
                    list ID SLIST INSERT POS MUIV_List_Insert_Active,
                      STRING temp_list','temp_song_time','format','total_tracks','division
                end
              end /* of if index */
              else request ID WARN GADGETS '"OK"' '"Cannot insert the filename with comma."'
            end /* of if null */
            else request ID WARN GADGETS '"OK"' '"Corrupted MIDI file."'
          end /* of if open */
          else request ID WARN GADGETS '"OK"' '"Could not open a pipe."'
        end /* of if exists */
        else do
          temp_string = "Could not open the file '"temp_list"'."
          request ID WARN GADGETS '"OK"' temp_string
        end
      end /* of if readch */
    end /* of do while */
    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 request ID WARN GADGETS '"OK"' '"Could not open a pipe."'
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
