!INCLUDE ..\win32\common.mak

!IF "$(NTLM)" == "1"
PLUGINS_EXT=saslNTLM.dll
!ELSE 
PLUGINS_EXT=
!ENDIF 

!IF "$(GSSAPI)" == "CyberSafe"
PLUGINS_EXT=$(PLUGINS_EXT) saslGSSAPI.dll
!ENDIF 

!IF "$(SRP)" == "1"
PLUGINS_EXT=$(PLUGINS_EXT) saslSRP.dll
!IF "$(DO_SRP_SETPASS)" == "1"
SRP_FLAGS=/DDO_SRP_SETPASS=1
!ENDIF 
!ENDIF 

!IF "$(OTP)" == "1"
PLUGINS_EXT=$(PLUGINS_EXT) saslOTP.dll
!ENDIF

PLUGINS=saslANONYMOUS.dll \
	saslPLAIN.dll \
	saslCRAMMD5.dll \
	saslDIGESTMD5.dll \
	saslLOGIN.dll \
	$(PLUGINS_EXT) \
	saslSASLDB.dll

compat_sources = getaddrinfo.c getnameinfo.c
compat_objs = getaddrinfo.obj getnameinfo.obj

common_sources = plugin_common.c plugin_common.h
common_objs = plugin_common.obj $(compat_objs)

saslANONYMOUS_sources = anonymous.c anonymous_init.c $(common_sources)
saslANONYMOUS_objs = anonymous.obj anonymous_init.obj $(common_objs)
saslANONYMOUS_out = saslANONYMOUS.dll saslANONYMOUS.exp saslANONYMOUS.lib

saslPLAIN_sources = plain.c plain_init.c $(common_sources)
saslPLAIN_objs = plain.obj plain_init.obj $(common_objs)
saslPLAIN_out = saslPLAIN.dll saslPLAIN.exp saslPLAIN.lib

saslCRAMMD5_sources = cram.c crammd5_init.c $(common_sources)
saslCRAMMD5_objs = cram.obj crammd5_init.obj $(common_objs)
saslCRAMMD5_out = saslCRAMMD5.dll saslCRAMMD5.exp saslCRAMMD5.lib

saslDIGESTMD5_sources = digestmd5.c digestmd5_init.c $(common_sources)
saslDIGESTMD5_objs = digestmd5.obj digestmd5_init.obj $(common_objs)
saslDIGESTMD5_out = saslDIGESTMD5.dll saslDIGESTMD5.exp saslDIGESTMD5.lib

saslLOGIN_sources = login.c login_init.c $(common_sources)
saslLOGIN_objs = login.obj login_init.obj $(common_objs)
saslLOGIN_out = saslLOGIN.dll saslLOGIN.exp saslLOGIN.lib

saslNTLM_sources = ntlm.c ntlm_init.c $(common_sources)
saslNTLM_objs = ntlm.obj ntlm_init.obj $(common_objs)
saslNTLM_out = saslNTLM.dll saslNTLM.exp saslNTLM.lib

saslGSSAPI_sources = gssapi.c gssapiv2_init.c $(common_sources)
saslGSSAPI_objs = gssapi.obj gssapiv2_init.obj $(common_objs)
saslGSSAPI_out = saslGSSAPI.dll saslGSSAPI.exp saslGSSAPI.lib

saslSRP_sources = srp.c srp_init.c $(common_sources)
saslSRP_objs = srp.obj srp_init.obj $(common_objs)
saslSRP_out = saslSRP.dll saslSRP.exp saslSRP.lib

saslOTP_sources = otp.c otp_init.c $(common_sources)
saslOTP_objs = otp.obj otp_init.obj $(common_objs)
saslOTP_out = saslOTP.dll saslOTP.exp saslOTP.lib

!IF "$(NTLM)" == "1" || "$(SRP)" == "1" || "$(OTP)" == "1"
OPENSLL_FLAGS= /I $(OPENSSL_INCLUDE)
!ELSE 
OPENSLL_FLAGS=
!ENDIF 

!IF "$(GSSAPI)" == "CyberSafe"
GSS_FLAGS= /I $(GSSAPI_INCLUDE) /D "HAVE_GSS_C_NT_HOSTBASED_SERVICE" /D "HAVE_GSS_C_NT_USER_NAME"
GSS_LIBS=/libpath:$(GSSAPI_LIBPATH) gssapi32.lib
!ELSE 
GSS_FLAGS=
GSS_LIBS=
!ENDIF 

# Auxprop Plugin
libsasldb_sources = allockey.c db_berkeley.c
libsasldb_objs = allockey.obj db_berkeley.obj

saslSASLDB_sources = sasldb.c sasldb_init.c $(libsasldb_sources) $(common_sources)
saslSASLDB_objs = sasldb.obj sasldb_init.obj $(libsasldb_objs) $(common_objs)
saslSASLDB_out = saslSASLDB.dll saslSASLDB.exp saslSASLDB.lib

all_objs = $(saslANONYMOUS_objs) $(saslPLAIN_objs) $(saslCRAMMD5_objs) $(saslDIGESTMD5_objs) $(saslLOGIN_objs) $(saslNTLM_objs) $(saslGSSAPI_objs) $(saslSRP_objs) $(saslOTP_objs) $(saslSASLDB_objs)
all_out = $(saslANONYMOUS_out) $(saslPLAIN_out) $(saslCRAMMD5_out) $(saslDIGESTMD5_out) $(saslLOGIN_out) $(saslNTLM_out) $(saslGSSAPI_out) $(saslSRP_out) $(saslOTP_out) $(saslSASLDB_out)

# LIBSASL_EXPORTS is required to export additional DB routines from sasldb
DB_FLAGS = /I $(DB_INCLUDE) /I "..\sasldb" /D "LIBSASL_EXPORTS"
CPPFLAGS = /I "..\win32\include" /I "." /I "..\include" $(DB_FLAGS) $(OPENSLL_FLAGS) $(GSS_FLAGS) $(SRP_FLAGS) /D "WIN32" /D "_WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL"

DB_LIBS=/libpath:$(DB_LIBPATH) $(DB_LIB)
OPENSSL_LIBS=/libpath:$(OPENSSL_LIBPATH) libeay32.lib

# Where to install files from this directory
libdir = $(prefix)\lib
bindir = $(prefix)\bin\sasl2

all : all-recursive

#
# /I flag to xcopy tells to treat the last parameter as directory and create all missing levels
#
# In order to force xcopy not to confirm if the second parameter is file or directory,
# the first parameter has to contain a wildcard character. For example, we use libsasl.l*,
# instead of libsasl.lib. Ugly, but works!
#
# Note, that we will copy all dlls here, not just $(PLUGINS). This is a bug, but it allows
# us to copy GSSAPI plugin, which might not be in $(PLUGINS).
#
install: $(PLUGINS)
	@xcopy *.dll $(bindir) /I /F /Y

all-recursive : $(PLUGINS)

getaddrinfo.c: ..\lib\getaddrinfo.c
	copy ..\lib\getaddrinfo.c .

getnameinfo.c: ..\lib\getnameinfo.c
	copy ..\lib\getnameinfo.c .

allockey.c: ..\sasldb\allockey.c
	copy ..\sasldb\allockey.c .

db_berkeley.c: ..\sasldb\db_berkeley.c
	copy ..\sasldb\db_berkeley.c .

#Add /pdb: option?

saslANONYMOUS.dll: $(saslANONYMOUS_objs) saslANONYMOUS.res
	$(LINK32DLL) @<< $(LINK32DLL_FLAGS) /out:"saslANONYMOUS.dll" /implib:"saslANONYMOUS.lib" $(saslANONYMOUS_objs) saslANONYMOUS.res
<<

saslPLAIN.dll: $(saslPLAIN_objs) saslPLAIN.res
	$(LINK32DLL) @<< $(LINK32DLL_FLAGS) /out:"saslPLAIN.dll" /implib:"saslPLAIN.lib" $(saslPLAIN_objs) saslPLAIN.res
<<

saslCRAMMD5.dll: $(saslCRAMMD5_objs) saslCRAMMD5.res
	$(LINK32DLL) @<< $(LINK32DLL_FLAGS) /out:"saslCRAMMD5.dll" /implib:"saslCRAMMD5.lib" $(saslCRAMMD5_objs) saslCRAMMD5.res
<<

saslDIGESTMD5.dll: $(saslDIGESTMD5_objs) saslDIGESTMD5.res
	$(LINK32DLL) @<< $(LINK32DLL_FLAGS) /out:"saslDIGESTMD5.dll" /implib:"saslDIGESTMD5.lib" $(saslDIGESTMD5_objs) saslDIGESTMD5.res
<<

saslLOGIN.dll: $(saslLOGIN_objs) saslLOGIN.res
	$(LINK32DLL) @<< $(LINK32DLL_FLAGS) /out:"saslLOGIN.dll" /implib:"saslLOGIN.lib" $(saslLOGIN_objs) saslLOGIN.res
<<

saslNTLM.dll: $(saslNTLM_objs) saslNTLM.res
	$(LINK32DLL) @<< $(OPENSSL_LIBS) $(LINK32DLL_FLAGS) /out:"saslNTLM.dll" /implib:"saslNTLM.lib" $(saslNTLM_objs) saslNTLM.res
<<

saslGSSAPI.dll: $(saslGSSAPI_objs) saslGSSAPI.res
	$(LINK32DLL) @<< $(GSS_LIBS) $(LINK32DLL_FLAGS) /out:"saslGSSAPI.dll" /implib:"saslGSSAPI.lib" $(saslGSSAPI_objs) saslGSSAPI.res
<<

saslSRP.dll: $(saslSRP_objs) saslSRP.res
	$(LINK32DLL) @<< $(OPENSSL_LIBS) $(LINK32DLL_FLAGS) /out:"saslSRP.dll" /implib:"saslSRP.lib" $(saslSRP_objs) saslSRP.res
<<

saslOTP.dll: $(saslOTP_objs) saslOTP.res
	$(LINK32DLL) @<< $(OPENSSL_LIBS) $(LINK32DLL_FLAGS) /out:"saslOTP.dll" /implib:"saslOTP.lib" $(saslOTP_objs) saslOTP.res
<<

saslSASLDB.dll: $(saslSASLDB_objs) saslSASLDB.res
	$(LINK32DLL) @<< $(DB_LIBS) $(LINK32DLL_FLAGS) /out:"saslSASLDB.dll" /implib:"saslSASLDB.lib" $(saslSASLDB_objs) saslSASLDB.res
<<

CLEAN :
	-@erase $(all_objs)
	-@erase "*.idb"
	-@erase "*.pdb"
	-@erase getaddrinfo.c
	-@erase allockey.c
	-@erase db_berkeley.c
	-@erase getnameinfo.c
	-@erase "*.rc"
	-@erase "*.res"
	-@erase $(all_out)

.c.obj::
   $(CPP) @<<
   $(CPP_PROJ) $< 
<<

.cpp.obj::
   $(CPP) @<<
   $(CPP_PROJ) $< 
<<

.cxx.obj::
   $(CPP) @<<
   $(CPP_PROJ) $< 
<<

.rc.res:
	rc $<

saslANONYMOUS.rc saslPLAIN.rc saslCRAMMD5.rc saslDIGESTMD5.rc saslLOGIN.rc saslNTLM.rc saslGSSAPI.rc saslSRP.rc saslOTP.rc saslSASLDB.rc:
	copy <<temp.rc $@
#include "afxres.h"

VS_VERSION_INFO VERSIONINFO
 FILEVERSION $(SASL_VERSION_MAJOR),$(SASL_VERSION_MINOR),$(SASL_VERSION_STEP),0
 PRODUCTVERSION $(SASL_VERSION_MAJOR),$(SASL_VERSION_MINOR),$(SASL_VERSION_STEP),0
 FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
 FILEFLAGS 0x1L
#else
 FILEFLAGS 0x0L
#endif
 FILEOS 0x40004L
 FILETYPE 0x1L
 FILESUBTYPE 0x0L
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904b0"
        BEGIN
            VALUE "CompanyName", "Carnegie Mellon University\0"
            VALUE "FileDescription", "CMU SASL $(@B) plugin\0"
            VALUE "FileVersion", "$(SASL_VERSION_MAJOR).$(SASL_VERSION_MINOR).$(SASL_VERSION_STEP).0\0"
            VALUE "InternalName", "$(@B)\0"
            VALUE "LegalCopyright", "Copyright (c) Carnegie Mellon University 2004\0"
            VALUE "OriginalFilename", "$(@B).dll\0"
            VALUE "ProductName", "Carnegie Mellon University SASL\0"
            VALUE "ProductVersion", "$(SASL_VERSION_MAJOR).$(SASL_VERSION_MINOR).$(SASL_VERSION_STEP)-0"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x409, 1200
    END
END
<<
