OS = LINUX
RM = rm -rf
MV = mv
CP = cp
CC = gcc
SUFFIX =
BASEDIR = $(shell pwd)
BIN = $(BASEDIR)/../bin/
VERSION = $(shell cat $(BASEDIR)/../VERSION)
LDFLAGS = -lpthread -D_THREAD_SAFE -D 'VERSION="$(VERSION)"'
BUILDLIBS = $(shell net-snmp-config --libs) 
OBJS = list.o scanner.o snmp.o types.o debug.o output.o postprocessing.o

# DEBUG variable must be defined to enable debug logs creation
ifdef debug
	CFLAGS = -Wall -ggdb -DDEBUG=$(debug)
else
	CFLAGS = -Wall -O2
endif


.PHONY: hynetd
all: hynetd

hynetd: $(OBJS) hynetd.c
	@ echo -n "Compiling and linking main executable..."
	@ $(CC) $(CFLAGS) $(LDFLAGS) hynetd.c -o hynetd$(SUFFIX) $(OBJS) $(BUILDLIBS)
	@ $(MV) $@$(SUFFIX) $(BIN)
ifdef debug
	@ echo "done! (Debug mode enabled)"
else
	@ echo "done!"
	@ echo "(To enable debug mode launch: \"make clean all debug=1\")"
endif
	@ echo "The Hynetd executable has been created in \"bin\" folder."

install:
ifneq ($(shell id -u),0) 
	@ echo 'You need root permissions to install Hynetd!'
else
	@ echo -n "Copying binary file to /usr/local/bin..."
	@ $(CP) $(BIN)/hynetd$(SUFFIX) /usr/local/bin
	@ echo "done!"
endif

uninstall:
ifneq ($(shell id -u),0) 
	@ echo 'You need root permissions to uninstall Hynetd!'
else
	@ echo -n "Removing binary file from /usr/local/bin..."
	@ $(RM) /usr/local/bin/hynetd$(SUFFIX)
	@ echo "done!"
endif

# Libraries
%.o: %.c %.h
	@ echo -n "Compiling $@ module..."
	@ $(CC) -c $(CFLAGS) -o $@ $<
	@ echo "done!"

clean:
	@ echo -n "Cleaning..."
	@ $(RM) $(OBJS) 
	@ $(RM) $(BIN)/*
	@ echo "done!"


