# This is a simple makefile for compiling single executable C and C++ projects.
# It assumes an extension of .c for C files and .cc for C++ files.
# Created by Dave Cook, <davecook@home.com>.  Use as you wish.

# Also enables you to create a Visual Studio-like configuration of 
# Debug and Release versions.
# make CFG=release -> builds the release configuration.
# make CFG=debug -> builds the debug configuration.
# You can add as many additional configurations as required.

# The following are variables that you can modify in this makefile or
# override from the make command line, e.g.

# make PROG=foobar SRCS='foo.c bar.c baz.c'

EXENAME = client
CFGRELEASE = release
CFGDEBUG = debug
POSTBUILD = 
CFG = $(CFGDEBUG)

ifeq ($(CFG), $(CFGRELEASE))

# Source code files (do not include headers):
# Default is all the *.c and *.cc files in the current directory.
SRCS = sslclnt.cc
# C compiler options.
CFLAGS = -Wall 
# C++ compiler options.
CXXFLAGS= -Wall 
# Preprocessor options.
CPPFLAGS=
# Include directories.  Each directory is preceded by the -I flag.
INCLUDE=-I. -I/usr/include -I./include
# Linker options
LDFLAGS=
# Libraries to link against.  Each library is preceded by the -l flag.
# Path information is included with the -L flag
LIBS=-lm -L./lib -lssl -lcrypto -lpthread


else

ifeq ($(CFG), $(CFGDEBUG))

# Source code files (do not include headers):
# Default is all the *.c and *.cc files in the current directory.
SRCS = sslclnt.cc
# C compiler options.
CFLAGS = -Wall -g3  
# C++ compiler options.
CXXFLAGS= -Wall -g3 -DDEBUG
# Preprocessor options.
CPPFLAGS= -DDEBGUG
# Include directories.  Each directory is preceded by the -I flag.
INCLUDE=-I. -I/usr/include -I./include
# Linker options
LDFLAGS=
# Libraries to link against.  Each library is preceded by the -l flag.
# Path information is included with the -L flag
LIBS=-lm -L./lib -lssl -lcrypto -lpthread

endif

endif
  
# Name of final executable.
PROG = $(CFG)/$(EXENAME)

# Name of C++ compiler
CXX=g++
# Name of the Linker
CXXLD=$(CXX)

# Shouldn't need to edit below this line except to customize

BNAMES = $(basename $(SRCS))
OBJS = $(addprefix $(CFG)/, $(addsuffix .o, $(BNAMES)))
DEPS = $(addsuffix .$(CFG), $(addsuffix .d, $(BNAMES)))

CFLAGS+=$(INCLUDE)
CXXFLAGS+=$(INCLUDE)

.PHONY: all clean dist distclean $(CFG)

# Add any Post-build steps after all
all: $(CFG) $(PROG)  	

clean:
	-rm -f $(CFG)/*.o *.d.$(CFG) *.d *.o

distclean:
	-rm -f $(CFG)/*.o *.d.$(CFG) *.d *.o core *~ $(PROG) 

dist: all
	-tar -cz $(CFG) > $(EXENAME)_$(CFG)_bin.tgz
	
$(CFG): 
	@$(SHELL) -ec 'if [ ! -d $(CFG) ]; then \
		      mkdir $(CFG); \
		      fi;'

$(PROG): $(CFG) $(OBJS)
	$(CXXLD) -o $(PROG) $(OBJS) $(LDFLAGS) $(LIBS)

# Create dependency files foo.d for each source file foo.c.

%.d.$(CFG): %.cc
		@echo Creating dependencies for $<. 
		@$(SHELL) -ec '$(CXXLD) -MM $(INCLUDE) $(CPPFLAGS) $< \
                      | sed '\''s/\($*\)\.o[ :]*/$(CFG)\/\1.o \1.d.$(CFG) : /g'\'' > $@; \
		      echo -e "\t" $(CXXLD) -c $(CFLAGS) -o $(CFG)/$*.o $< >> $@ ; \
		      [ -s $@ ] || rm -f $@'	
%.d.$(CFG): %.cc
	@echo Creating dependencies for $<.
	@$(SHELL) -ec '$(CXX) -MM $(INCLUDE) $(CPPFLAGS) $< \
                      | sed '\''s/\($*\)\.o[ :]*/$(CFG)\/\1.o \1.d.$(CFG) : /g'\'' > $@; \
		      echo -e "\t" $(CXX) -c $(CXXFLAGS) -o $(CFG)/$*.o $< >> $@ ; \
                      [ -s $@ ] || rm -f $@'

-include $(DEPS)

