###############################################################################
## Makefile for Dftpd
##
## Usage: make platform=<linux|solaris|freebsd> [opensslroot=PATH]
##
## Example1: make platform=linux
## Example2: make platform=solaris opensslroot=../../openssl-0.9.6l
##
## NOTE: If "opensslroot" is not specified SSL will not be compiled into
##       the server.  The core server is currently only compatible with
##       OpenSSL 0.9.6x (not 0.9.7x).  To use SSL you must first build
##       the OpenSSL libraries.
## 
###############################################################################

# The compiler to be used
CC = gcc

# Target file name (without extension).
TARGET = dftpd

# Set the platform parameters
# Initialize
BUILDPLATFORM = NONE
# Linux parameters
ifeq ($(platform),linux)
  BUILDPLATFORM = LINUX
#  LIBSTDC = -lc
#  LIBSTDCPP = -lstdc++
#  LIBPTHR = -lpthread
  LIBSTDC = /usr/lib/libc.a
  LIBSTDCPP = /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/libstdc++.a 
  LIBPTHR = /usr/lib/libpthread.a
  LDFLAGS = $(LIBPTHR) $(LIBSTDC) $(LIBSTDCPP)
endif
# Solaris parameters
ifeq ($(platform),solaris)
  BUILDPLATFORM = SOLARIS
  LIBSTDCPP = -lstdc++
  LIBPTHR = -lpthread
  LDFLAGS = -lnsl -lsocket $(LIBPTHR) $(LIBSTDCPP)
endif
# FreeBSD parameters
ifeq ($(platform),freebsd)
  BUILDPLATFORM = BSD
  LIBSTDC = -lc
  LIBPTHR = -lpthread
  LDFLAGS = $(LIBPTHR) $(LIBSTDC) -lcompat
endif

# Set the compiler options
CFLAGS = -O2 -pedantic -Wall -Wstrict-prototypes -fno-builtin -D$(BUILDPLATFORM)

# Enable SSL if the user specified "opensslroot=PATH"
ifdef opensslroot
  CFLAGS += -DENABLE_SSL -I$(opensslroot)/include
  LDFLAGS += $(opensslroot)/libssl.a $(opensslroot)/libcrypto.a
endif


# List C++ source files here. (C++ dependencies are automatically generated.)
# Core source files
FILESCORE = BlowfishCrypt.cpp CmdLine.cpp Crypto.cpp Dll.cpp FSUtils.cpp \
            Ftps.cpp FtpsXfer.cpp Log.cpp Service.cpp SiteInfo.cpp Sock.cpp \
            SSLSock.cpp StrUtils.cpp Termcli.cpp Termsrv.cpp Thr.cpp Timer.cpp
# Dftpd source files
FILESDFTPD = DftpdFileUtils.cpp dftpdmain.cpp DftpdRemote.cpp DftpdSite.cpp \
             DftpdSiteAlias.cpp DftpdSiteConf.cpp DftpdSiteFile.cpp \
             DftpdSiteFS.cpp DftpdSiteGroup.cpp DftpdSiteInfo.cpp \
             DftpdSiteLink.cpp DftpdSiteMirror.cpp DftpdSiteOnline.cpp \
             DftpdSitePW.cpp DftpdSiteSSLKey.cpp DftpdSiteUser.cpp

# Set the directory path for the core files
SRCCORE = $(FILESCORE:%=../core/%)
# Set the directory path for the Dftpd files
SRCDFTPD = $(FILESDFTPD:%=./%)

# Define the core object files.
OBJCORE = $(SRCCORE:.cpp=.o)
# Define the Dftpd object files.
OBJDFTPD = $(SRCDFTPD:.cpp=.o)


# Default target (no arguments to make)
all: begin checkplatform build

begin:
ifeq ($(BUILDPLATFORM),NONE)
	@echo "Usage: make platform=<linux|solaris|freebsd> [opensslroot=PATH]"
else
	@echo "Building for" $(platform)
	-mkdir ./bin
endif

checkplatform:
ifeq ($(BUILDPLATFORM),NONE)
	$(error No platform was specified) 
endif

build: $(OBJCORE) $(OBJDFTPD)
	$(CC) -o ./bin/$(TARGET) $(OBJCORE) $(OBJDFTPD) $(LDFLAGS)

# The clean target is used to remove all machine generated files 
# and start over from a clean slate.
clean:
	rm -f $(OBJCORE)
	rm -f $(OBJDFTPD)
	rm -f $(SRCCORE:.cpp=.d)
	rm -f $(SRCDFTPD:.cpp=.d)
	rm -f $(TARGET)

# Compile: create object files from C source files.
%.o: %.cpp
	$(CC) -c $(CFLAGS) $< -o $@

# Automatically generate C++ source code dependencies. 
#
%.d: %.cpp
	set -e; $(CC) -MM $(CFLAGS) $< \
	| sed 's,\(.*\)\.o[ :]*,\1.o \1.d : ,g' > $@; \
	[ -s $@ ] || rm -f $@

-include $(SRCCORE:.cpp=.d)
-include $(SRCDFTPD:.cpp=.d)
