local function Logwrite(...)
end

local function gen_search( text )
   Logwrite "Scratchpad.search text='#{text}'"

   local pretext
   repeat
      pretext = text
      text = text:gsub("%(%(", "( (")
      text = text:gsub("%)%)", ") )")
   until text == pretext 

   print(text)

   local qstr = ''
   local do_and
   local do_or
   local do_not
   
   local wordlist = {}
   local spcndx = 1
   while true do
      local b,e = text:find('%s+',spcndx)
      if b then
         table.insert(wordlist,text:sub(spcndx,b-1))
         spcndx = e+1
      else
         table.insert(wordlist,text:sub(spcndx))
         break
      end
   end

   for _,v in ipairs(wordlist) do
   --   print(string.format('"%s"',v))
   end
   --print ' ------------'

   local function consume_logic()
      qstr = do_and and (qstr .. " and") or qstr
      qstr = do_or and (qstr .. " or") or qstr
      do_and = true
      do_or  = false
   end

--[[--
      if word =~ /^\((.*)/ then
        word = $1
        consume_logic.call()
        qstr += ' ( '
        do_and = false
        next  if word.length < 1
      end
--]]--
   for _,word in ipairs(wordlist) do

      local b,e,match = word:find('^%((%a*)') 
      if b then
         -- print('openparen',match)
         consume_logic()
         qstr = qstr .. ' ( '
         do_and = false
         word = match
      end

      if #word > 0 then
         if word == 'and' then
            ;
         elseif word == 'not' then
            do_not = true
         elseif word == 'or' then
            do_or = true
            do_and = false
         else
            local doit = true
            local closer = ''
            local b,e,match = word:find('(%a*)%)$' )
            if b then
               word = match
               closer = ' ) '
               if #word < 1 then
                  qstr = qstr .. closer
                  doit = false
               end
            end
            if doit then
               consume_logic()
               if do_not then
                  qstr = qstr .. " (detail not like '%" .. word .. "%' and title not like '%" .. word .. "%')" .. closer
                  do_not = false
               else
                  qstr = qstr .. " (detail like '%" .. word .. "%' or title like '%" .. word .. "%')" .. closer
               end
            end
         end -- not and, not, or or
      end --, #word > 0

   end -- for -loop

   -- print( 'qstr:', qstr )
   return qstr
end


--gen_search '(((mattm or miller) and (not discount)) or rcm) and reward'

return {
   megasearch = gen_search
}


--[[--
      if word == 'and' then
        true # 'and' is assumed if not otherwise spec'ed
      elsif word == 'not' then
        do_not = true
      elsif word == 'or' then
        do_or  = true
        do_and = false
      else
        closer = ''
        if word =~ /(.*)\)$/ then
          word = $1
          closer = ' ) '
          if word.length < 1 then
            qstr += closer
            next
          end
        end

        consume_logic.call()

        if do_not then
          qstr += " (details not like '%#{word}%' and title not like '%#{word}%')" + closer
          do_not = false
        else
          qstr += " (details like '%#{word}%' or title like '%#{word}%')" + closer
        end

      end
    }
    sql = "select * from #{tableName} where #{qstr} order by date_modified desc"
    Log.write "Searching: '#{qstr}' -- sql: '#{sql}' --  wordlist: #{wordlist.join(':')} text=#{text}"
    queryset = $db.execute( sql )
    queryset
--]]--

--gen_search('foo and bar and gaba')
--gen_search('(foo and bar) or zoobie')
-- gen_search('(not (foo and bar)) or zoobie')
--gen_search('((foo and bar) or zoobie) and gaba')