DFLAGS	= -g -DDEBUG=1 -Wall
CFLAGS	= $(DFLAGS) -I.
LDFLAGS = -L.
LIBS	= -lsquarelists
DEST	= /usr/local
MANDIR	= 3
MANPAGE	= squarelists.man 
MANPGS	= SqList_deleteNode.man SqList_findNode.man SqList_freeList.man \
	SqList_getMax.man SqList_getMin.man SqList_getNext.man \
	SqList_insertNode.man SqList_makeHead.man SqList_printList.man \
	SquareLists.man

MAJVER	= 1
MINVER	= 1
PTCHLVL	= 3
VERSION	= $(MAJVER).$(MINVER).$(PTCHLVL)

ALL: libsquarelists.so

libsquarelists.a: squarelists.o
	ar rcs libsquarelists.a squarelists.o
	ranlib $@

libsquarelists.so: libsquarelists.so.$(VERSION)
	ln -sf libsquarelists.so.$(VERSION) libsquarelists.so.$(MAJVER); 
	ln -sf libsquarelists.so.$(VERSION) libsquarelists.so;

libsquarelists.so.$(VERSION): squarelists.c squarelists.h
	$(CC) -fPIC $(CFLAGS) -c squarelists.c -o squarelists.o
	$(CC) -shared -Wl,-soname,libsquarelists.so -o libsquarelists.so.$(VERSION) squarelists.o -lc -lm

install: install-lib squarelists.h $(MANPGS) $(MANPAGE)
	test -d $(DEST)/include || mkdir -p $(DEST)/include
	cp -p squarelists.h $(DEST)/include
	test -d $(DEST)/man/man$(MANDIR) || mkdir -p $(DEST)/man/man$(MANDIR)
	for FILE in $(MANPAGE) $(MANPGS); do \
	    MANF=`echo $$FILE | sed 's/.man$$//'` ; \
	    cp -p $$MANF.man $(DEST)/man/man$(MANDIR)/$$MANF.$(MANDIR); \
	    gzip $(DEST)/man/man$(MANDIR)/$$MANF.$(MANDIR); \
	done
	touch install

install-lib: libsquarelists.so.$(VERSION)
	test -d $(DEST)/lib || make -p $(DEST)/lib
	cp -p libsquarelists.so.$(VERSION) $(DEST)/lib
	cd $(DEST)/lib; \
	ln -sf libsquarelists.so.$(VERSION) libsquarelists.so.$(MAJVER); \
	ln -sf libsquarelists.so.$(VERSION) libsquarelists.so;
	touch install-lib

test: squarelists.o prog1 prog2

prog1: prog1.o squarelists.o
	$(CC) -o $@ prog1.o squarelists.o -lc -lm

prog2: prog2.o squarelists.o
	$(CC) -o $@ prog2.o squarelists.o -lc -lm

README.html: squarelists.man
	groff -man -T html squarelists.man >README.html

$(MANPGS): $(MANPAGE)
	for FILE in $@; do \
		echo ".so man$(MANDIR)/squarelists.$(MANDIR)" >$$FILE ; \
	done

clean:
	$(RM) *.o *~ prog? libsquarelists.* install install-lib $(MANPGS) README.html

dist:	squarelists-$(VERSION).tgz

squarelists-$(VERSION).tgz: clean README.html
	tar -czf $@ README.html Makefile squarelists.* prog?.c ddj_square.txt Changelog
