# This is the developer's -*- Makefile -*-, not the user's makefile.
# Do not use it unless you know exactly what you are doing!

# This Makefile requires GNU make and
# GNU gettext 0.11 or above (msgfmt and msguniq).

SHELL = /bin/sh
RM = rm -rf

GMSGFMT = msgfmt --statistics

all : tmp pot gmo

# This is hardwired. (See clisp-xgettext.)
PREDEF_LINGUAS = en
# Other languages must be translated by hand. They must also be added
# to the variable ALL_LINGUAS in ../configure.in.
OTHER_LINGUAS = de fr es nl ru

LINGUAS = $(PREDEF_LINGUAS) $(OTHER_LINGUAS)

DSOURCES = \
 lispbibl fsubr subr pseudofun constsym constobj constpack avl sort subrkw \
    bytecode unix amiga amiga2 acorn msdos win32 xthread \
 spvw spvw_module spvw_debug spvw_alloca spvw_mmap spvw_multimap \
    spvw_singlemap spvw_page spvw_heap spvw_global spvw_gcstat spvw_space \
    spvw_mark spvw_objsize spvw_update spvw_fault spvw_sigsegv spvw_sigcld \
    spvw_sigpipe spvw_sigint spvw_sigwinch spvw_garcol spvw_genera1 \
    spvw_genera2 spvw_genera3 spvw_allocate spvw_typealloc spvw_circ \
    spvw_walk spvw_ctype spvw_language spvw_memfile \
 spvwtabf spvwtabs spvwtabo \
 eval control \
 encoding pathname stream socket io \
 array hashtabl list package record sequence \
 charstrg debug \
 error erramiga errunix errwin32 \
 misc time predtype symbol \
 lisparit aridecl arilev0 arilev1 arilev1c arilev1e arilev1i intelem \
    intlog intplus intcomp intbyte intmal intdiv intgcd int2adic intsqrt \
    intprint intread rational sfloat ffloat dfloat lfloat flo_konv flo_rest \
    realelem realrand realtran compelem comptran posixmath \
 posixmisc \
 rexx affi \
 foreign \
 unixaux win32aux acornaux acornsig amigaaux \
 dirkey

LISPSOURCES = init defseq backquote defmacro macros1 macros2 defs1 \
              timezone places floatprint type defstruct format \
              savemem trace cmacros compiler defs2 loop clos \
              disassem condition threads gstream xcharin keyboard \
              screen beossock runprog query reploop dribble complete \
              describe room edit macros3 clhs inspect gray \
              foreign1 posix dirkey1 german french spanish dutch \
              affi1 rexx1 amigasock

SOURCES := $(patsubst %,%.d,$(DSOURCES)) $(patsubst %,%.lisp,$(LISPSOURCES))

# Lots of small catalog pieces. This will help reduce the building time
# for the catalogs, since most often only a few source files change.
SOURCES_POT := $(patsubst %,tmp/%.pot,$(SOURCES))
SOURCES_POT2 := $(patsubst %,tmp/%.pot2,$(SOURCES))
SOURCES_EN := $(patsubst %,tmp/%.en,$(SOURCES))

tmp :
	test -d tmp || mkdir tmp

$(SOURCES_POT) : tmp/%.pot : ../% clisp-xgettext
	(cd .. ; $${CLISP} po/clisp-xgettext $* po/tmp)

# When a .pot file is built, it automatically builds also the .pot2 and .en
# files.
$(SOURCES_POT2) : tmp/%.pot2 : tmp/%.pot
$(SOURCES_EN) : tmp/%.en : tmp/%.pot

pot : clisp.pot $(patsubst %,%.po,$(LINGUAS)) \
      clisplow.pot $(patsubst %,clisplow_%.po,$(LINGUAS))

MSGUNIQ = msguniq -w 1000

# We build the .pot file by simple concatenation. No sorting!!
clisp.pot : $(SOURCES_POT) ../VERSION po_header
	(./po_header t ; cat $(SOURCES_POT)) > clisp.pox
	$(MSGUNIQ) clisp.pox -o $@
	$(RM) clisp.pox

en.po : $(SOURCES_EN) ../VERSION po_header
	(./po_header en ; cat $(SOURCES_EN)) > en.pox
	$(MSGUNIQ) en.pox -o $@
	$(RM) en.pox


PACKAGE = clisp
MSGMERGE = msgmerge -v -w 1000

update-po : force
	for lang in $(OTHER_LINGUAS); do \
	  echo "$$lang:"; \
	  if $(MSGMERGE) $$lang.po $(PACKAGE).pot -o $$lang.new.po; then \
	    mv -f $$lang.new.po $$lang.po; \
	  else \
	    echo "msgmerge for $$lang.po failed!"; \
	  fi; \
	  rm -f $$lang.new.po; \
	done

$(patsubst %,%.po,$(OTHER_LINGUAS)) : %.po : $(PACKAGE).pot
	$(MSGMERGE) $*.po $(PACKAGE).pot -o $*.pox
	cp -p $*.pox $*.po
	$(RM) $*.pox

# A single "$(MSGMERGE) $*.po.new $(PACKAGE).pot" throws away old
# translations. Two separate $(MSGMERGE) runs don't do this.
$(patsubst %,%.po.merged,$(OTHER_LINGUAS)) : %.po.merged : %.po.new %.po $(PACKAGE).pot
	$(MSGMERGE) $*.po.new $*.po -o $*.po.tmp
	$(MSGMERGE) $*.po.tmp $(PACKAGE).pot -o $*.po.merged
	$(RM) $*.po.tmp


# clisplow.pot is a subset of clisp.pot.

clisplow.pot : $(SOURCES_POT2) ../VERSION po_header
	(./po_header t ; cat $(SOURCES_POT2)) > clisplow.pox
	$(MSGUNIQ) clisplow.pox -o $@
	$(RM) clisplow.pox

$(patsubst %,clisplow_%.po,$(LINGUAS)) : clisplow_%.po : clisplow.pot %.po
	$(MSGMERGE) $*.po clisplow.pot -o - | sed -e '/^#~ /d' -e '/^#: /d' -e '/^# /d' > clisplow_$*.po


# We must generate and distribute the .gmo files, because on the machine
# where clisp is built, GNU's [g]msgfmt might not be available.

GMOFILES = $(patsubst %,%.gmo, $(LINGUAS) $(patsubst %,clisplow_%,$(LINGUAS)))

gmo : $(GMOFILES)

$(GMOFILES) : %.gmo : %.po
	$(GMSGFMT) -o $@ $<


clean :
	$(RM) *.pox *.new.po tmp

force :
