# -*-mode: Makefile -*-

# Copyright 2000-2002 by Eric House (fixin@peak.org)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

FRANK_EXT = xwd

PALM_DICT_TYPE = DAWG
PALM_DICT_CID = Xwr3
PAR = ../par.pl
#PAR = par				# available from djw.org

LANGUAGE = $(shell basename $$(pwd))

#all: target_all

# let languages set this first, but we always add blank to it.
BLANK_INFO =  "_" /dev/null /dev/null

# Supply a default so don't have to type so much; feel free to change 
TARGET_TYPE ?= FRANK

##############################################################################
# PalmOS rules
##############################################################################
ifeq ($(TARGET_TYPE),PALM)

all: $(LANG)2to8.pdb

empty: $(LANG)0to0.pdb

#%.pdb: %.dict ../dict2pdb table.bin values.bin
#	../dict2pdb -t table.bin -v values.bin -n $(basename $(@F)) \
#		< $< > $@

# Those languages that have bitmap files for custom glyphs will need to
# define BMPBINFILES and perhaps provide a rule for building the files
binfiles.stamp: $(BMPBINFILES)
	touch binfiles.stamp

palmspecials.bin: ../palm_mkspecials.pl $(BMPFILES)
	$< $(BLANK_INFO) $(LANG_SPECIAL_INFO) > $@

# can't just use values.bin because the specials bitmap info is
# platform-specific
palmvalues.bin: values.bin palmspecials.bin
	cat $^ > $@

# values.bin: palmspecials.bin ../xloc binfiles.stamp
# 	cd ../ && $(MAKE) xloc
# 	binfileparms=""; \
# 		if [ "$(BMPBINFILES)" != "" ]; then \
# 			for f in $(BMPBINFILES)""; \
# 				do binfileparms="$$binfileparms -i $$f"; \
# 			done; \
# 		fi; \
# 		../xloc -l $(LANGCODE) $$binfileparms -T $@
# 		cat palmspecials.bin >> $@

# header (first record) is node count (long) and 4 chars: 
#    unsigned char firstEdgeRecNum;
#    unsigned char charTableRecNum;
#    unsigned char valTableRecNum;
#    unsigned char reserved[3]; // worst case this points to a new resource

palmheader%.bin: $(LANG)%_wordcount.bin
	rm -f $@
	cat $< > $@
	perl -e "print pack(\"C\",3)" >> $@	# first edge
	perl -e "print pack(\"C\",1)" >> $@	# char table rec number
	perl -e "print pack(\"C\",2)" >> $@	# valTable rec number
	perl -e "print pack(\"CCC\",0)" >> $@	# reserved 3 bytes
	perl -e "print pack(\"CC\",0)" >> $@	# c code added two more...


# This works, but leaves out the header info that the current version
# has.  I'm not sure anybody cares, though...
$(LANG)%.pdb: dawg$(LANG)%.stamp table.bin palmvalues.bin palmheader%.bin 
	$(PAR) c -a backup $@ \
		$(basename $(@F)) $(PALM_DICT_TYPE) $(PALM_DICT_CID) \
		palmheader$*.bin table.bin palmvalues.bin dawg$(LANG)$*_*.bin

#	start=$$(echo $@ | sed -e 's/$(LANG)\([0-9]*\)to[0-9]*.pdb/\1/'); \
#	end=$$(echo $@ | sed -e 's/$(LANG)[0-9]*to\([0-9]*\).pdb/\1/'); \
#	zcat $< | grep "^.\{$${start},$${end}\}$$" | \
#		../dict2pdb -t table.bin -v values.bin -n $(basename $(@F)) \
#		> $@

# the files to export for byod
byodbins: table.bin values.bin palmvalues.bin 

#endif				# TARGET_TYPE==PALM

##############################################################################
# Franklin ebook rules
##############################################################################
else
ifeq ($(TARGET_TYPE),FRANK)

# means version 1 and what else?
XWD_DICT_FLAGS = 0x0001

all: $(LANG)2to8.seb

empty: $(LANG)0to0.seb

# get defn of ESDK_CREATESEB_EXE
include ${EBOOKMAN_SDK}/ebsdk.uses

# a binary file (one byte) giving the number of tiles in the dict
charcount.bin: table.bin
	siz=$$(wc -c $< | sed -e 's/$<//'); \
	perl -e "print pack(\"c\",$$siz)" > $@

# 16 bits worth of flags for the start of the eventual file.
flags.bin:
	perl -e "print pack(\"n\",${XWD_DICT_FLAGS})" > $@

# For each entry in the table whose face < 32, there needs to be a pair of
# pbitm files and a string giving the printing form
frankspecials.bin: ../frank_mkspecials.pl  $(BMPFILES)
	$< $(BLANK_INFO) $(LANG_SPECIAL_INFO) > $@

$(LANG)%.$(FRANK_EXT): dawg$(LANG)%.stamp flags.bin charcount.bin table.bin values.bin frankspecials.bin
	cat flags.bin charcount.bin table.bin values.bin frankspecials.bin \
		$(LANG)StartLoc.bin $$(ls dawg$(LANG)$*_*.bin) > $@
	cp $@ saveme.bin

$(LANG)%.seb: $(LANG)%.$(FRANK_EXT) $(LANG)%.atts 
	${ESDK_CREATESEB_EXE} $<
	cp $< $<.saved

$(LANG)%.atts:			#recreate it each time based on params
	echo '_PUB|global+read-only|"Eric_House"' >> $@
	echo "_NAME|global+read-only|\"$(LANG)2to8\""  >> $@
	echo "_EXT|global+read-only|\"$(FRANK_EXT)\""  >> $@
	echo '_LCAT|nosign+global|"CONTENT"'  >> $@
	echo '_PERM|global+read-only|"r"' >> $@

# values.bin: ../xloc 
# 	cd ../ && $(MAKE) xloc
# 	../xloc -l $(LANGCODE) -T $@

# the files to export for byod
byodbins: table.bin values.bin frankspecials.bin


else
	(Need to define TARGET_TYPE if get error poining to this line)
endif				#ifeq ($(TARGET_TYPE),FRANK)
endif

ifeq (s$(TARGET_TYPE),s)
It's an error not to specify a TARGET_TYPE
endif

##############################################################################
# shared rules
##############################################################################

# For some reason I can't fathom dawg$(LANG)% gets nuked every time
# the top-level rule fires (all: for whatever TARGET_TYPE.)  It
# happens after the rule finishes....

dawg$(LANG)%.stamp: $(LANG)Main.dict.gz ../dict2dawg.pl table.bin ../Makefile.langcommon
	start=$$(echo $@ | sed -e 's/dawg$(LANG)\([0-9]*\)to[0-9]*.stamp/\1/'); \
	end=$$(echo $@ | sed -e 's/dawg$(LANG)[0-9]*to\([0-9]*\).stamp/\1/'); \
	echo $${start} and $$end; \
	zcat $< | grep "^.\{$${start},$${end}\}$$" | tr '\n' '\0'| \
		sort -z | ../dict2dawg.pl -m table.bin -b 28000 \
		-ob dawg$(LANG)$* \
		-sn $(LANG)StartLoc.bin -k -term 0 -wc $(LANG)$*_wordcount.bin
	touch $@

$(LANG)%_wordcount.bin: dawg$(LANG)%.stamp
	@echo

# the files to export for byod
allbins: 
	$(MAKE) TARGET_TYPE=PALM byodbins
	$(MAKE) TARGET_TYPE=FRANK byodbins

# table.bin:  ../xloc 
# 	cd ../ && $(MAKE) xloc
# 	../xloc -l $(LANGCODE) -O $@

table.bin:  ../xloc.pl 
	perl -I../ ../xloc.pl -t > $@

values.bin:  ../xloc.pl 
	perl -I../ ../xloc.pl -v > $@

%.dict: %.dict.gz
	zcat $< > $@

clean_common:
	rm -f $(LANG)Main.dict *.bin *.pdb *.seb dawg*.stamp *.$(FRANK_EXT)

help:
	@echo "make TARGET_TYPE=[FRANK|PALM]"

test:
	@echo $(LANGUAGE)
