#############################################################################
#
# Makefile for librf24 via LittleWire
#
# License: GPL (General Public License)
# Author:  Andreas Steinel
# Date:    2014/11/19
#
# Description:
# ------------
# use make all and make install to install the library 
# You can change the install directory by editing the LIBDIR line
#

PREFIX=/opt/LittleWirePrefix

LIB=librf24

HEADER_DIR=${PREFIX}/include/RF24
LIB_DIR=$(PREFIX)/lib
DRIVER_DIR=utility/LittleWire

ifeq ($(shell uname), Linux)
	DYN_SUFFIX=so
	LINK_FORMAT=-shared -Wl,-soname,$@.$(DYN_SUFFIX)
else ifeq ($(shell uname), Darwin)
	DYN_SUFFIX=dylib
	LINK_FORMAT=-dynamiclib -install_name ${LIB_DIR}/${LIB}.${DYN_SUFFIX}
else
	$(shell echo "Unknown System, please fix Makefile!")
	$(shell false )
endif

LIBNAME=$(LIB).$(DYN_SUFFIX)
CCFLAGS=-O2 -Wall -g -DLITTLEWIRE -I$(PREFIX)/include
LIBS=-L$(PREFIX)/lib -llittlewire-spi

# make all
# reinstall the library after each recompilation
all: $(LIB)

# Make the library
$(LIB): RF24.o
	g++ $(LINK_FORMAT) ${LIBS} -o ${LIBNAME} $^
	
# Library parts
RF24.o: RF24.cpp
	g++ -Wall -fPIC ${CCFLAGS} -c $^

# clear build files
clean:
	rm -rf *.o ${LIBNAME}

install: all install-libs install-headers

# Install the library to LIBPATH
install-libs: 
	@echo "[Installing Libs]"
	@if ( test ! -d ${LIB_DIR} ) ; then mkdir -p ${LIB_DIR}; fi
	@install -m 0755 ${LIBNAME} ${LIB_DIR}
	@ln -sf ${LIB_DIR}/${LIBNAME} ${LIB_DIR}/${LIBNAME}.1

install-headers:
	@echo "[Installing Headers]"
	@if ( test ! -d ${HEADER_DIR} ) ; then mkdir -p ${HEADER_DIR} ; fi
	@install -m 0644 *.h ${HEADER_DIR}
	@if ( test ! -d ${HEADER_DIR}/${DRIVER_DIR} ) ; then mkdir -p ${HEADER_DIR}/${DRIVER_DIR} ; fi
	@install -m 0644 ${DRIVER_DIR}/*.h ${HEADER_DIR}/${DRIVER_DIR}