require 'monitor'
require 'base/kpsefast'

class KpseTrees < Monitor

    def initialize
        @trees = Hash.new
        @monitor = Monitor.new
    end

    def choose(filenames,environment)
        current = filenames.join('|')
        # @monitor do
            unless @trees[current] then
                puts "loading tree #{current}"
                @trees[current] = KpseFast.new
                @trees[current].push_environment(environment)
                @trees[current].load_cnf(filenames)
                @trees[current].expand_variables
                @trees[current].load_lsr
            end
            puts "enabling tree #{current}"
        # end
        current
    end

    def set(tree,key,value)
        case key
            when 'progname' then @trees[tree].progname = value
            when 'engine'   then @trees[tree].engine   = value
            when 'format'   then @trees[tree].format   = value
        end
    end
    def load_cnf(tree)
        @trees[tree].load_cnf
    end
    def load_lsr(tree)
        @trees[tree].load_lsr
    end
    def expand_variables(tree)
        @trees[tree].expand_variables
    end
    def expand_braces(tree,str)
        @trees[tree].expand_braces(str)
    end
    def expand_path(tree,str)
        @trees[tree].expand_path(str)
    end
    def expand_var(tree,str)
        @trees[tree].expand_var(str)
    end
    def show_path(tree,str)
        @trees[tree].show_path(str)
    end
    def var_value(tree,str)
        @trees[tree].var_value(str)
    end
    def find_file(tree,filename)
        @trees[tree].find_file(filename)
    end
    def find_files(tree,filename,first)
        @trees[tree].find_files(filename,first)
    end

end
