#! lua

function searchdir(thisdir,textpat,recurse)
  for leaf,ftype in os.dir(thisdir) do
    if (ftype == 0x1000) or (ftype == 0x2000) then
      if recurse == "recurse" then
        searchdir(thisdir.."."..leaf,textpat,recurse)
      end
    elseif (ftype == 0xffd) or (ftype == 0xfff) then
      local linenumber = 0
      for line in io.lines(thisdir.."."..leaf) do
        linenumber = linenumber + 1
        local match = line:find(textpat)
        if match then
          if not throwback then
            swi.! "DDEUtils_ThrowbackStart"
            throwback = true
          end -- if
          swi.r[0] = 2
          swi.r[1] = 0
          swi.r[2] = thisdir.."."..leaf
          swi.r[3] = linenumber
          swi.r[4] = 0
          swi.r[5] = line
          swi.! "DDEUtils_ThrowbackSend"
        end -- if
      end -- for
    end -- if
  end -- for
end -- function


do
  local thisdir = arg[1]
  local textpat = arg[2]
  local recurse = arg[3]
  throwback = false
  searchdir(thisdir,textpat,recurse)
  if throwback then
    swi.! "DDEUtils_ThrowbackEnd"
  else
    swi.r[0] = "Sound 1 &fff5 &5000 10"
    swi.! "OS_CLI"
  end -- if
end -- do

