#
# Makefile (gpm-Linux)
#
# Copyright 1994   rubini@ipvvis.unipv.it
#
####
#
# The main configuration parameters are in gpmCfg.h

CFLAGS = -O3 -fomit-frame-pointer
#CFLAGS = -g -DDEBUG

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

ifndef PREFIX
  PREFIX = /usr
endif
LIBDIR = $(PREFIX)/lib
BINDIR = $(PREFIX)/bin
INCDIR = $(PREFIX)/include
MANDIR = $(PREFIX)/man/man1
INFODR = $(PREFIX)/info
LSPDIR = $(PREFIX)/lib/emacs/site-lisp

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

%.elc: %.el
	emacs -batch -l exec.el -exec '(byte-compile-file "$<")'

CLIENTOBJS = mev.o gpm-root.o
CLIENTS = $(CLIENTOBJS:.o=)

LDFLAGS = -lcurses -L. -l$(TARGET)

ELFILE = t-mouse.el
ELCFILE = $(ELFILE:.el=.elc)
LFILES = $(ELFILE) $(ELCFILE)

TARGET = gpm
   TOBJS = gpm.o gpn.o mice.o
LIB = lib$(TARGET).a
   LOBJS = liblow.o libhigh.o libcurses.o
INCLUDES = gpm.h
INFO = doc/gpm.info
MAN = *.1

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

# 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) $(LIB) $(CLIENTOBJS) $(CLIENTS) $(ELCFILE) $(INFO) $(TARGET).1
	strip $(TARGET) $(CLIENTS)

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

$(TARGET): $(TOBJS) $(MORETOBJS)
	$(CC) $(CFLAGS) -o $@ $^

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

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

$(CLIENTS): $(LIB)

$(INFO): doc/doc.gpm
	$(MAKE) -C doc info

$(TARGET).1: doc/doc.gpm doc/manpager
	doc/manpager $^
	
############## misc targets

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

install:
	@for i in $(BINDIR) $(LIBDIR) $(INCDIR) $(INFODR) $(MANDIR) $(LSPDIR); do\
		if [ -d $$i ]; then set +x; else (set -x; mkdir -p $$i); fi; \
	done
	$(INSTALLBIN) $(TARGET) $(CLIENTS) $(BINDIR) 
	$(INSTALLLIB) $(LFILES)            $(LSPDIR)
	$(INSTALLLIB) $(LIB)               $(LIBDIR)
	$(INSTALLLIB) $(INCLUDES)          $(INCDIR)
	$(INSTALLLIB) $(INFO)              $(INFODR)
	$(INSTALLLIB) $(MAN)               $(MANDIR)
	@echo ""
	@echo -n "Now update your /etc/gpm-root.conf "
	@echo    "by editing a copy of ./gpm-root.conf"

pack-gpm:
	$(MAKE) install PREFIX=/tmp/gpmpack/gpm/usr

pack-gpmtools:
	$(MAKE) install PREFIX=/tmp/gpmpack/gpmtools/usr

quickinst:
	(cd bin; tar cf - *) | (cd $(PREFIX); tar xvf -)
	@echo ""
	@echo -n "     ==> WARNING:"
	@echo " The emacs-lisp file has been installed in $(PREFIX)/bin"
	@echo ""
	
terse:
	rm -f *~ */*~ */*.dvi */*.log

clean: terse
	rm -f .depend TAGS Test.log
	$(MAKE) -C doc clean
	rm -f $(TARGET) $(LIB) $(CLIENTS) *.o

cleanest: clean
	rm -r bin
printv:
	@grep RELEASE *.h doc/* sample/*.c

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

mpage:
	$(MAKE) -C doc mpage

sample-dist:
	$(MAKE) -C sample -f Makefile.in distrib
test: gpm
	csh -c "(time ./gpm -m ./data -T ) |& tee /dev/tty >> Test.log"

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

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

bin: all
	-mkdir bin bin/bin bin/include bin/lib bin/info
	$(MAKE) install PREFIX=./bin

sample:
	$(MAKE) -C sample

# don't release binaries, since my compiler has become old.
distrib: mpage sample-dist clean tar printv


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






