GOAL = tmp
PROGS = transform.$(MACH) respell.$(MACH) yid2html.$(MACH) spellcheck.$(MACH) \
	y2i.pl y2h.pl u16.to.utf8.pl
DATA = respell.data spellcheck.data
CFLAGS = -g -Wall
CC = gcc
USPELLPROG = /homes/raphael/bin/local.$(MACHTYPE)/udriver
USPELLDIR = /homes/raphael/cs/source/cvs/uspell
USPELLDICT = $(USPELLDIR)/dic/yiddish.uspell.dat
USPELLTRANS = $(USPELLDIR)/dic/yiddish.uspell.trans
UYIDLIST = /homes/raphael/cs/HTML/yiddish/wordlist.utf8.txt
YIDLIST = /homes/raphael/cs/HTML/yiddish/wordlist.txt
CSV = /homes/raphael/cs/HTML/yiddish/wordlist.csv
DEFLIST = /homes/raphael/cs/HTML/yiddish/deflist.utf8.txt
BIDI = fribidi --reordernsm --width 80 --rtl
DROPBOX = $(HOME)/Dropbox/yiddish/

default: $(GOAL).dvi

CWD := $(shell pwd)

lj: $(GOAL).lj
dvi: $(GOAL).dvi
ps: $(GOAL).ps
pdf: $(GOAL).pdf
zip: $(GOAL).zip
unicode: $(GOAL).unicode
utf8: $(GOAL).utf8.html
tar: yiddishtex.tar.gz
win: $(GOAL).win
mac: $(GOAL).mac
qte: $(GOAL).qte
qtd: $(GOAL).qtd
uu: yiddishtex.tar.gz.encode
spell: $(GOAL).spell
plain: $(GOAL).tra.html

all: utf8 plain pdf

smallsee: $(GOAL).dvi
	xdvi -S 1 -s 4 -geometry 1000x800+20-10 -xoffset -.2 -yoffset -1.1 -expert \
		-paper 7.1x9 $(GOAL).dvi

see: $(GOAL).dvi
	xdvi -S 1 -s 4 -geometry 1100x1000+80+10 -k -sidemargin 2 \
		 -expert $(GOAL).dvi
	/bin/rm -rf $(GOAL).dvi

dvinorespell:
	perl y2i.pl < $(GOAL).yid | /bin/grep -a -v exact | transform.$(MACH) > $(GOAL).tex
	/bin/rm -f driver.dvi driverinput.tex
	ln -s $(GOAL).tex driverinput.tex
	-latex driver.tex
	-while [ "x`/bin/grep Rerun driver.log`" != "x" ]; do \
        latex driver.tex ; done
	-../ivd2dvi/ivd2dvi.$(MACH) -b -b -b -b < driver.dvi > $(GOAL).dvi
	/bin/rm -f $(GOAL).tex

$(GOAL).dvi: driver.tex $(GOAL).tex yiddish.texinsert
	/bin/rm -f driver.dvi driverinput.tex
	ln -s $(GOAL).tex driverinput.tex
	-latex driver.tex
	-while [ "x`/bin/grep Rerun driver.log`" != "x" ]; do \
        latex driver.tex ; done
	-../ivd2dvi/ivd2dvi.$(MACH) -b -b -b -b -b < driver.dvi > $(GOAL).dvi
	/bin/rm -f $(GOAL).tex

elatex: driver.1.tex $(GOAL).tex yiddish.texinsert
	/bin/rm -f driver.1.dvi driverinput.tex
	ln -s $(GOAL).tex driverinput.tex
	-elatex driver.1.tex
	mv driver.1.dvi $(GOAL).dvi
	/bin/rm -f $(GOAL).tex

$(GOAL).tex: $(GOAL).yid $(PROGS) $(DATA) 
	$(PWD)/respell.$(MACH) respell.data $(GOAL).yid - | \
		perl y2i.pl | /bin/grep -a -v exact | transform.$(MACH) > $(GOAL).tex

$(GOAL).unicode: $(GOAL).yid $(PROGS)
	respell.$(MACH) respell.data $(GOAL).yid - | \
		perl y2i.pl | yid2html.$(MACH) -n > $(GOAL).unicode

$(GOAL).win: $(GOAL).yid $(PROGS)
	respell.$(MACH) respell.data $(GOAL).yid - | \
		perl y2i.pl | yid2html.$(MACH) -w -h > $(GOAL).win

$(GOAL).mac: $(GOAL).yid $(PROGS)
	respell.$(MACH) respell.data $(GOAL).yid - | \
		perl y2i.pl | yid2html.$(MACH) -c -h > $(GOAL).mac

$(GOAL).qte: $(GOAL).yid $(PROGS)
	respell.$(MACH) respell.data $(GOAL).yid - | \
		perl y2i.pl | yid2html.$(MACH) -q -h > $(GOAL).qte

# dynamic font qte
$(GOAL).qtd: $(GOAL).yid $(PROGS)
	respell.$(MACH) respell.data $(GOAL).yid - | \
		perl y2i.pl | yid2html.$(MACH) -d > $(GOAL).qtd

$(GOAL).utf8.html: $(GOAL).yid $(PROGS)
	respell.$(MACH) respell.data $(GOAL).yid - | \
		perl y2i.pl | yid2html.$(MACH) -up8 > $(GOAL).utf8.html

$(GOAL).tra.html: $(GOAL).yid y2h.pl
	perl y2h.pl < $(GOAL).yid > $(GOAL).tra.html

$(GOAL).spell: $(GOAL).yid $(PROGS)
	@ spellcheck.$(MACH) -w -i $(GOAL).yid -o /dev/null -d spellcheck.data \
		-a alphabet -q

sp: $(GOAL).yid $(PROGS)
	@ for bad in \
	  `spellcheck.$(MACH) -w -i $(GOAL).yid -o /dev/null -d spellcheck.data \
       -a alphabet -q 2>&1` ; do \
		test=`echo $$bad | sed -e 's/|/\\\\|/g' -e 's/#/\\\\#/' `; \
		s=`agrep -1 "^$$test\$$" spellcheck.data | fmt -1000`; \
		if [ "x$$s" != x ] ; then echo "$$bad ($$s)" ; else echo "$$bad"; fi \
	done

$(GOAL).ps: $(GOAL).dvi
	dvips -e 0 -o $(GOAL).ps -N0 $(GOAL).dvi

mpage: $(GOAL).ps
	mpage -2o -P $(GOAL).ps > $(GOAL).mpage.ps

$(GOAL).pdf: $(GOAL).ps
	ps2pdf $(GOAL).ps $(GOAL).pdf

$(GOAL).lj: $(GOAL).ps
	gs -sOutputFile=$(GOAL).lj -sDEVICE=laserjet - < $(GOAL).ps

$(GOAL).zip: $(GOAL).lj
	zip $(GOAL).zip $(GOAL).lj

print: $(GOAL).dvi
	dvitps $(GOAL).dvi | lpr

respell.$(MACH): respell.c
	$(CC) $(CFLAGS) -o respell.$(MACH) respell.c

transform.$(MACH): transform.c
	$(CC) $(CFLAGS) -o transform.$(MACH) transform.c

yid2html.$(MACH): yid2html.c
	$(CC) $(CFLAGS) -o yid2html.$(MACH) yid2html.c

spellcheck.$(MACH): spellcheck.c
	$(CC) $(CFLAGS) -o spellcheck.$(MACH) spellcheck.c

clean:
	-mv driver.tex save
	/bin/rm -f tmp.* *.dvi *.log *.tex *.zip *.ps *.pdf *.unicode *.utf8.html \
		 *.win *.mac *.qte yiddishtex.tar.gz* *.tra.html spellcheck.utf8
	-mv save driver.tex

yiddishtex.tar.gz:
	(cd ../hebcalig; echo make clean)
	(cd /homes/raphael/cs/source/fonts/semitic; tar cf - \
		README hebrew/HOWTO hebrew/Makefile hebrew/*.tex hebrew/*.mf \
		hebcalig/COPYRIGHT hebcalig/Makefile hebcalig/*.mf \
		others/CONTENTS others/*.mf others/Makefile \
		yiddish/Makefile yiddish/README yiddish/driver.tex yiddish/COPYRIGHT \
		yiddish/intermediate.doc yiddish/transform.c yiddish/yiddish.texinsert \
		yiddish/yid2html.c yiddish/spellcheck.c yiddish/respell.c \
		yiddish/respell.data yiddish/y2i.pl yiddish/e2i.pl yiddish/i2y.pl \
		yiddish/spellcheck.data yiddish/alphabet \
		yiddish/u16.to.utf8.pl \
		yiddish/mac2ascii.pl yiddish/y2h.pl \
		yiddish/new.s yiddish/expand.pl \
		ivd2dvi/00files.txt ivd2dvi/00readme.txt ivd2dvi/auxiliary.c \
		ivd2dvi/commands.h ivd2dvi/global.h ivd2dvi/io.c ivd2dvi/ivd2dvi.1 \
		ivd2dvi/ivd2dvi.c ivd2dvi/ivd2dvi.readme ivd2dvi/Makefile) \
	| gzip > yiddishtex.tar.gz

yiddishtex.tar.gz.encode: yiddishtex.tar.gz
	uuencode yiddishtex.tar.gz yiddishtex.tar.gz > yiddishtex.tar.gz.encode

checkexpand:
	# generate a sorted list of words in s that aren't yet in new.s
	expand.pl new.s s | sort | less

# the following builds a Unicode list of correctly spelled words
ulrich: spellcheck.utf8
	mv spellcheck.utf8 /homes/raphael/HTML/private/words.utf8

spellcheck.data: new.s
	expand.pl --defsFile new.s --wordList > spellcheck.data

spellcheck.utf8: spellcheck
	$(PWD)/respell.$(MACH) respell.data spellcheck.data - | y2i.pl | \
		yid2html.$(MACH) -t -h > spellcheck.utf8

defs.data: new.s
	expand.pl --defsFile new.s --saveDefs

respellutf8.data: new.s expand.pl
	expand.pl --defsFile new.s --respellutf8List > respellutf8.data

# yidwords.vim: defs.data Makefile
vim: $(HOME)/cs/.vim/yidwords.vim
$(HOME)/cs/.vim/yidwords.vim: Makefile new.s
	expand.pl --defsFile new.s --wordList | tr '\012' ' ' | fold -bs -w 470 | \
		sed 's/^/syn keyword GoodWord transparent /' > yidwords.vim
	expand.pl --defsFile new.s --utf8List | tr '\012' ' ' | fold -bs -w 470 | \
		sed 's/^/syn keyword GoodWord transparent /' > uyidwords.vim
	# expand.pl new.s | y2i.pl | yid2html.$(MACH) -t -h | \
	# 	tr -d '\012' >> yidwords.vim
	mv yidwords.vim uyidwords.vim ~/.vim/

# for uspell
uspell: $(USPELLDICT)
$(USPELLDICT): new.s expand.pl
	expand.pl --defsFile new.s --utf8List > $(USPELLDICT)

$(UYIDLIST): $(USPELLDICT)
	cp $(USPELLDICT) $(UYIDLIST)

$(CSV): wordlist.csv
	cp wordlist.csv $(CSV)

$(YIDLIST): spellcheck.data
	cp spellcheck.data $(YIDLIST)

uspellit: $(USPELLDICT)
	extractwords.pl $(GOAL).uyid /tmp/uspell
	$(USPELLPROG) $(USPELLDICT) $(USPELLTRANS) /tmp/uspell /dev/null | \
		grep -v 'is ok' | grep -v "Table length" | grep -v "deallocating" | \
		 $(BIDI)
	/bin/rm -rf /tmp/uspell 

uspellForwards: $(USPELLDICT)
	forwards/e2i.pl -f < $(GOAL).fyid | \
		yid2html.$(MACH) -t -h > $(GOAL).uyid
	make -s uspellit GOAL=$(GOAL)
	/bin/rm -rf $(GOAL).uyid
	
# for ispell
dict: utf8List
	munchlist -l affixfile < utf8List > dictfile
	buildhash dictfile affixfile yiddish.hash
	/bin/rm -f dictfile.stat dictfile.cnt dictfile
	@echo you can now do ispell -d ./yiddish s

# to check spelling of a utf8 file against new.s
spellu: new.s $(GOAL)
	expand.pl --defsFile new.s --utf8Filter < $(GOAL) > $(GOAL:.uyid=.bads)

# to show defs of a utf8 file via new.s
defu: new.s $(GOAL)
	expand.pl --defsFile new.s --uDefFilter < $(GOAL) > $(GOAL:.uyid=.defs)
	
respell.data: new.s
	expand.pl --defsFile new.s --respellList > respell.data

baseWords.txt: new.s
	expand.pl --defsFile new.s --baseList > baseWords.txt
	
uBaseWords.txt: new.s
	expand.pl --defsFile new.s --uBaseList > uBaseWords.txt

$(DEFLIST): new.s
	expand.pl --defsFile new.s --uDefList > $(DEFLIST)

wordlist.csv: new.s
	expand.pl --defsFile new.s --csv > wordlist.csv

# to try manually
manual:
	expand.pl --defsFile - --immediate

# to try manually, UTF
manualu:
	expand.pl --defsFile - --utf8List | sort | $(BIDI)

dropbox: 
	mkdir -p $(DROPBOX)
	rsync new.s $(DROPBOX)
	mkdir -p $(DROPBOX)/src
	rsync *.pl $(DROPBOX)/src
	

rebuild: respell.data respellutf8.data uspell spellcheck.data 
rebuild: baseWords.txt uBaseWords.txt $(UYIDLIST) $(DEFLIST) 
rebuild: wordlist.csv $(CSV)

JUSTUSDATA = justus/knip.zip
.PHONY: justus
justus: justus.out
# Dropbox -> Jiddisj-hollendisch verterboech -> woordenboek - allerlaatste versie
justus.out: $(JUSTUSDATA) justus2utf8.pl Makefile
	-mv justus.out justus.out.bak
	cd justus; unzip ~/y/$(JUSTUSDATA); cat */WW?? > justus.data
	justus2utf8.pl justus/justus.data > justus.out
	/bin/rm -rf justus/wo*
	gzip justus/justus.data
try:
	justus2utf8.pl $(JUSTUSDATA) | less

lexicon.csv: new.s dict2lexicon.pl
	dict2lexicon.pl < new.s > lexicon.csv
