# $Header: /var/cvsroot/tagger/lexicon/Makefile,v 1.40 2005/07/24 12:44:34 alex Exp $

XFST_PATH=
XFST=${XFST_PATH}xfst
SOURCE_DIR=./lexc
SOURCE_SCRIPT_DIR=./script
GUESSER_DIR=./guesser
ALTERNATION_DIR=./alternations

DEST=./fst
TEMP=./tmp

VERSION=0.46
ARCHIVE="german-lexicon-${VERSION}"

# lexc-sources that don't need to be altered by regexes 
SRC=ART KON KOUI KOUS KOKOM PUNCT APPO APZR PTKZU PTKNEG PTKANT PTKVZ \
	PTKA ITJ APPR APPRART PIS NE ADV PIAT PIDAT PWAV FM \
	VAFIN VAINF VAPP VMFIN VMINF VMPP PPER PRELAT PRELS PWAT PWS PRF \
	VVIZU VVPP ADJD VAIMP
	
# lexc-sources that come with additional scripts containing additional regexes
SRC_SCRIPT=PPOSAT PPOSS PDAT PDS CARD NN VVFIN VVIMP ADJA PAV VVINF

# lexc + script-guessers
SRC_GUESSER=TRUNC NNg VVFINg ADJDg ADJAg

# fallback-guesser (assigns _ALL_ tags!!!)
LAST_RESORT_SCRIPT=FALLBACK

# list of alternations
SRC_ALTERNATIONS=ORTHOGRAPHY

TARGET=german.fst
LAST_RESORT=last_resort.fst
GUESSER=guesser.fst
ALTERNATION=alternation.fst

all: german guesser last_resort alternation
	@echo -e "\n>>> All done."
	@ls -lh ${TARGET} ${GUESSER} ${LAST_RESORT} ${ALTERNATION}

german: $(SRC) $(SRC_SCRIPT)
	@echo -e ">>> Building target '${TARGET}'\n"
	@echo "-e 'save stack $@.fst' -stop" >> \
		${TEMP}/build_target.tmp
	@sh ${TEMP}/build_target.tmp >/dev/null 2>&1

init:
	@echo -e ">>> Building:\n\t[dirs]\t\tcreating base dirs"
	@if ! [ -d "$(DEST)" ]; then \
		mkdir $(DEST) ; \
	fi
	@if ! [ -d "$(TEMP)" ]; then \
		mkdir $(TEMP) ; \
	fi
	@echo "xfst \\" > ${TEMP}/build_target.tmp
	@echo "xfst \\" > ${TEMP}/build_guesser.tmp
	@echo "xfst \\" > ${TEMP}/build_last_resort.tmp
	@echo "xfst \\" > ${TEMP}/build_alternation.tmp

$(SRC): init
	@echo -e "\t[lexc]\t\t$@.lexc -> $@.fst"
	
	@if ! [ -e "${SOURCE_DIR}/$@.lexc" ]; then \
		echo "Warning: sourcefile '$@.lexc' doesn't exist!" && \
		exit 1; \
	fi
	
	@$(XFST) -e 'read lexc < $(SOURCE_DIR)/$@.lexc' \
		-e 'save stack ${DEST}/$@.fst' \
		-stop > /dev/null 2>&1
	@echo "-e 'load stack ${DEST}/$@.fst' -e 'union net' \\" >> \
		$(TEMP)/build_target.tmp

$(SRC_SCRIPT): init
	@echo -e "\t[script]\t$@.script -> $@.fst"
	
	@if ! [ -e "${SOURCE_SCRIPT_DIR}/$@.lexc" ]; then \
		echo "Warning: sourcefile '$@.lexc' doesn't exist!" && \
		exit 1; \
	fi
	@if ! [ -e "${SOURCE_SCRIPT_DIR}/$@.script" ]; then \
		echo "Warning: sourcefile '$@.script' doesn't exist!" && \
		exit 1; \
	fi
	
	@cd $(SOURCE_SCRIPT_DIR); \
		$(XFST) -f $@.script >/dev/null 2>&1
	@mv $(SOURCE_SCRIPT_DIR)/$@.fst $(DEST)
	@echo "-e 'load stack ${DEST}/$@.fst' -e 'union net' \\" >> \
		$(TEMP)/build_target.tmp

clean:
	@echo ">>> Making clean in '`pwd`'"
	-@rm -rf ${DEST} ${TEMP} ${TARGET} ${GUESSER} ${LAST_RESORT} ${ALTERNATION} 

dist: clean all
	@echo ">>> Making distribution '${ARCHIVE}-src.tar.gz'"
	@tar cf ${ARCHIVE}-src.tar `cat MANIFEST`
	@gzip --best --verbose ${ARCHIVE}-src.tar
	
	@echo ">>> Making distribution '${ARCHIVE}-bin.tar.gz'"
	@tar cf ${ARCHIVE}-bin.tar ${TARGET} ${GUESSER} ${LAST_RESORT} \
		${ALTERNATION} strategy.txt COPYING
	@gzip --best --verbose ${ARCHIVE}-bin.tar

$(LAST_RESORT_SCRIPT): init
	@echo -e "\t[fallback]\t$@.script -> $@.fst"
	
	@if ! [ -e "${GUESSER_DIR}/$@.lexc" ]; then \
		echo "Warning: sourcefile '$@.lexc' doesn't exist!" && \
		exit 1; \
	fi
	@if ! [ -e "${GUESSER_DIR}/$@.script" ]; then \
		echo "Warning: sourcefile '$@.script' doesn't exist!" && \
		exit 1; \
	fi

	@cd $(GUESSER_DIR); \
		$(XFST) -f $@.script >/dev/null 2>&1
	@mv $(GUESSER_DIR)/$@.fst $(DEST)
	@echo "-e 'load stack ${DEST}/$@.fst' -e 'union net' \\" >> \
		$(TEMP)/build_last_resort.tmp
		
last_resort: $(LAST_RESORT_SCRIPT)
	@echo -e ">>> Building last resort fallback-guesser '${LAST_RESORT}'\n"
	@echo "-e 'save stack ${LAST_RESORT}' -stop" >> \
		${TEMP}/build_last_resort.tmp
	@sh ${TEMP}/build_last_resort.tmp >/dev/null 2>&1
	
$(SRC_GUESSER): init
	@echo -e "\t[guesser]\t$@.script -> $@.fst"
	
	@if ! [ -e "${GUESSER_DIR}/$@.lexc" ]; then \
		echo "Warning: sourcefile '$@.lexc' doesn't exist!" && \
		exit 1; \
	fi
	@if ! [ -e "${GUESSER_DIR}/$@.script" ]; then \
		echo "Warning: sourcefile '$@.script' doesn't exist!" && \
		exit 1; \
	fi

	@cd $(GUESSER_DIR); \
		$(XFST) -f $@.script >/dev/null 2>&1
	@mv $(GUESSER_DIR)/$@.fst $(DEST)
	@echo "-e 'load stack ${DEST}/$@.fst' -e 'union net' \\" >> \
		$(TEMP)/build_guesser.tmp
		
guesser: $(SRC_GUESSER)
	@echo -e ">>> Building POS-guesser '${GUESSER}'\n"
	@echo "-e 'save stack ${GUESSER}' -stop" >> \
		${TEMP}/build_guesser.tmp
	@sh ${TEMP}/build_guesser.tmp >/dev/null 2>&1
	
	
$(SRC_ALTERNATIONS): init
	@echo -e "\t[alternation]\t$@.script -> $@.fst"
	
	@if ! [ -e "${ALTERNATION_DIR}/$@.script" ]; then \
		echo "Warning: sourcefile '$@.script' doesn't exist!" && \
		exit 1; \
	fi

	@cd $(ALTERNATION_DIR); \
		$(XFST) -f $@.script >/dev/null 2>&1
	@mv $(ALTERNATION_DIR)/$@.fst $(DEST)
	@echo "-e 'load stack ${DEST}/$@.fst' -e 'union net' \\" >> \
		$(TEMP)/build_alternation.tmp

alternation: $(SRC_ALTERNATIONS)
	@echo -e ">>> Building alternations '${ALTERNATION}'\n"
	@echo "-e 'save stack ${ALTERNATION}' -stop" >> \
		${TEMP}/build_alternation.tmp
	@sh ${TEMP}/build_alternation.tmp >/dev/null 2>&1
