# You MUST Have GNU's `find', `ln' and `make' for this Makefile hack to work...
# Change these below to `find' and `ln' for a linux machine (I cross compiled)

FIND=gfind
LN=gln

DIRS=$(shell $(FIND) src ! -name src -a -type d -print -maxdepth 1)
SRCS=$(shell $(FIND) src -type f \( -name \*.c -a ! -name \*T.c -a ! -name \*test.c \) -maxdepth 2)
BITMAPS=$(shell $(FIND) src \( -name \*.xbm -o -name \*.xpm -o -name \*.bm \) )
HDRS=$(shell $(FIND) src -name \*.h -print)
MANS=$(shell $(FIND) src -name \*.man -print)

TEST=$(shell $(FIND) src \( -name \*T.c -o -name \*test.c \) -print)
TOBJS=$(TEST:.c=.o)

SRCDIR:=$(shell pwd)
OBJS=$(SRCS:.c=.o)
CTANGLE=echo Converted

RANLIB=ranlib
AR=ar

CC=gcc
CFLAGS=-Iinclude -Ibitmaps -O6 -I/usr/local2/include -I/usr/X386/include
LDFLAGS=-L/usr/X386/lib -s
LIBS=-lXpm -lXt -lXmu -lXaw -lXext -lX11 -lm

all: lib/libfwf.a

mkdirs:
	@if [ ! -d lib ] ; then mkdir lib; fi
	@if [ ! -d include ] ; then mkdir include; fi
	@if [ ! -d bin ] ; then mkdir bin; fi
	@if [ ! -d man ] ; then mkdir man ;\
		echo building man page links... ;\
		for i in $(MANS) ;\
			do $(LN) -sf ../$$i man/`basename $$i .man`.1 ;\
		done ;\
		fi
	@if [ ! -d include/Xfwf ] ; then mkdir include/Xfwf ;\
		echo building include links... ;\
		for i in $(HDRS) ;\
		do \
			$(LN) -sf ../../$$i include/Xfwf/`basename $$i` ;\
		done ;\
		fi
	@if [ ! -d bitmaps ] ; then mkdir bitmaps;\
		echo building bitmap links... ;\
		for i in $(BITMAPS) ; do $(LN) -sf ../$$i bitmaps/`basename $$i` ;done ;\
		fi

lib/libfwf.a: mkdirs $(OBJS)
	$(AR) rcv $@ $(OBJS)
	$(RANLIB) $@

test: $(TOBJS)
	@for i in $(TOBJS) ;\
	do \
	  echo Building bin/`basename $$i .o`... ;\
	  $(CC) $(CFLAGS) $(LDFLAGS) -o bin/`basename $$i .o` $$i lib/libfwf.a $(LIBS);\
	  echo done. ;\
	done

clean:
	rm -f $(OBJS) $(TOBJS)
	rm -rf man bin lib include bitmaps
