# Makefile of DeuTex/DeuSF
# by Per Allansson, Olivier Montanuy and André Majorel
#
# Platform-specific notes:
#
#   Unix    User targets should work on all Unices. Some _developer_
#           targets, however, require Elinks, GCC or GNU tar and gzip.
#
#   DOS     "make [all]" should work with DJGPP and Cygwin. With other
#           compilers, use the batch files in dos\ instead.
#           "make install" will most likely not work ; install the
#           executables by hand.
#
#   Others  Don't know. Send feedback to the maintainer.
#

# Compiled by developers
DCFLAGS  = -g -Wall -Wpointer-arith -Wstrict-prototypes -pg
DCC      = gcc
DLDFLAGS = -g -pg

#DEFINES = -DDT_ALPHA -DDT_PRIVATE
#DEFINES = -DDT_PRIVATE

######### do not edit after this line #########
VERSION       = `cat VERSION`
DISTARC       = deutex-$(VERSION).tar.gz
DISTARCDOS    = deutex-$(VERSION).zip
DISTARCDOS8   = dtex`tr -cd '[0-9]' <VERSION`.zip
BINZIP        = deutex-$(VERSION).bin.dos.zip
DISTDIR       = deutex-$(VERSION)
DISTDIRDOS    = dtex`tr -cd '[0-9]' <VERSION`
DISTFILES     = $(DOC_SRC) $(HEADERS) $(MISCFILES) $(MISCSRC)\
		$(SCRIPTS) $(SRC)
DISTFILESBIN  = $(MISCFILES) deutex.exe deusf.exe

DOC_SRC =\
	docsrc/README\
	docsrc/changes.html\
	docsrc/deutex.6\
	docsrc/faq.html\
	docsrc/hackers_guide.html\
	docsrc/readme.dos\
	docsrc/todo.html\
	docsrc/transpblue.png

DDOCUNIXFILES =\
	unixtmp1/CHANGES\
	unixtmp1/COPYING\
	unixtmp1/COPYING.LIB\
	unixtmp1/FAQ\
	unixtmp1/INSTALL\
	unixtmp1/LICENCE\
	unixtmp1/README\
	unixtmp1/TODO\
	unixtmp1/deutex.6\
	unixtmp1/dtexman6.txt

DDOCUNIX = unixtmp1 $(DDOCUNIXFILES)

DDOCDOSFILES =\
	dostmp1/changes.txt\
	dostmp1/copying\
	dostmp1/copying.lib\
	dostmp1/dtexman6.txt\
	dostmp1/faq.txt\
	dostmp1/install.txt\
	dostmp1/licence.txt\
	dostmp1/manpage.txt\
	dostmp1/readme.txt\
	dostmp1/todo.txt

DDOCDOS = dostmp1 $(DDOCDOSFILES)

HEADERS =\
	src/color.h\
	src/deutex.h\
	src/endianio.h\
	src/endianm.h\
	src/extract.h\
	src/gifcodec.h\
	src/ident.h\
	src/lists.h\
	src/log.h\
	src/merge.h\
	src/mkwad.h\
	src/picture.h\
	src/sound.h\
	src/sscript.h\
	src/text.h\
	src/texture.h\
	src/tools.h\
	src/usedidx.h\
	src/wadio.h

MISCFILES =\
	VERSION

# Only in source distributions
MISCSRC =\
	Makefile.in\
	configure\
	dos/buildbc.bat\
	dos/builddj.bat\
	dos/buildmsc.bat\
	old/deusf.ide\
	old/deutex.ide\
	old/dos2unix.sh\
	old/save.bat\
	src/deusf.def\
	src/deusfos.def\
	src/deutex.def\
	src/deutexos.def

SCRIPTS =\
	scripts/codes\
	scripts/html2txt\
	scripts/process

SRC =\
	src/color.c\
	src/compose.c\
	src/deutex.c\
	src/endianio.c\
	src/endianm.c\
	src/extract.c\
	src/gifcodec.c\
	src/ident.c\
	src/listdir.c\
	src/lists.c\
	src/log.c\
	src/lzw.c\
	src/merge.c\
	src/mkwad.c\
	src/picture.c\
	src/sound.c\
	src/sscript.c\
	src/substit.c\
	src/text.c\
	src/texture.c\
	src/tools.c\
	src/usedidx.c\
	src/version.c\
	src/wadio.c

OBJSF   = $(SRC:.c=.os)
OBJTEX  = $(SRC:.c=.ot)
DOBJSF  = $(SRC:.c=.osd)
DOBJTEX = $(SRC:.c=.otd)
MSGTEX  = $(SRC:.c=.mt)

.SUFFIXES: .mt .os .os~ .ot .ot~ .osd .osd~ .otd .otd~ $(SUFFIXES)

.c.ot: src/deutex.h
	$(CC) $(CFLAGS) $(DEFINES) -DDeuTex -o $@ -c $<

.c.os: src/deutex.h
	$(CC) $(CFLAGS) $(DEFINES) -DDeuSF  -o $@ -c $<

.c.otd: src/deutex.h
	$(DCC) $(DCFLAGS) $(DEFINES) -DDeuTex -o $@ -c $<

.c.osd: src/deutex.h
	$(DCC) $(DCFLAGS) $(DEFINES) -DDeuSF  -o $@ -c $<

.c.mt: src/deutex.h
	scripts/codes $< >$@. && mv $@. $@

all: deutex deusf

deutex: $(OBJTEX) tmp/_deutex
	$(CC) $(LDFLAGS) -o deutex $(OBJTEX) -lm

deusf: $(OBJSF) tmp/_deusf
	$(CC) $(LDFLAGS) -o deusf $(OBJSF) -lm

dall: ddeutex ddeusf

ddt: ddeutex

dds: ddeusf


ddeutex: $(DOBJTEX)
	$(DCC) $(DLDFLAGS) -lm -o deutex $(DOBJTEX) 
	@# Force next "make deutex" to relink
	(sleep 1; mkdir -p tmp; >tmp/_deutex) &

ddeusf: $(DOBJSF)
	$(DCC) $(DLDFLAGS) -lm -o deusf $(DOBJSF) 
	@# Force next "make deusf" to relink
	(sleep 1; mkdir -p tmp; >tmp/_deusf) &

tmp/_deutex:
	[ -d tmp ] || mkdir tmp
	>$@

tmp/_deusf:
	[ -d tmp ] || mkdir tmp
	>$@

install:
	install -p -m 0755 deutex	$(BINDIR)
	install -p -m 0755 deusf	$(BINDIR)
	install -p -m 0644 deutex.6	$(MANDIR)/man6
	if [ -d $(MANDIR)/man6/deusf.6 ]; then rmdir $(MANDIR)/man6/deusf.6; fi
	ln -sf deutex.6			$(MANDIR)/man6/deusf.6

src/version.c: VERSION
	printf "const char deutex_version[] = \"%s\";\n" $(VERSION) >$@

strip: deutex deusf
	strip deutex
	strip deusf

msg: msgtex

msgtex: $(MSGTEX)

doc: $(DDOCUNIX)

unixtmp1:
	mkdir -p $@

unixtmp1/CHANGES: docsrc/changes.html VERSION
	scripts/html2txt docsrc/changes.html >$@_ && mv $@_ $@

unixtmp1/COPYING: docsrc/COPYING
	cp -p docsrc/COPYING $@

unixtmp1/COPYING.LIB: docsrc/COPYING.LIB
	cp -p docsrc/COPYING.LIB $@

unixtmp1/FAQ: docsrc/faq.html VERSION
	echo 'THIS IS A GENERATED FILE -- DO NOT EDIT !' >$@.1
	printf 'Edit docsrc/faq.html instead.\n\n' >>$@.1
	links -dump docsrc/faq.html >>$@.1
	sed 's/^   //; s/^    //' $@.1 >$@.2
	rm -f $@.1
	mv $@.2 $@

unixtmp1/INSTALL: docsrc/INSTALL VERSION scripts/process
	scripts/process docsrc/INSTALL >$@

unixtmp1/LICENCE: docsrc/LICENCE VERSION scripts/process
	scripts/process docsrc/LICENCE >$@

unixtmp1/README: docsrc/README VERSION scripts/process
	scripts/process docsrc/README >$@

unixtmp1/TODO: docsrc/todo.html VERSION
	echo 'THIS IS A GENERATED FILE -- DO NOT EDIT !' >$@.1
	printf 'Edit docsrc/todo.html instead.\n\n' >>$@.1
	links -dump docsrc/todo.html >>$@.1
	sed 's/^   //' $@.1 >$@.2
	rm -f $@.1
	mv $@.2 $@

unixtmp1/deutex.6: docsrc/deutex.6 VERSION unixtmp1/deutex.6.opt\
	  unixtmp1/deutex.6.diag scripts/process
	scripts/process docsrc/deutex.6 >$@

unixtmp1/deutex.6.diag:
	echo 'FIXME \-\- not implemented yet' >$@

unixtmp1/deutex.6.opt: deutex
	./deutex -man >$@

unixtmp1/dtexman6.txt: docsrc/dtexman6.txt
	cp -p docsrc/dtexman6.txt $@

dostmp1:
	mkdir -p $@

dostmp1/changes.txt: unixtmp1/CHANGES
	todos <unixtmp1/CHANGES >$@
	touch -r unixtmp1/CHANGES $@

dostmp1/copying: unixtmp1/COPYING
	todos <unixtmp1/COPYING >$@
	touch -r unixtmp1/COPYING $@

dostmp1/copying.lib: unixtmp1/COPYING.LIB
	todos <unixtmp1/COPYING.LIB >$@
	touch -r unixtmp1/COPYING.LIB $@

dostmp1/dtexman6.txt: unixtmp1/dtexman6.txt
	todos <unixtmp1/dtexman6.txt >$@
	touch -r unixtmp1/dtexman6.txt $@

dostmp1/faq.txt: unixtmp1/FAQ
	todos <unixtmp1/FAQ >$@
	touch -r unixtmp1/FAQ $@

dostmp1/install.txt: docsrc/INSTALL VERSION scripts/process
	scripts/process docsrc/INSTALL | todos >$@

dostmp1/licence.txt: unixtmp1/LICENCE
	todos <unixtmp1/LICENCE >$@
	touch -r unixtmp1/LICENCE $@

dostmp1/manpage.txt: unixtmp1/deutex.6
	nroff -man -Tlatin1 unixtmp1/deutex.6 | ul -t dumb | todos >$@
	touch -r unixtmp1/deutex.6 $@

dostmp1/readme.txt: docsrc/README VERSION scripts/process
	scripts/process docsrc/README | todos >$@

dostmp1/readme-bin.txt: docsrc/readme.dos VERSION scripts/process
	scripts/process docsrc/readme.dos | todos >$@

dostmp1/todo.txt: unixtmp1/TODO
	todos <unixtmp1/TODO >$@
	touch -r unixtmp1/TODO $@

clean:
	rm -f $(OBJTEX) $(OBJSF) $(DOBJTEX) $(DOBJSF) deutex deusf
	rm -f deutex.exe deusf.exe

# dist - make the distribution archive for Unix (.tar.gz)
dist: $(DISTFILES) $(DDOCUNIX)
	mkdir -p $(DISTDIR)
	tar cf - $(DISTFILES) | (cd $(DISTDIR) && tar xvf -)
	cp -p $(DDOCUNIXFILES) $(DISTDIR)
	tar -zcf $(DISTARC) $(DISTDIR)
	rm -rf $(DISTDIR)

# distdos - make the distribution archive DOS (.zip, 8+3)
distdos: $(DISTFILES) $(DDOCDOS)
	mkdir -p $(DISTDIRDOS)
	tar cf - $(DISTFILES) | (cd $(DISTDIRDOS) && tar xvf -)
	cp -p $(DDOCDOSFILES) $(DISTDIRDOS)
	if [ -e $(DISTARCDOS) ]; then rm $(DISTARCDOS); fi
	zip -D -X -9 -r $(DISTARCDOS) $(DISTDIRDOS)
	rm -rf $(DISTDIRDOS)
	printf 'DeuTex %s\nhttp://www.teaser.fr/~amajorel/deutex/'\
	  "$(VERSION)" | zip -z $(DISTARCDOS)

# distbindos - make the DOS binary distribution archive (.zip, 8+3)
TMP=tmpd
distbindos: $(DISTFILESBIN) $(DDOCDOS) dostmp1/readme-bin.txt
	mkdir -p $(TMP)
	cp -dpP $(DISTFILESBIN) $(TMP)
	cp -p $(DDOCDOSFILES) $(TMP)
	# Use a different readme.txt because this is a binary distribution
	cp -p dostmp1/readme-bin.txt $(TMP)/readme.txt
	# INSTALL is irrelevant to binary distributions
	rm -f $(TMP)/install.txt
	if [ -e $(BINZIP) ]; then rm $(BINZIP); fi
	export name=`pwd`/$(BINZIP); cd $(TMP); zip -D -X -9 -R $$name '*'
	rm -rf $(TMP)
	printf 'DeuTex %s\nhttp://www.teaser.fr/~amajorel/deutex/'\
	  "$(VERSION)" | zip -z $(BINZIP)

# save - your daily backup
save:
	tar -czvf ../deutex-`date '+%Y%m%d'`.tar.gz\
	  --exclude dostmp1 --exclude unixtmp1\
	  --exclude "*~" --exclude "*.o"\
	  --exclude "*.os" --exclude "*.ot"\
	  --exclude "*.osd" --exclude "*.otd"\
	  --exclude "*.obj" .

# help - display list of interesting targets
help:
	@echo "Targets for end users:"
	@echo "  [all]       Build DeuTex and DeuSF"
	@echo "  install     Install DeuTex, DeuSF and the doc"
	@echo
	@echo "Targets for developers:"
	@echo "  doc         Just the doc"
	@echo "  dall        Alias for ddeutex + ddeusf"
	@echo "  ddt         Alias for ddeutex"
	@echo "  dds         Alias for ddeusf"
	@echo "  ddeutex     Debug version of DeuTex -> ./deutex"
	@echo "  ddeusf      Debug version of DeuSF  -> ./deusf"
	@echo "  dist        Source dist. (Unix)     -> ./deutex-VERSION.tar.gz"
	@echo "  distdos     Source dist. (DOS)      -> ./dtexVERSION.zip"
	@echo "  distbindos  Binary-only dist. (DOS) -> ./deutex-VERSION.bin.dos.zip"
	@echo "  save        Backup archive          -> ../deutex-YYYYMMDD.tar.gz"
	@echo "  strip       Strip ./deutex and ./deusf"
	@echo "  test        Run all tests (long)"
	@echo "  clean       Remove executables and object files"


