#  Authors     [Robert Meolic (meolic@uni-mb.si)]
#  Revision    [$Revision: 51 $]
#  Date        [$Date: 2013-04-03 17:57:12 +0200 (sre, 03 apr 2013) $]
#
#  Copyright   [This file is part of Biddy.
#               Copyright (C) 2006, 2013 UM-FERI
#               UM-FERI, Smetanova ulica 17, SI-2000 Maribor, Slovenia
#
#               Biddy 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; either version 2
#               of the License, or (at your option) any later version.
#
#               Biddy 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 General Public
#               License along with this program; if not, write to the Free
#               Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#               Boston, MA 02110-1301 USA.]

# -----------------------------------------------------------------------
# When compiling biddyMain.c you have to use the following switches:
#   MAINFLAGS = -DMINGARBAGE | -DMAXGARBAGE
#   MAINFLAGS = -DMINACC | -DMAXACC
# -----------------------------------------------------------------------

# About shared libraries on Linux:
# http://www.debian.org/doc/debian-policy/ch-sharedlibs.html
# http://www.netfort.gr.jp/~dancer/column/libpkg-guide/libpkg-guide.html

LIBNAME = LIBNAME
MAINFLAGS = -DMINGARBAGE -DMAXACC
OS = $(shell uname | sed "s/_.*//;s/[0-9]//g")
RM = rm -f
CP = cp
MKDIR = mkdir -p
CD = cd

all: start dynamic clean static ok
	$(CP) ./biddy.h $(BINDIR)

dynamic: start_dynamic make_dynamic

static: start_static make_static

debug: make_debug

lib: $(BINDIR) $(BINDIR)/$(LIBNAME)

install:
	$(MKDIR) $(DESTDIR)/lib
	$(MKDIR) $(DESTDIR)/share/biddy
	$(CP) -d $(BINDIR)/lib*.so.* $(DESTDIR)/lib
	$(CP) -d $(BINDIR)/README $(DESTDIR)/share/biddy

installdev:
	$(MKDIR) $(DESTDIR)/lib
	$(MKDIR) $(DESTDIR)/include
	$(CP) -d $(BINDIR)/lib*.so $(DESTDIR)/lib
	$(CP) -d $(BINDIR)/lib*.a $(DESTDIR)/lib
	$(CP) -d $(BINDIR)/biddy.h $(DESTDIR)/include

# -----------------------------------------------------------------------
# initialization

make_dynamic:
	@$(MAKE) --no-print-directory dynamic -f Makefile.$(OS)

make_static:
	@$(MAKE) --no-print-directory static -f Makefile.$(OS)

make_debug:
	@$(MAKE) --no-print-directory debug -f Makefile.$(OS)

# -----------------------------------------------------------------------
# help

start:
	@echo "------------------------------"
	@echo "| Building Biddy package ... |"
	@echo "------------------------------"

start_dynamic:
	@echo "*** Building dynamic library ..."

start_static:
	@echo "*** Building static library ..."

ok:
	@echo "---------------------------------------"
	@echo "| Biddy package successfully compiled |"
	@echo "---------------------------------------"

# -----------------------------------------------------------------------
# create a directory for binary files

$(BINDIR):
	$(MKDIR) $(BINDIR)

# -----------------------------------------------------------------------
# make library

$(BINDIR)/biddyMain.o: biddyMain.c biddy.h biddyInt.h
	$(CC) $(MAINFLAGS) -o $(BINDIR)/biddyMain.o -c biddyMain.c -D'BIDDYVERSION="$(BIDDYVERSION)"'

$(BINDIR)/biddyStat.o: biddyStat.c biddy.h biddyInt.h
	$(CC) -o $(BINDIR)/biddyStat.o -c biddyStat.c

$(BINDIR)/$(LIBNAME): $(BINDIR)/biddyMain.o $(BINDIR)/biddyStat.o
	$(CD) $(BINDIR); $(LN) $(LIBNAME) biddyMain.o biddyStat.o

# -----------------------------------------------------------------------
# purge = purify source directory, clean = purify bin directory

purge:
	@$(RM) *~
	@$(RM) */*~

clean:
	@$(RM) $(BINDIR)/biddyMain.o
	@$(RM) $(BINDIR)/biddyStat.o
