# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")

module Qwik
  class LoadLibrary
    def initialize
    end

    def glob(dir, glob)
      ar = list_files(dir, glob)
      add_load_path(dir)
      require_files(ar)
    end

    private

    def list_files(dir, glob)
      return Dir.glob(dir+"/"+glob).map {|f|
	f.sub(dir+"/", "")
      }
    end

    def add_load_path(dir)
      dir = "../../lib" if $debug
      $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
    end

    def require_files(ar)
      before = $".dup
      ar.each {|f|
	require f
      }
      after = $".dup
      #pp "load success", after-before if before != after
    end
  end
end

if $0 == __FILE__
  require "test/unit"
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "qwik/test-common"
  require "qwik/config"
  $debug = true
end

if $debug
  class TC_LoadLibrary < Test::Unit::TestCase
    def test_loadlib
      return if $0 != __FILE__	# just only for unit test.

      @config = Qwik::Config.new(true)
      loadlib = Qwik::LoadLibrary.new
      dir = @config.lib_dir
      org_path = $LOAD_PATH.dup
      org_libs = $".dup

      Qwik::LoadLibrary.instance_eval {
	public :list_files
      }

      # test list_files
      glob = "qwik/act-*.rb"
      files = loadlib.list_files(dir, glob)
      assert_equal(true, 0 < files.length)
      files.each {|f|
	assert_match(/\Aqwik\/act-[-a-z0-9]+\.rb\z/, f)
      }

      loadlib.glob(dir, glob) # LOAD

      # There is no change on LOAD_PATH.
      diff = $LOAD_PATH.length - org_path.length
      #qp $LOAD_PATH.length, org_path.length
      assert_equal(false, 0 < diff)

      diff = $".length - org_libs.length
      #qp $".length, org_libs.length
      assert_equal(true, 0 < diff)
    end
  end
end
