BUILD   = lepage
PARSER  = LePage/Parser.pm
GRAMMAR = LePage.grammar
PACKAGE = LePage::Parser

ALL_TESTS =

all: test

test:
	@for test in $(ALL_TESTS); do $(MAKE) TEST_$$test || exit 1; done
	@echo "ALL TESTS WERE SUCCESSFUL"

clean:
	@for test in $(ALL_TESTS); do $(MAKE) CLEAN_$$test; done
	rm -rf LePage
	rm -f $(BUILD) $(PARSER) $(GRAMMAR)

TEST_%:
	@echo "Running Test $*"
	@$(MAKE) $*_test
	@echo "  Test $* Succeeded"

CLEAN_%:
	@$(MAKE) $*_clean

ALL_TESTS += compile
compile_test: $(PARSER)
	@perl -wc $(PARSER)
        
compile_clean:

ALL_TESTS += bootstrap
bootstrap_test: $(BUILD) $(PARSER) $(GRAMMAR)
	@echo "  Generating second Parser"
	@./$(BUILD) $(GRAMMAR) LePage::Parser > Parser_bootstrap.pm
	@echo "  Comparing Parsers"
	@-diff -b $(PARSER) Parser_bootstrap.pm

bootstrap_clean:
	rm -f Parser_bootstrap.pm

ALL_TESTS += lebotconf
lebotconf_test: $(BUILD) $(PARSER) lebotconf.gr lebotconf.sample
	@echo "  Generating Parser"
	@./$(BUILD) lebotconf.gr LeBotConf > LeBotConf.pm
	@echo "  Compiling Parser"
	@perl -wc LeBotConf.pm
	@echo "  Parsing Sample"
	@perl -MLeBotConf -e '\
	$$PARSER = LeBotConf->new(); \
	$$INPUT = join("", <STDIN>); \
	$$TREE = $$PARSER->parse($$INPUT); \
	defined($$TREE) || exit(1)' < lebotconf.sample

lebotconf_clean:
	rm -f LeBotConf.pm

ALL_TESTS += directives
directives_test: $(BUILD) $(PARSER) directives.gr
	@echo "  Generating Parser"
	@./$(BUILD) directives.gr Directives > Directives.pm
	@echo "  Compiling Parser"
	@perl -wc Directives.pm

directives_clean:
	rm -f Directives.pm
