#
# Makefile, in GNU make syntax
# by pts@fazekas.hu at Sun Feb 24 16:20:15 CET 2002
# Sat Apr 20 19:14:11 CEST 2002
# ccdep.pl at Sat Jun  1 15:51:36 CEST 2002
#
# To clean .o files and some more, run: make clean
# To clean all, procsets and configure outputs, run: make allclean
# To create a .tar.gz, run: make dist-noautoconf
# To install dist, run: make dist-install
#

# Magic to convert `make clean' to `make MAKE_DIST=x clean' so that
# `make clean' won't depend on the file Makedep to exist.
ifeq ($(MAKECMDGOALS),clean)
MAKE_DIST=000
endif
ifeq ($(MAKECMDGOALS),allclean)
MAKE_DIST=000
endif
ifeq ($(MAKECMDGOALS),dist-noautoconf)
MAKE_DIST=000
endif
ifeq ($(MAKECMDGOALS),dist-install)
MAKE_DIST=000
endif
ifeq ($(MAKECMDGOALS),Makedep.force)
MAKE_DIST=000
endif

BASH=bash
PERL_BADLANG=x
export PERL_BADLANG

CXXX=$(CXD_assert)
LDALL=$(LDXX) -s $(LDFLAGS) $(LIBS)
ifeq ($(ENABLE_DEBUG), no)
CXXX=$(CXD_no)
endif
ifeq ($(ENABLE_DEBUG), yes)
CXXX=$(CXD_yes)
LDALL=$(LDXX) $(LDFLAGS) $(LIBS)
endif
ifeq ($(ENABLE_DEBUG), checker)
CXXX=$(CXD_checker)
LDALL=checkerg++ $(LDFLAGS) $(LIBS)
endif

CXXALL=$(CXXX) $(CXXFLAGS) $(CXXFLAGSB)
CCALL =$(CXXX) $(CXXFLAGS) $(CXXFLAGSB)
CXDFAL=$(CXXFLAGS) $(CXXFLAGSB) $(LDFLAGS) $(LIBS)

.PHONY: clean dist dist-noautoconf dist-more allclean distclean dist-install
.PHONY: most all all1 install

ALL_FALLBACK=jpegdump ps_tiny sam2p test testfax
ALL=
# Used my Makehedep to add dependencies to .o files.
GLOBFILES=Makefile Makehelp Makedep

MAKEDEP_CMD = perl -x -S ./ccdep.pl --FAL=assert,no,yes,checker $(CXX)
ifeq ($(MAKE_DIST),)
  most all: sam2p  # First target, build this target by default `make'.
  # Actually ./configure creates Makehelp as well, but by adding it to the
  # left side below, make 3.81 would run configure twice (once for each
  # `include' below).
  #
  # echo to config.h is needed to make config.h younger than config.h.in;
  # configure doesn't change the timestamp of config.h if it's unchanged.
  Makedep config.h: configure Makehelp.in config.h.in ccdep.pl sam2p_version.h; ./configure --enable-gif --enable-lzw && echo "" >>config.h
  Makehelp:;
  include Makehelp  # Defines CXX. File generated by ./configure.
  include Makedep   # Contains ALL+=... .
else
  #ifeq ($(wildcard Makedep),Makedep)
  ifeq ($(wildcard Makehelp Makedep),Makehelp Makedep)
    # Do an `include Makedep' anyway, to get the proper value for ALL.
    #Makehelp:;
    Makedep:;
    #include Makehelp  # Defines CXX. File generated by ./configure.
    include Makedep  # Contains ALL+=... .
  else
    # For `make MAKE_DIST=x clean'.
    ALL=$(ALL_FALLBACK)
    # This forces an error for `make MAKE_DIST=x Makedep'. Just run
    # `make Makedep' to build Makedep. We can't build it here, because we don't
    # have Makehelp (which would be generated ./configure, but with MAKE_DIST=x
    # we don't assume that ./configure has been run.)
    Makedep: .Makedep.not.here.;
  endif
  most all:;  # `make MAKE_DIST=x' should be a no-op by default.
endif

.PHONY: Makedep.force
# Called by ./configure
Makedep.force: sam2p_version.h; $(MAKEDEP_CMD)

all1: $(ALL)
# vvv for Epsilon at Thu Oct 31 09:58:58 CET 2002
IDE_MODES := release debug
IDE_TARGETS_debug := $(patsubst %,%.yes, $(TARGETS))
IDE_TARGETS_release := $(TARGETS)

CXD_assert =$(CXX) -s -O2
CXD_no     =$(CXX) -s -DNDEBUG -O3
# CXD_yes    =$(CXX) $(GFLAG) -lefence
CXD_yes    =$(CXX) $(GFLAG)
CXD_checker=checkerg++ $(GFLAG)

# .PHONY: clean dist allclean distclean

SAM2P_VERSION=$(shell $(BASH) ./mkdist.sh --getversion)
sam2p_version.h: debian/changelog mkdist.sh
	(echo '/* Generated automatically by Makefile. */' && \
	 echo '#ifndef SAM2P_VERSION_H' && \
	 echo '#define SAM2P_VERSION_H 1' && \
	 echo '#define SAM2P_VERSION "$(SAM2P_VERSION)"' && \
	 echo '#endif') >$@

xpmc.h: cols2.pl
	perl -x ./cols2.pl >xpmc.h

#HQ=perl -e'$$_=join"",<STDIN>; s@([^\w\.\/\-])@sprintf"\\%03o",ord$$1@ge; print "\"$$_\"\n"'
HQ=perl -x ./hq.pl
#%.tth: %.tte
#	<$< >$@	$(HQ)
#%.tth: %.ttm
#	<$< >$@	$(HQ)
%.tth: %.ttt
	$(HQ) <$< >$@

# Sun Sep 22 01:16:20 CEST 2002
L1_LIST=l1g8z.pst l1ghz.pst l1gbz.pst \
        l1g8l.pst l1ghl.pst l1gbl.pst
PREPROC_STRIP=perl -ne's@/\s+(?=\w)@/@g;print if!/^\#/&&!/^\s*\Z/'
L1_FLAGS=-DCFG_FMT_ZLIB_ONLY=1 -DNDEBUG=1 \
  -DCFG_NO_VAR_S=1 -DUSE_NO_BIND=1 -DUSE_SHORT_NAMES=1 \
  -DUSE_CURRENTFILE=1 -DUSE_NO_EOF=1 \
  -DUSE_UNITLENGTH_8 -DUSE_EARLYCHANGE_1 -DUSE_LOWBITFIRST_FALSE \
  -DUSE_NO_NULLDEF=1 -DUSE_PIN=1
# TTT_QUOTE=perl -e '$$s=$$_=join"",<STDIN>; s@%.*@@g; s@[(][^()]*[)]@@g; die if /[()\\]/ or $$ARGV[0]!~/^(\w+)/; print "\n% TTT_QUOTE\n/$$1 ($$s)\n\n"' --
TTT_QUOTE=perl -e '$$s=$$_=join"",<STDIN>; $$s=~s@([()\\])@\\$$1@g; die if $$ARGV[0]!~/^(\w+)/; print "\n% TTT_QUOTE\n/$$1 ($$s)\n\n"' --
# vvv Dat: input file for `g++ -E' must have .h extension
l1g8z.pst: l1zip.psm psmlib.psm ps_tiny
	<$< >$@.tmp.h perl -pe0
	$(CXX) -E $(L1_FLAGS) -DUSE_A85D=1 $@.tmp.h >$@.tmp.i
	<$@.tmp.i >$@.tmp.pin $(PREPROC_STRIP)
	<$@.tmp.pin >$@.tmp.ps0 ./ps_tiny
	<$@.tmp.ps0 >$@.tmp.pst $(TTT_QUOTE) $@
	mv -f $@.tmp.pst $@
l1ghz.pst: l1zip.psm psmlib.psm ps_tiny
	<$< >$@.tmp.h perl -pe0
	$(CXX) -E $(L1_FLAGS) -DUSE_HEXD=1 $@.tmp.h >$@.tmp.i
	<$@.tmp.i >$@.tmp.pin $(PREPROC_STRIP)
	<$@.tmp.pin >$@.tmp.ps0 ./ps_tiny
	<$@.tmp.ps0 >$@.tmp.pst $(TTT_QUOTE) $@
	mv -f $@.tmp.pst $@
l1gbz.pst: l1zip.psm psmlib.psm ps_tiny
	<$< >$@.tmp.h perl -pe0
	$(CXX) -E $(L1_FLAGS) -DUSE_BINARY=1 $@.tmp.h >$@.tmp.i
	<$@.tmp.i >$@.tmp.pin $(PREPROC_STRIP)
	<$@.tmp.pin >$@.tmp.ps0 ./ps_tiny
	<$@.tmp.ps0 >$@.tmp.pst $(TTT_QUOTE) $@
	mv -f $@.tmp.pst $@
l1g8l.pst: l1lzw.psm psmlib.psm ps_tiny
	<$< >$@.tmp.h perl -pe0
	$(CXX) -E $(L1_FLAGS) -DUSE_A85D=1 $@.tmp.h >$@.tmp.i
	<$@.tmp.i >$@.tmp.pin $(PREPROC_STRIP)
	<$@.tmp.pin >$@.tmp.ps0 ./ps_tiny
	<$@.tmp.ps0 >$@.tmp.pst $(TTT_QUOTE) $@
	mv -f $@.tmp.pst $@
l1ghl.pst: l1lzw.psm psmlib.psm ps_tiny
	<$< >$@.tmp.h perl -pe0
	$(CXX) -E $(L1_FLAGS) -DUSE_HEXD=1 $@.tmp.h >$@.tmp.i
	<$@.tmp.i >$@.tmp.pin $(PREPROC_STRIP)
	<$@.tmp.pin >$@.tmp.ps0 ./ps_tiny
	<$@.tmp.ps0 >$@.tmp.pst $(TTT_QUOTE) $@
	mv -f $@.tmp.pst $@
l1gbl.pst: l1lzw.psm psmlib.psm ps_tiny
	<$< >$@.tmp.h perl -pe0
	$(CXX) -E $(L1_FLAGS) -DUSE_BINARY=1 $@.tmp.h >$@.tmp.i
	<$@.tmp.i >$@.tmp.pin $(PREPROC_STRIP)
	<$@.tmp.pin >$@.tmp.ps0 ./ps_tiny
	<$@.tmp.ps0 >$@.tmp.pst $(TTT_QUOTE) $@
	mv -f $@.tmp.pst $@
# vvv copy the .ttt, not the .tth
#     The perl program down there is a portable cat(1) implementation.
bts1.ttt: bts.ttt $(L1_LIST)
	perl -pe0 $^ >$@
bts2.ttt: bts1.ttt ps_tiny
	./ps_tiny --copy <$< >$@

clean:
	# Not removing sam2p_version.h, because it would trigger the rerun of ./configure at the next `make'.
	rm -f *~ a.out DEADJOE core *.o *.tth *.tmp.pin *.tmp.i *.tmp.ps0 *.tmp.h *.tmp.pst autom4te.cache/*
	rm -f sam2p.yes sam2p.no
	rm -f debian/changelog.dch debian/*~
	rm -f $(ALL) $(ALL:=.yes) $(ALL:=.no) $(ALL:=.assert) $(ALL:=.checker)
	test ! -d autom4te.cache || rmdir autom4te.cache
allclean: clean
	rm -f config.h Makehelp Makedep config.cache config.log \
	    config.status test.eps test.pdf build build-stamp \
	    bts2.tth bts1.ttt bts2.ttt *.pst.tmp.* *.pst

distclean: allclean
	rm -f configure
	-autoconf
dist: distclean dist-noautoconf
dist-noautoconf:
	chmod 755 configure
	$(BASH) ./mkdist.sh
# vvv Create a distribution with more files to aid compilation
dist-more: bts2.tth
	$(BASH) ./mkdist.sh sam2p-more bts2.tth
dist-install: dist-noautoconf
	chmod 600 ../sam2p-*.tar.gz
	scp ../sam2p-*.tar.gz kozma:public_html

# Mac OS/X cp doesn't have -a, so we don't use it
install: sam2p
	-mkdir -p '$(bindir)'
	cp sam2p '$(bindir)'
	chmod 755 '$(bindir)'/sam2p

# __END__ of Makefile