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

# To compile with "-g -Wall -DDEBUG" and without stripping
# uncomment the following line.
#DEBUG = t


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=)

EXTRAS = hltest mouse-test


ifdef DEBUG
  CFLAGS = -g -DDEBUG -Wall
  LDFLAGS = -L.
else
  CFLAGS = -O3 -fomit-frame-pointer
  LDFLAGS = -L. -s
endif
LOADLIBES	= -lcurses -ltermcap

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 libxtra.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) $(CLIENTS) $(EXTRAS) $(ELCFILE) $(TARGET).1 info

info: $(INFO)

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)

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

$(CLIENTS): $(LIB)

$(EXTRAS): $(LIB)

mouse-test: mouse-test.o mice.o 
	$(CC) $(CFLAGS) $^ -o $@
	
$(INFO): doc/doc.gpm
	-@$(MAKE) -C doc info || \
		(echo "Warning: failed to make the info file"; exit 1)

$(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: all
	@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 -)
	
terse:
	rm -f *~ */*~ */*.dvi */*.log

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

cleanest: clean
	rm -r bin
printv:
	@grep -n RELEASE *.h doc/* sample/*.c
	@sed -n -e '/DEBUG =/p' -e '/DEBUG =/q' Makefile
tar:
	n=`basename \`pwd\``; cd ..; tar cvf - $$n | gzip > $$n.tar.gz

suid: gpm gpm-root
	chown root $^; chmod +s $^

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






