TOPDIR:=.
TGT:=../bin/restproxy

DIR=$(TOPDIR)
SRCS=$(foreach dir, $(DIR), $(wildcard $(dir)/*.c))
OBJS:=$(patsubst %.c,%.o,$(SRCS))

DIR_LIBEVENT = ./third-lib/libevent/include/
DIR_JSON = ./third-lib/libmjson/src/
DIR_CURL = ./third-lib/libcurl/include/curl/

INCDIR = $(foreach dir, $(DIR), -I $(dir)/ )
INCDIR += -I $(DIR_LIBEVENT)
INCDIR += -I $(DIR_JSON)
INCDIR += -I $(DIR_CURL)

LIBS 	= ./dba/lib/libdba.a ./smcli_client/lib/libclient.a 
LIBS	+= ./pstn_cli/lib/libpstncli.a ./tcp/lib/libtcp.a 
LIBS	+= /usr/lib/x86_64-linux-gnu/libidn.a -I/usr/include/mysql -L/usr/local/lib 
LIBS	+= -L/usr/lib64/mysql -lmysqlclient -lrt -lpthread -lglib-2.0
LIBS	+= ./third-lib/libevent/.libs/libevent.a ./third-lib/libevent/.libs/libevent_pthreads.a 
LIBS	+= ./third-lib/libcurl/lib/.libs/libcurl.a ./third-lib/libmjson/src/.libs/libmjson.a
LIBS	+= -lssl -lcrypto -lzstd -lidn2 -lz

# Determine OS version
# OS_VERSION=$(shell lsb_release -rs)

# ifeq ($(OS_VERSION),22.04)
#     LIBS	+= /usr/lib/x86_64-linux-gnu/libevent.a /usr/lib/x86_64-linux-gnu/libevent_pthreads.a 
# 	LIBS	+= ./third-lib/libcurl/lib/libcurl.a ./third-lib/json-lib/libmjson.a
# 	LIBS	+= -lssl -lcrypto -lzstd -lidn2 -lz  
# else ifeq ($(OS_VERSION),12.04)
#     LIBS	+= ./third-lib/libevent/lib/libevent.a ./third-lib/libevent/lib/libevent_pthreads.a 
# 	LIBS	+= ./third-lib/libcurl/lib/libcurl.a ./third-lib/json-lib/libmjson.a
# 	LIBS	+= -lssl -lcrypto -lzstd -lidn2 -lz  
# endif

CFLAGS = -Wall -g -DX86_64 -DTEST_RESTPROXY $(LIBS) 

CC = gcc

$(TGT):$(OBJS) lib_all 3rdlib_all
	$(CC) -o $(TGT) $(CFLAGS) $(SRCS) $(INCDIR) $(LIBS)
	chmod a+x $(TGT)

$(OBJS):%.o:%.c
	$(CC) $(CFLAGS) -c -o $@ $< $(INCDIR)

all: $(TGT)

clean_all: clean lib_clean 3rdlib_clean

lib_all:
	$(MAKE) -C ./smcli_client
	$(MAKE) -C ./pstn_cli
	$(MAKE) -C ./tcp
	$(MAKE) -C ./dba

3rdlib_all:
	$(MAKE) -C ./third-lib/libevent
	$(MAKE) -C ./third-lib/libcurl
	$(MAKE) -C ./third-lib/libmjson

lib_clean:
	$(MAKE) -C ./smcli_client clean
	$(MAKE) -C ./pstn_cli clean
	$(MAKE) -C ./tcp clean	
	$(MAKE) -C ./dba clean

3rdlib_clean:
	$(MAKE) -C ./third-lib/libevent clean
	$(MAKE) -C ./third-lib/libcurl clean
	$(MAKE) -C ./third-lib/libmjson clean	

clean:
	rm -rf $(OBJS) $(TGT)
