# USAGE:
#   1. place this Makefile in the directory where the source files are
#   2. edit this Makefile to:
#      a) specify the source extension, e.g., cpp, c, c++, etc.,
#         whatever your source files end in, by assigining it to variable SCXT
#      b) specify the compiler your using, e.g., CC, gcc, etc.,
#         by assigning it to the variable CC
#      c) list all the source files under the SRCS = \ line, in the format:
#         file1.$(SCXT) \
#         file2.$(SCXT) \
#         ...
#         filen.$(SCXT)
#         (note that the last file doesn't have the \ at the end)
#      d) list all the object files under the OBJS = \ line, in a similar
#         format to the SRCS list, e.g.,
#         file1.$(OBXT) \
#         file2.$(OBXT) \
#         ...
#      e) change the "Main target" to the name of your executable, e.g.,
#         curproj, this will be the name of your program (instead of a.out).
#      f) --IMPORTANT--
#         after you have edited all your source files to include
#         the appropriate header files, run this file with the command
#         `make depend'.  Repeat this any time the header info changes in
#         any of the source files.
#      g) run make to compile your file set, using the command `make'.

.SUFFIXES: .o .c .cpp

# Preamble: sets up source and object extensions, compiler, include flags
#
SCXT	= c
OBXT	= o
CC	= CC
INCLUDE	= -I. -I/usr/include/ -I/usr/include/CC/
# C Flags
#
CFLDBUG	= -g			# debugger info
CFLNOTO	= -no_auto_include	# disable impl. inclusion of tmplt imp. files.
CFLLANG	= -LANG:bool=ON		# turn on bool and true/false identifiers
#CFLAGS	= $(CFLDBUG) $(CFLNOTO) $(CFLLANG)	# SGI
CFLAGS	= $(CFLDBUG)				# Sun CC
# Load libraries
#
LDLIBS	=
LDFLAGS	=

# Source files
#
SRCS	= \
qm.$(SCXT) \
stk.$(SCXT) \
asg5.$(SCXT)

# Object files
#
OBJS	= \
qm.$(OBXT) \
stk.$(OBXT)

# Conversion rule: source_extension -> object extension
#
.$(SCXT).$(OBXT):	; $(CC) -c $(CFLAGS) $(INCLUDE) -o $*.$(OBXT) $*.$(SCXT)

# Main target
#
all: asg5

# Targets
#
asg5: asg5.$(SCXT) $(OBJS)
	$(CC) $(CFLAGS) $@.$(SCXT) $(OBJS) $(LDFLAGS) $(LOADLIBS) -o $@

# Extra targets
#
backup:
	cp Makefile *.fd *.h *.c Backup

neat:
	rm -f *.$(OBXT) core

clean:
	rm -f *.$(OBXT) core asg5

# Make dependencies: grep for "#include" lines in ${SRCS}, append to Makefile
#
#depend: cleanmake
#	@grep '^#include' ${SRCS} | grep -v '<' | \
#	sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' \
#	    -e 's/\.c/.$$\(OBXT\)/' | \
#	awk ' { if ($$1 != prev) { print rec; rec = $$0; prev = $$1; } \
#		else { if (length(rec $$2) > 78) { print rec; rec = $$0; } \
#		       else rec = rec " " $$2 } } \
#	      END { print rec } ' >> Makefile
#
depend: cleanmake
	@grep '^#include' ${SRCS} | grep -v '<' | \
	sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' \
	    -e 's/\.${SCXT}/.$$\(OBXT\)/' | \
	awk ' { if ($$1 != prev) { print rec; rec = $$0; prev = $$1; } \
		else { if (length(rec $$2) > 78) { print rec; rec = $$0; } \
		       else rec = rec " " $$2 } } \
	      END { print rec } ' >> Makefile

# Delete everything below the "# DO NOT DELETE -- depend" line
#
cleanmake:
	@cp Makefile Makefile.bak;
	@grep -n '# DO NOT DELETE' Makefile | \
	awk '{ if ($$4 == "DELETE" && $$6 == "depend") { print($$1) } }' | \
	sed -e 's/[^0-9]//' \
	    -e 's/[^0-9]//' | \
	awk '{ {printf("%d,$$d\n",$$1+1)} }' > sed.script;
	@sed -f sed.script Makefile.bak > Makefile;
	@rm -f sed.script

# Dependencies
#
# DO NOT DELETE -- depend depends on it!

qm.$(OBXT): qm.h
stk.$(OBXT): stk.h
asg5.$(OBXT): stk.h
