#
# Makefile (cxdrv)
#
# Copyright 1995   rubini@ipvvis.unipv.it
#
####
#
# To compile with "-g -Wall -DDEBUG_CX100" and without stripping
# uncomment the following line.
#DEBUG = t
export DEBUG

INSTALL = install -c
INSTALLBIN = $(INSTALL) -m 755
INSTALLLIB = $(INSTALL) -m 644

ifndef PREFIX
  PREFIX = /usr/local
endif

MODDIR = /lib/modules/$(shell uname -r)
LIBDIR = $(PREFIX)/lib
BINDIR = $(PREFIX)/bin
INCDIR = $(PREFIX)/include
MANDIR = $(PREFIX)/man
MAN1DIR = $(PREFIX)/man1
MAN3DIR = $(PREFIX)/man3
MAN4DIR = $(PREFIX)/man4

INFODR = $(PREFIX)/info

############## no more editing...

INSTALLDIRS =  $(MODDIR) $(BINDIR) $(LIBDIR) $(INCDIR) \
	$(MANDIR) $(MAN1DIR) $(MAN3DIR) $(MAN4DIR)



CLIENTOBJS = cxg_check.o cxg_show.o cxg_grab.o 
CLIENTS = $(CLIENTOBJS:.o=) cxg_xgrab
DEMOOBJS = test.o roitest.o seltest.o
DEMOS = $(DEMOOBJS:.o=)

ifdef DEBUG
  # -O is needed, because of "extern inline"
  CFLAGS = -g -O -DDEBUG_CX100 -Wall
  LDFLAGS = -L. -l$(TARGET)
else
  CFLAGS = -O3 -fomit-frame-pointer
  LDFLAGS = -L. -l$(TARGET) -s
endif
export CC CFLAGS AR RANLIB

TARGET = cxdrv
export TARGET

LIB = lib$(TARGET).a
   LOBJS = lib.o
DOSLIB = lib$(TARGET)dos.a
   #DOBJS = libdos/*.o
INCLUDES = $(TARGET).h libdos/cx100.h
INFO = doc/$(TARGET).info
MAN = $(TARGET).4

ifeq ($(RANLIB),)
   RANLIB = ranlib
endif

# The wish script must be generated, with this rule

%: %.tcl
	awk '/^##EXE/ {sub("##EXE",""); system($$0);next} {print}' $< > $@
	chmod +x $@

# if not Linux, print a message and die
ifneq ($(shell uname),Linux)
beforeall: check
endif

# force make depend
ifneq (.depend,$(wildcard .depend))
all: depend
endif

all: $(TARGET).o $(LIB) $(CLIENTOBJS) $(CLIENTS) \
	$(DEMOOBJS) $(DEMOS) $(DOSLIB) $(MAN) $(INFO)

check:
	@if [ `uname` != Linux ]; then \
	  echo "" ;\
	  echo "**** This package can compile only under Linux."; \
	  echo "**** Please read the docs (after \"make -C doc\") "; \
	  echo "" ;\
	  exit 1; fi

$(TARGET).o: 

$(LIB): $(LOBJS)
	$(AR) r $@ $^
	$(RANLIB) $@


$(DOSLIB): $(wildcard src/*.[ch])
	$(MAKE) -C libdos

# Without this silly rule it won't work :-(
# $(LD) won't work, either.
%: %.o
	$(CC) $*.o -o $@ $(LDFLAGS)

$(CLIENTS) $(DEMOS): $(LIB)

$(INFO): doc/doc.$(TARGET)
	-@$(MAKE) -C doc info || \
		(echo "Warning: failed to make the info file"; exit 1)

$(MAN): doc/doc.$(TARGET) doc/manpager
	doc/manpager $<

	
############## misc targets

.PHONY: install clean cleanest check distrib terse tar bin\
             mpage sample sample-dist depend dep DEPEND DEP

install: all
	@for i in $(INSTALLDIRS); do\
		if [ -d $$i ]; then set +x; else (set -x; mkdir -p $$i); fi; \
	done
	$(INSTALLBIN) $(TARGET).o   $(MODDIR)
	$(INSTALLBIN) $(CLIENTS)    $(BINDIR) 
	$(INSTALLLIB) $(LIB)        $(LIBDIR)
	$(INSTALLLIB) $(DOSLIB)     $(LIBDIR)
	$(INSTALLLIB) $(INCLUDES)   $(INCDIR)
	-$(INSTALLLIB) $(INFO)      $(INFODR)
	$(INSTALLLIB) $(MAN)        $(MAN4DIR)
	if [ -f $(TARGET).3 ]; then $(INSTALLLIB) $(TARGET).3 $(MAN3DIR); fi
	if [ -f $(TARGET).1 ]; then $(INSTALLLIB) $(TARGET).1 $(MAN1DIR); fi

terse:
	rm -f *~ */*~ */*.dvi */*.log

clean: terse docclean
	rm -f .depend TAGS Test.log
	rm -f $(TARGET).o $(LIB) $(DOSLIB) $(CLIENTS) $(DEMOS) *.o
	$(MAKE) -C libdos clean
	rm -f $(TARGET).[0-9]
	rm -f exe x2d

cleanest: clean
	rm -r bin

docclean:
	$(MAKE) -C doc clean

tar:
	n=`basename \`pwd\``; cd ..; tar cvf - $$n | gzip > $$n.tar.gz

mpage:
	$(MAKE) -C doc mpage

depend dep:
	gcc -MM *.c > .depend

DEPEND DEP:
	gcc -M *.c > .depend


distrib: clean mpage docclean tar printv

printv:
	grep -n RELEASE $(TARGET).[ch] doc/doc.$(TARGET)

dis: exe x2d
	./disass	

exe: $(TARGET).o main.o
	$(LD) $^ -o $@

ifeq (.depend,$(wildcard .depend))
include .depend
endif






