# Include this file at the end of the Makefile.am of your Alexina lexicon, which must define before that the following variables:
# LANGUAGE
# LEXFILES
# DOC (not mandatory)

AUTOMAKE_OPTIONS = foreign

pkgdata_DATA = $(LEXFILES) \
	       constructions \
	       templates \
	       cmpnd \
	       amlgm \
	       pctabr \
	       encoding

EXTRA_DIST = morpho.$(LANGUAGE) \
	     $(srcdir)/*.ilex \
	     $(srcdir)/*.mf \
	     $(DOC) \
	     LICENSE \
	     uw \
	     constructions \
	     templates \
	     amlgm

CLEANFILES =	*.lex *.f *.dls *.df *.fsa *.tbl *.flt cmpnd pctabr constr.pl encoding \
		morpho.$(LANGUAGE).* $(LANGUAGE).morpho

LC_COLLATE = C
LC_CTYPE = C
LC_ALL = C
LANG = C

install-data-hook:
	@echo Creating symlink from $(datadir)/alexina/$(LANGUAGE) to $(pkgdatadir).
	test -d "$(datadir)/alexina" || mkdir $(datadir)/alexina
	chmod +w $(datadir)/alexina
	for p in `ls $(datadir)/alexina/*`; do chmod +w $(datadir)/alexina/*; done;
	if test -L "$(datadir)/alexina/$(LANGUAGE)"; then rm $(datadir)/alexina/$(LANGUAGE); fi;
	ln -s $(pkgdatadir) $(datadir)/alexina/$(LANGUAGE)

# to do: uninstall-data-hook?

.SECONDARY: $(LANGUAGE).morpho

%.morpho: morpho.% $(alexinatoolsdir)/morpho2msa.pl
	@echo Building morphological scripts for language $(LANGUAGE); \
	$(alexinatoolsdir)/morpho2msa.pl $(srcdir)/morpho.$* -s -od=$(top_builddir)
	@echo -n "  Compiling scripts";\
	$(PERLCC_OR_CP) morpho.$*.dir.pl $(PERLCC_OR_CP_INTERARG) morpho.$*.dir
	@echo -n .;\
	$(PERLCC_OR_CP) morpho.$*.rev.pl $(PERLCC_OR_CP_INTERARG) morpho.$*.rev
	@echo -n .;\
	$(PERLCC_OR_CP) morpho.$*.der.pl $(PERLCC_OR_CP_INTERARG) morpho.$*.der
	@echo -n .;\
	$(PERLCC_OR_CP) morpho.$*.pder.pl $(PERLCC_OR_CP_INTERARG) morpho.$*.pder
	@echo done;\
	chmod +x morpho.$*.dir ;\
	chmod +x morpho.$*.der ;\
	chmod +x morpho.$*.pder ;\
	chmod +x morpho.$*.rev ;\
	touch $@

%.f: $(srcdir)/%.ilex $(srcdir)/%.mf %.df $(LANGUAGE).morpho \
     $(alexinatoolsdir)/add_lemma_uid.pl $(alexinatoolsdir)/add_mf_lemma_uid.pl
	@echo Building $@; \
	cut -f1-3 $(srcdir)/$*.ilex | grep -v "^>" | grep -v "	0	" | ./morpho.$(LANGUAGE).dir -vars -nosep -synt -onerad \
		| $(PERL) -an -F"\t" -e 'chomp $$F[-1]; print join("\t",$$F[2], $$F[0], $$F[1], $$F[3], $$F[5])."\n";' \
		>> $$$$af; \
	cut -f2,5 $$$$af | sort -u > $$$$af1;\
	grep "	0	" $(srcdir)/$*.ilex | $(alexinatoolsdir)/add_lemma_uid.pl > $$$$ilex0;\
	cat $$$$ilex0 | cut -f1,3 | sort -u > $$$$af2;\
	cat $$$$af1 $$$$af2 | $(LEXED) -p $$$$s build 2>/dev/null;\
	cat $(srcdir)/$*.mf | $(alexinatoolsdir)/add_mf_lemma_uid.pl $$$$ilex0 > $$$$mf1; \
	cut -f2 $$$$mf1 | $(LEXED) -p $$$$s consult 2>/dev/null > $$$$mf2;\
	paste $$$$mf1 $$$$mf2 > $$$$mf ;\
	cat $*.df | grep -vE '^(#|[ 	]*$$)' \
	        | $(PERL) -an -F"\t" -e 'chomp $$F[-1]; print join("\t",$$F[2], $$F[0], $$F[1], $$F[3], $$F[5])."\n";' > $$$$df;\
	sort -u $$$$af $$$$mf $$$$df > $@;\
	rm $$$$*

%.dls: $(srcdir)/%.ilex $(LANGUAGE).morpho $(alexinatoolsdir)/prepare_der.pl
	@echo "=================== Building $* files ==================="
	@echo Building $@; \
	echo "###### File $@ ######" > $@;\
	cat $< | grep -v "	0$$" | $(alexinatoolsdir)/prepare_der.pl \
		| ./morpho.$(LANGUAGE).der -nosep | cut -f3- \
		>> $@

%.df: %.dls $(LANGUAGE).morpho
	@echo Building $@; \
	echo "###### File $@ ######" > $@;\
	cat $< | ./morpho.$(LANGUAGE).dir -vars -nosep -synt -onerad \
		>> $@

uw.lex: uw
	cp $(srcdir)/uw uw.lex

constr.pl: constructions $(alexinatoolsdir)/constructions_builder.pl
	$(alexinatoolsdir)/constructions_builder.pl $< > $@
	chmod +x $@

%.lex: %.f constr.pl $(alexinatoolsdir)/lex_generator.pl
	@echo Building $@; \
	cat $< | perl constr.pl | $(alexinatoolsdir)/lex_generator.pl \
	$(PRERECODING) \
	| iconv -f UTF-8 -t $(LEX_ENCODING) \
	> $@

%.flt: %.lex
	@echo Building $@; \
	$(PERL) -pe "\
		s/ [a-z]+ \[pred=.//; \
		s/\].*//; \
		s/<.*>//; \
		s/\t(.*)\'[^\']*@/\t$$1\t@/; \
		s/\t(.*)\'[^\'@]*$$/\t$$1\t/; \
		s/\t@/	/; \
		" $*.lex \
		| sort -u | sort -k2 > $@

cmpnd: $(LEXFILES) amlgm $(alexinatoolsdir)/lex2cmpnd.pl
	@echo Building the compounds file; \
	cat $(LEXFILES) $(srcdir)/amlgm | $(alexinatoolsdir)/lex2cmpnd.pl -l $(LANGUAGE) > $@

pctabr: $(LEXFILES)
	@echo Building the file of abbreviations containing a \".\"; \
	echo "xxxxxxxxxxxx." > $@
	cut -f1 *.lex | grep -v '^[ 	]*$$' | grep '\.' >> $@

encoding:
	echo $(LEX_ENCODING) > $@

$(PACKAGE): $(LEXFILES) $(DOC) LICENSE
	@echo "Building $(PACKAGE)-ext-$(VERSION).tgz";\
	mkdir $(PACKAGE)-ext-$(VERSION);\
	cp LICENSE $(PACKAGE)-ext-$(VERSION)/;\
	cp $(DOC) $(PACKAGE)-ext-$(VERSION)/;\
	cat $(LEXFILES) | grep -v "^#" | sort -u > $(PACKAGE)-ext-$(VERSION)/$(PACKAGE)-ext-$(VERSION).txt;\
	tar -cvzf $(PACKAGE)-ext-$(VERSION).tgz $(PACKAGE)-ext-$(VERSION);\
	rm -rf $(PACKAGE)-ext-$(VERSION);\
	touch $(PACKAGE)


######

%.pdf: $(srcdir)/%.tex
	pdflatex $<
