

# Defines the RPI variable which is needed by rc-switch/RCSwitch.h
CXXFLAGS=-DRPI

# configuration file for plugins placed into directory ../../Config/
CONFIG_PLUGINS="Config_02.c"

#all: send codesend RFSniffer
all: | prepare

# prepare (generate) files for plagins and filess need for compile
prepare: | createDirPlugins ./Plugins/Make_Generated_list_plugin.txt ./Plugins/Make_Generated_all_h.h ./Plugins/Make_Generated_all_RX.h ./Plugins/Make_Generated_all_TX.h
	$(MAKE) RFlink

# variable object conatin project files for compilation with .o (make use default macros for create .o from .c or .cpp)
objects = ./Base.o ./RawSignal.o ./server.o ./Plug.o ./Misc.o ./arduino/EmulateArduino.o
#objects =

# NOTICE main diference between = and :=
#---------------------------------------
# name_variable  = $(shell cmd) # this will be actualize every time vhen $(name_variable) is call
# name_variable := $(shell cmd) # this will be set only one when start make

# example howto generate variable form files in directory and coincided rename items of variable to .o
#allowed_plagins = $(patsubst %.h,%.o,$(wildcard ./Plugins/Plugin_*.h))

# examle howto generate variable from shell (only one command may be used not sequence )
allowed_plagins = $(shell cat ./Plugins/Make_Generated_list_plugin.txt )

RFlink: $(objects) $(allowed_plagins)
	$(CXX) $(CXXFLAGS) $(LDFLAGS) $+ -o $@ -lwiringPi -lpthread

clean:
	$(RM) ./RFlink ./*.o ./*.gch ./arduino/*.o ./arduino/*.gch ./Plugins/* ; rmdir ./Plugins


#---------------------------------------------- install/uninstall-------------------------------------------------------------------------
# must be run as sudo (sudo make install)
install: ./RFlink
	if ! [ -d "/opt/rflink" ] ; then mkdir "/opt/rflink"; fi ;
	if ! [ -f "/opt/rflink/RFlink" ] ; then cp "./RFlink" "/opt/rflink/" ; fi ;
	if ! [ -f "/etc/rflink.conf" ] ; then cp "./etc/rflink.conf" "/etc/" ; fi ;
	if ! [ -f "/etc/init.d/rflink.sh" ] ; then cp "./etc/init.d/rflink.sh" "/etc/init.d/" ; fi
	update-rc.d rflink.sh defaults
	update-rc.d rflink.sh enable

# must be run as sudo (sudo make uninstall)
uninstall:
	if [ -f "/etc/init.d/rflink.sh" ] ; then update-rc.d rflink.sh remove ; fi
	if [ -f "/etc/init.d/rflink.sh" ] ; then /etc/init.d/rflink.sh stop ; fi
	if [ -f "/etc/init.d/rflink.sh" ] ; then rm "/etc/init.d/rflink.sh" ; fi
	if [ -f "/etc/rflink.conf" ] ; then rm "/etc/rflink.conf" ; fi ;
	if [ -f "/opt/rflink/RFlink" ] ; then rm "/opt/rflink/RFlink" ; fi ;
	if [ -d "/opt/rflink" ] ; then rmdir "/opt/rflink"; fi ;

# enable autostart RFlink service after start system
autostart_on: /opt/rflink/RFlink
	update-rc.d rflink.sh enable
	/etc/init.d/rflink.sh start

# disable auto start RFlink service after start system
autostart_off: /opt/rflink/RFlink
	/etc/init.d/rflink.sh stop
	update-rc.d rflink.sh disable
	
#---------------------------------------------- GENERATE FILES -------------------------------------------------------------------------

#create dir with pugins if not exist
createDirPlugins:
	if ! [ -d ./Plugins ] ; then mkdir "./Plugins"; fi ;

# generate not only list of allowed plugins but also .cpp, .h files for allowed plagins
# file "Make_Generated_list_plugin.txt" contain name of allowed plafin with .o used later for generate variable with list files for compile
# .cpp and .h contain nessesery files for compile plugins
./Plugins/Make_Generated_list_plugin.txt:
	cd ./Plugins; \
	for i in `ls ../../Plugins/Plugin_*.c`;do \
		n=$${i#*_}; \
		n=$${n%.*}; \
		if grep -q "^#define PLUGIN_$$n" "../../Config/$(CONFIG_PLUGINS)"; then \
			echo -n "./Plugins/Plugin_$$n.o " >> ./Make_Generated_list_plugin.txt; \
			\
			echo "//This file is generated by GNU make, do not edit." >> ./Plugin_$$n.cpp; \
			echo "#include \"Plugin_$$n.h\"" >> ./Plugin_$$n.cpp; \
			echo "#ifdef PLUGIN_$$n" >> ./Plugin_$$n.cpp; \
			echo "#include \"../../Plugins/Plugin_$$n.c\"" >> ./Plugin_$$n.cpp; \
			echo "#endif" >> ./Plugin_$$n.cpp; \
			\
			echo "//This file is generated by GNU make, do not edit." >> ./Plugin_$$n.h; \
			echo -n "#ifndef _plugin_$$n" >> ./Plugin_$$n.h; \
			echo "_h" >> ./Plugin_$$n.h; \
			echo -n "	#define _plugin_$$n" >> ./Plugin_$$n.h; \
			echo "_h" >> ./Plugin_$$n.h; \
			echo "	#include \"../../Config/$(CONFIG_PLUGINS)\"" >> ./Plugin_$$n.h; \
			echo "	#ifdef PLUGIN_$$n" >> ./Plugin_$$n.h; \
			echo "		#include \"../Base.h\"" >> ./Plugin_$$n.h; \
			echo "		//prototype functions" >> ./Plugin_$$n.h; \
			echo "		boolean Plugin_$$n(byte function, char *string);" >> ./Plugin_$$n.h; \
			if grep -q "^#define PLUGIN_TX_$$n" "../../Config/$(CONFIG_PLUGINS)"; then \
				echo "		boolean PluginTX_$$n(byte function, char *string);" >> ./Plugin_$$n.h; \
			fi; \
			echo "	#endif" >> ./Plugin_$$n.h; \
			echo "#endif" >> ./Plugin_$$n.h; \
		fi; \
	done;

# Generate Make_Generated_all_h.h file contain include directives for all plugins:
# #include "./Plugins/Plugin_001.h"
# #include "./Plugins/Plugin_002.h"
# ...
# ..
./Plugins/Make_Generated_all_h.h:
	echo "//This file is generated by GNU make, do not edit.\n//This generated file is used in ../Plug.h ." >> ./Plugins/Make_Generated_all_h.h; \
	cd ./Plugins; \
	for i in `ls ./Plugin_*.h`;do \
		echo "#include \"$$i\"" >> ./Make_Generated_all_h.h; \
	done;


# Generate Make_Generated_all_RX.h file contain block C++ code init RX function plugins.
# Only for enable plugin in ..\Config\Config_02.c. Example:
# Plugin_id[x]=1;Plugin_ptr[x++]=&Plugin_001;
# Plugin_id[x]=2;Plugin_ptr[x++]=&Plugin_002;
# ...
# ..
./Plugins/Make_Generated_all_RX.h:
	cd ./Plugins; \
	echo "//This file is generated by GNU make, do not edit." >> ./Make_Generated_all_RX.h; \
	echo "//This generated file is used in ../Plug.h ." >> ./Make_Generated_all_RX.h; \
	x=1; \
	for i in `ls Plugin_*.h`;do \
		n=$${i#*_}; \
		n=$${n%.*}; \
		if grep -q "^#define PLUGIN_$$n" "../../Config/$(CONFIG_PLUGINS)"; then \
			echo "Plugin_id[x]=$$x;Plugin_ptr[x++]=&Plugin_$$n;" >> ./Make_Generated_all_RX.h; \
			x=$$(($$x+1)); \
		fi; \
	done;

# Generate Make_Generated_all_TX.h file contain block C++ code init TX function plugins.
# Only for enable plugin in ..\Config\Config_02.c. Example:
# PluginTX_id[x]=1;PluginTX_ptr[x++]=&Plugin_001;
# PluginTX_id[x]=2;PluginTX_ptr[x++]=&Plugin_002;
# ...
# ..
./Plugins/Make_Generated_all_TX.h:
	cd ./Plugins; \
	echo "//This file is generated by GNU make, do not edit." >> ./Make_Generated_all_TX.h; \
	echo "//This generated file is used in ../Plug.h ." >> ./Make_Generated_all_TX.h; \
	x=1; \
	for i in `ls Plugin_*.h`;do \
		n=$${i#*_}; \
		n=$${n%.*}; \
		if grep -q "^#define PLUGIN_TX_$$n" "../../Config/$(CONFIG_PLUGINS)"; then \
			echo "PluginTX_id[x]=$$x;PluginTX_ptr[x++]=&PluginTX_$$n;" >> ./Make_Generated_all_TX.h; \
			x=$$(($$x+1)); \
		fi; \
	done;

