/* Insert.rexx */

options results; address MrMPEG; 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:MPEG_List.')
address command 'RequestFile >'temp_name 'DRAWER "'fpath'" PATTERN "#?.(mp1|mp2|mp3)"
  TITLE "Select MPEG 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:MPEG_Temp.')
          address command '"'pathname'MPEGA" -h "'first'" >'temp_name
          if open(2, temp_name, 'r') then do
            null = readln(2)
            null = readln(2)
            null = readln(2)
            null = readln(2)
            info = readln(2)
            length = readln(2)
            call close(2)
            if left(info, 13) = '   File Type:' then do
              parse var info dummy':' '[1m'detail'[22m'
              parse var length dummy':' '[1m'temp_total_time'[22m'
              if index(temp_list, ',') = 0 then do
                if entries = 0 then
                  list ID SLIST INSERT POS MUIV_List_Insert_Bottom,
                    STRING first","temp_total_time","strip(detail)
                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 first","temp_total_time","strip(detail)
                end
              end /* of if index */
              else request ID WARN GADGETS '"OK"' '"Cannot insert the filename with comma."'
            end /* of if left */
            else request ID WARN GADGETS '"OK"' '"Corrpted MPEG 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 '"first"'."
          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
