# IMWheel Makefile
# best used with gmake!

VERSION=0.9.6

.PHONY: jax getopt

CC=gcc

ifeq ("$(shell uname -s)","Linux")
INCDIRS=-I/usr/X11R6/include -Ijax
LIBDIRS=-L/usr/X11R6/lib
SUBDIRLIBS=jax/libjax.a
SUBDIRS=jax
LIBS=-lX11 -lXext -lXtst
ifeq ("$(shell ls -d gpm-imwheel)","gpm-imwheel")
GPM=gpm
endif
else
SHELL=`which bash`
INCDIRS=-I/usr/X11R6/include -Ijax -Igetopt
LIBDIRS=-L/usr/X11R6/lib
SUBDIRLIBS=jax/libjax.a getopt/libgetopt.a
SUBDIRS=jax getopt
LIBS=-lX11 -lXext -lXtst
GPM=
endif

#FLAGS=-g -DDEBUG -Wall
#FLAGS=-g -pg
FLAGS=-O6 -s -Wall
INSTALLDIR=/usr/local/bin
# No spaces are allowd before or after the PIDDIR directory
#PIDDIR=/var/run#NOTE : this is great for root users... but regular users
               # have no access here which is when the program needs to be
               # setuid root... only do this if you are prepared to have
               # another setuid root program in your install base.
PIDDIR=/tmp#NOTE : This has been voted by a large majority to be the best
           # place for this file. for the good of all users.
CFLAGS=$(FLAGS) $(INCDIRS) -DPIDDIR="\"$(PIDDIR)\"" -DVERSION="\"$(VERSION)\""
LDFLAGS=$(FLAGS) $(LIBDIRS) $(LIBS)
SHELL=/bin/sh
PROGS= imwheel $(GPM) mdump setimps2 getmdt setmmplus setps2

all: $(SUBDIRS) $(PROGS)

gpm-imwheel/Makefile:
	cd gpm-imwheel ; ./configure

gpm: gpm-imwheel/Makefile
	@make -C gpm-imwheel

imwheel: imwheel.o util.o cfg.o $(SUBDIRLIBS)

jax:
	@make -C jax

getopt:
	@make -C getopt

imwheel.o: imwheel.c util.h

new: clean all

texts:
	set -x ; for f in README BUGS CHANGES COPYING ; do cp $$f $$f.tmp ; expand -t4 $$f.tmp > $$f ; rm $$f.tmp ; done ; set +x

tgz: nuke texts full-tgz nogpm-tgz

full-tgz:
	FNAME=`basename $$PWD` ; tar -zcvf ../$$FNAME.tar.gz -C .. $$FNAME

nogpm-tgz:
	FNAME=`basename $$PWD` ; tar -zcvf ../$$FNAME.nogpm.tar.gz -C .. $$FNAME --exclude='imwheel/gpm-*'

clean:
	rm -f *.o

nuke: clean
	rm -f $(PROGS)
	-make -C jax nuke
	-make -C getopt nuke
ifeq ("$(GPM)", "gpm")
	-make -C gpm-imwheel/docs distclean
	-make -C gpm-imwheel distclean
endif

ifeq ("$(GPM)", "gpm")
install: install-gpm install-imwheel

install-gpm: $(GPM)
	@make -C gpm-imwheel install
else
install: install-imwheel
endif

install-imwheel: imwheel
	install -m 755 -s imwheel $(INSTALLDIR)
	@setuid='' ;\
	while [ -z "$$setuid" ] ; do \
		echo -ne '\nShall I setuid root the imwheel executable [N/y]?' ;\
		read setuid ;\
		case "$$setuid" in \
			y|Y) echo '## Setting UID, this is best for non-root usage!' ;\
				echo '## This does not effect usage for root users. (duh!)' ;\
				echo '## This gives all users kill privileges for other imwheel processes.' ;\
				echo 'chown root $(INSTALLDIR)/imwheel' ;\
				chown root $(INSTALLDIR)/imwheel ; \
				echo 'chgrp bin $(INSTALLDIR)/imwheel' ;\
				chgrp bin $(INSTALLDIR)/imwheel ; \
				echo 'chmod 4755 $(INSTALLDIR)/imwheel' ;\
				chmod 4755 $(INSTALLDIR)/imwheel ; \
				setuid=y \
				;;\
			n|N|'') [ $(PIDDIR) = '/var/run' ] && echo '## This may cause problems for users other than root!' || ( echo '## This should be OK...if you have not altered the Makefile! ;)' ; echo '## /tmp is the best place for the pidfile this way.  That'"'"'s the way it is now.') ;\
				setuid=n \
				;;\
			*) setuid='' ;;\
		esac ;\
	done
	@[ -e /etc/imwheelrc ] \
		&& echo "## /etc/imwheelrc already exists, not installing." \
		|| (set -x;install -m 644 -o root imwheelrc /etc/imwheelrc)
	@[ -e $(HOME)/.imwheelrc ] \
		&& echo "## $(HOME)/.imwheelrc already exists, not installing." \
		|| (set -x;install -m 644 imwheelrc $(HOME)/.imwheelrc)
	install -m 644 imwheel.1 /usr/local/man/man1/imwheel.1

mdump: mdump.c
	$(CC) -O6 $^ -o $@

setimps2: setimps2.c
	$(CC) -O6 $^ -o $@

setmmplus: setmmplus.c
	$(CC) -O6 $^ -o $@

getmdt: getmdt.c
	$(CC) -O6 $^ -o $@
