#
# This makefile is way overly complicated for this
# program, but this is the same makefile I use with
# other more complicated packages
#
SELF   = a2gs
INSTALL_PATH=/usr/bin
INSTALLMAN_PATH=/usr/man/man1
CC     = gcc
CFLAGS = -Wall -O2
SHELL  = sh
A2GSOBJS   = a2gs.o getallopt.o
MV     = mv
CP     = cp
CAT    = cat
AWKPROG='{if($$2 == "::"){sub($$1 " ::",c,$$0);c=""};print $$0}'
SED    = sed
GAWK   = awk
TEE    = tee
FIND   = find

.PHONY: all clean dep depend dummy install mrproper


all: .depend $(SELF)

clean: ; $(RM) core.* *.o .depend~{,~}

mrproper: clean ; $(RM) $(SELF) .depend .depend~{,~}

#
# If the .depend file doesn't exist, then we must create
# it.  Then recursively call make to create what was asked for.
#

ifneq (.depend,$(wildcard .depend))

$(SELF) install: .depend
	$(MAKE) $@

DEPEND = dummy

else

include .depend

$(SELF): .depend $(A2GSOBJS)
	$(CC) $(CFLAGS) $(A2GSOBJS) -o $@

install: .depend all
	$(CP) $(SELF) $(INSTALL_PATH)
	$(CP) a2gs.1 $(INSTALLMAN_PATH)/$(SELF).1

endif

# Ok this is the make rules the one problem with this is that if 
# one of the files is deleted, the makefile will fail.  For that
# reason I define a dummy rule for each of the source/include files.

.depend depend dep:: $(DEPEND)
	$(FIND) . -name '*.c' -maxdepth 1 -exec $(CPP) -M '{}' ';'|\
          $(SED) 's/:/::/'|$(TEE) .depend~|\
	  $(GAWK) -v c="DEPEND = " $(AWKPROG)|\
          ( $(SED) 's/ $$/ \\/';echo ' ';\
          echo '.depend :: $$(sort $$(DEPEND))' )>.depend~~
	$(CAT) .depend~{,~}>.depend
	$(RM) .depend~{,~}

$(sort $(DEPEND)) : ;

test:	$(SELF)
	./$(SELF) -m letter < README |grep -v 's/	 *([12][90]){0,1}[0-9][0-9]-[0-3][0-9]-[0-3][0-9]  *[ 0-2][0-4]:[0-9][0-9]//' > test.ps
	if ( cmp README.ps test.ps ) ; then echo Test Passed ; else echo Test Failed ; fi
	$(RM)  test.ps

README.ps:	$(SELF)
	./$(SELF) -m letter < README |grep -v 's/	 *([12][90]){0,1}[0-9][0-9]-[0-3][0-9]-[0-3][0-9]  *[ 0-2][0-4]:[0-9][0-9]//' > README.ps

