#
# Makefile for Linux and GNU make
#
# libkb -- a free, advanced and portable low-level keyboard library
# Copyright (C) 1995, 1996 Markus Franz Xaver Johannes Oberhumer
# For conditions of distribution and use, see copyright notice in kb.h 
#

# This is a ready-to-run Makefile.
# do a 'make' to build the library and the test programs.

# do a 'make install' as root.
# you might want to change these, but probably not.
INSTALL_INCLUDEDIR := /usr/local/include
INSTALL_LIBDIR := /usr/local/lib


# /***********************************************************************
# // Configuration
# // Tools needed: find, install, mmv, perl, sed, tr, zip
# // System tools: chmod, chown, echo, gzip, mv, pwd, rm, tar, touch
# ************************************************************************/

LIBKB = libkb$A		# name of the library

FIND  := find
TOUCH := touch
REN   := mmv -rp

DIRNAME  := $(shell pwd | sed -e 's|.*[/\\]||')
DISTNAME := $(DIRNAME)
VERSION  := $(shell echo $(DISTNAME) | sed -e 's/[^0-9]//g')


# /***********************************************************************
# // Directories
# ************************************************************************/

SRCDIR = include:libsrc:samples:util

vpath %.c     $(SRCDIR)
vpath %.cc    $(SRCDIR)
vpath %.cpp   $(SRCDIR)
vpath %.h     $(SRCDIR)
vpath %.hh    $(SRCDIR)
vpath %.pl    $(SRCDIR)



# /***********************************************************************
# // Compiler and linker flags
# ************************************************************************/

O := .o#	# object extension
A := .a#	# library extension
E := .out#	# executable extension

CC           := gcc
## CFLAGS       += -v			# be verbose
CFLAGS       += -DKB_DEBUG=2		# log all debug information
CFLAGS       += -Iinclude
CFLAGS       += -Wall -W -pedantic	# all warnings on
CFLAGS       += -O2 			# optimize 
CFLAGS       += -fomit-frame-pointer 	# optimize (no debugging possible)
CFLAGS       += -m486 			# optimize for 486
## CFLAGS       += -malign-jumps=0	# can be even faster ?

ifeq (1,2)
CFLAGS       += -g 			# include debug information
else
LDFLAGS      += -s			# strip executable
endif

ifeq (1,2)
CFLAGS       += -fbounds-checking
LDFLAGS      += -fbounds-checking
endif

## LDFLAGS      += -N			# impure executable


# curses
ifeq (1,1)
CFLAGS       += -I/usr/include/ncurses
LDLIBS       += -lncurses		# use ncurses 1.8.5 or higher
else
LDLIBS       += -lcurses		# use curses
endif


CFLAGS       := $(strip $(CFLAGS))
LDFLAGS      := $(strip $(LDFLAGS))
LDLIBS       := $(strip $(LDLIBS))


# /***********************************************************************
# // Main targets
# ************************************************************************/

.PHONY: default all test_pgm clean

default: all

ifneq ($(strip $(wildcard Makefile.all)),)
include Makefile.all
else
include makefile.all
endif

all: uppercase lsm $(LIBKB) test_pgm 

$(LIBKB): $(OBJS)
	-rm -f $@ >& /dev/null
	$(AR) rcs $@ $^

mktables$E: mktables$O
	$(CC) $(LDFLAGS) -o $@ $<


test_pgm: kbtstlnx$E simple$E keycodes$E tube$E 8

kbtstlnx$E: kbtst$O $(LIBKB)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

simple$E: simple$O $(LIBKB)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

keycodes$E: keycodes$O $(LIBKB)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

tube$E: tube$O $(LIBKB)
	$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) -lvgagl -lvga -lm
##	chmod a+rs,go-w $@

8: 8$O 
	$(CC) $(LDFLAGS) -o $@ $^ 


_kbname.hh: mkkbname.pl kb.h
	perl -w $^ > $@


clean:
	-rm -f $(strip $(OBJS) $(LIBKB)) >& /dev/null
	-rm -f *.o *.out >& /dev/null
	-rm -f libkb*.a libkb*.tar.gz libkb*.zip >& /dev/null


# /***********************************************************************
# // Installation
# ************************************************************************/

.PHONY: hdr_install lib_install install

hdr_install : kb.h
	install -m 444 -o root $^  $(INSTALL_INCLUDEDIR)/

lib_install : $(LIBKB)
	install -m 444 -o root $^  $(INSTALL_LIBDIR)/

install : all hdr_install lib_install
	@echo 'libkb' library installed.


# /***********************************************************************
# // uppercase
# // if you unzip the MSDOS distribution (libkbXXX.zip),
# // make should automatically rename all files to uppercase as needed
# ************************************************************************/

.PHONY: uppercase 

uppercase: $(UPPER_MAKE) $(UPPER_FILE)

$(UPPER_MAKE):
	-$(REN) `echo $@ | sed -e 's/^M/m/'` $@

$(UPPER_FILE):
	-$(REN) `echo $@ | tr A-Z a-z` $@


# /***********************************************************************
# // Distribution
# ************************************************************************/

.PHONY: chmod chown chown_root tar zip distclean dist pack

chmod:
	chmod -R go-w *

chown_root:
	chown -R root:root *

tar: mkdist.pl uppercase lsm
	$(FIND) -type f -print | perl $< $(DIRNAME) | sort | (cd .. && tar -cvf- -T-) | (cd .. && gzip -9 > $(DIRNAME)/$(DISTNAME).tar.gz)

zip: mkdist.pl uppercase lsm
	-rm $(DISTNAME).zip
	$(FIND) -type f -print | perl $< $(DIRNAME) | sort | (cd .. && zip -9 -o -@ $(DIRNAME)/$(DISTNAME).zip)
	$(TOUCH) -c $(DISTNAME).zip


distclean: 

dist: grep distclean chmod all distexe touch tar

pack: chmod zip
