# CREDIT George Moschovitis

class Dir

  # Recursively scan a directory and pass each file
  # to the given block.
  def self.ls_r(path='.', &block)
    list = []
    stoplist = ['.', '..']
    Dir.foreach(path) do |f|
      next if stoplist.include?(f)
      filename = path + '/' + f
      list << filename
      block.call(filename) if block
      if FileTest.directory?(filename) and not FileTest.symlink?(filename)
        list.concat( Dir.recurse(filename, &block) )
      end
    end
    list
  end

  class << self
    # Alias for Dir#ls_r
    alias_method( :recurse, :ls_r )
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

# TODO This test needs good Dir mockup.

=begin #no test yet

  require 'test/unit'

  class TCDir < Test::Unit::TestCase

    def test_ls_r
      td = Dir.pwd
      Dir.chdir $TESTDIR
        r = ["A", "A/B","A/B/C.txt", "A/B.txt", "A.txt"].collect{ |e| 
          File.join( 'ls_r', e )
        }
        fs = Dir.ls_r( 'ls_r' )
        assert_equal( r, fs, Dir.pwd  )
      Dir.chdir td
    end

    def test_recurse
      td = Dir.pwd
      Dir.chdir $TESTDIR
        r = ["A", "A/B","A/B/C.txt", "A/B.txt", "A.txt"].collect{ |e| 
          File.join( 'ls_r', e )
        }
        fs = Dir.recurse( 'ls_r' )
        assert_equal( r, fs, Dir.pwd )
      Dir.chdir td
    end

  end

=end
