# FlexiPix library
# !version: 1.0.1! !date: 2010-08-19!
#
# Copyright 2010 Stefan Schuermans <stefan schuermans info>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

LIBTARGET=libflexipix
VER=1
VERMIN=0
VERREV=1

SRCS=$(wildcard src/*.c)
EX_SRCS=$(wildcard examples/src/*.c)

CC=gcc
AR=ar
RANLIB=ranlib
INSTALL=install

CFLAGS=-W -Wall -fPIC -O3
DEFINES=-DFLP_VER_MAJ=$(VER) -DFLP_VER_MIN=$(VERMIN) -DFLP_VER_REV=$(VERREV)
INCLUDES=-Iinclude
LDFLAGS=-Llib
LIBS=-lm -lpthread -lflexipix

SRCS_BASE=$(patsubst %.c,%,$(SRCS))
DEPS=$(addsuffix .d,$(SRCS_BASE))
OBJS=$(addsuffix .o,$(SRCS_BASE))
EX_SRCS_BASE=$(patsubst %.c,%,$(EX_SRCS))
EX_DEPS=$(addsuffix .d,$(EX_SRCS_BASE))
EX_OBJS=$(addsuffix .o,$(EX_SRCS_BASE))
EX_TARGETS=$(EX_SRCS_BASE)

LIBTGA = lib/$(LIBTARGET).a
LIBTGSO = lib/$(LIBTARGET).so.$(VER)
LIBTGSOM = $(LIBTGSO).$(VERMIN)
LIBTGSOR = $(LIBTGSOM).$(VERREV)

VERSION = $(VER).$(VERMIN).$(VERREV)
DATE := $(shell date +%Y-%m-%d)
PACKNAME = $(LIBTARGET)-$(VERSION)_$(DATE)
PACKDATA = ChangeLog COPYING COPYING.LESSER Makefile \
           examples include README src

ENV_PREFIX=$(shell echo $$PREFIX)
STOW_DIR=/usr/local/stow
STOW_PREFIX=$(STOW_DIR)/$(PACKNAME)
DEF_PREFIX=/usr/local
ifneq ($(ENV_PREFIX),)
  PREFIX=$(ENV_PREFIX)
else
  ifeq ($(shell if [ -d $(STOW_DIR) ]; then echo stow; fi),stow)
    PREFIX=$(STOW_PREFIX)
  else
    PREFIX=$(DEF_PREFIX)
  endif
endif

.PHONY: all clean pack install uninstall
.SECONDARY:
.SUFFIXES:

all: deps $(LIBTGA) $(LIBTGSO) $(EX_TARGETS)

ifneq ($(MAKECMDGOALS),clean)
  include deps
endif

deps: $(DEPS) $(EX_DEPS)
	cat /dev/null $+ >$@

%.d: %.c
	$(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -M -o $@ $<
	sed -i 's!^$(basename $(notdir $<))!$(dir $<)&!' $@

%.o: %.c
	$(CC) $(CFLAGS) $(DEFINES) $(INCLUDES) -c -o $@ $<

%: %.o $(LIBTGA) $(LIBTGSO)
	$(CC) $(LDFLAGS) -o $@ $< $(LIBS)

lib:
	mkdir -p lib

$(LIBTGA): $(OBJS) lib
	$(AR) cr $@ $(filter-out lib,$+)
	$(RANLIB) $@

$(LIBTGSOR): $(OBJS)
	$(CC) -shared -Wl,-soname,$(LIBTARGET).so.$(VER) $(LDFLAGS) \
	      -o $@ $+

$(LIBTGSOM): $(LIBTGSOR)
	rm -f $@
	ln -s $(notdir $<) $@

$(LIBTGSO): $(LIBTGSOM)
	rm -f $@
	ln -s $(notdir $<) $@

clean:
	rm -rf deps $(DEPS) $(OBJS) lib \
	       $(EX_DEPS) $(EX_OBJS) $(EX_TARGETS) \
	       pack

pack:
	$(MAKE) clean
	$(MAKE)
	$(MAKE) clean
	mkdir -p pack/$(PACKNAME)
	cp -r $(PACKDATA) pack/$(PACKNAME)
	sed -i 's/!version: 1.0.1!]*!/!version: 1.0.1!/g' $$(find pack -type f)
	sed -i 's/!date: 2010-08-19!]*!/!date: 2010-08-19!/g' $$(find pack -type f)
	cd pack; tar jcf ../$(PACKNAME).tar.bz2 $(PACKNAME)
	$(MAKE) clean

install: all
	$(INSTALL) -d $(PREFIX)/include/flexipix
	$(INSTALL) -m 644 -t $(PREFIX)/include/flexipix $(wildcard include/flexipix/*.h)
	$(INSTALL) -d $(PREFIX)/lib
	$(INSTALL) -m 644 -t $(PREFIX)/lib $(LIBTGA) $(LIBTGSO)
	ln -sf $(notdir $(LIBTGSO)) $(PREFIX)/lib/$(notdir $(LIBTGSOM))
	ln -sf $(notdir $(LIBTGSOM)) $(PREFIX)/lib/$(notdir $(LIBTGSOR))

uninstall:
	rm -f $(PREFIX)/lib/$(notdir $(LIBTGA))
	rm -f $(PREFIX)/lib/$(notdir $(LIBTGSOR))
	rm -f $(PREFIX)/lib/$(notdir $(LIBTGSOM))
	rm -f $(PREFIX)/lib/$(notdir $(LIBTGSO))
	rm -rf $(PREFIX)/include/flexipix