################################### tell Emacs this is a -*- makefile-gmake -*-
#
# Copyright (C) 2011-2016 SIPE Project <http://sipe.sourceforge.net/>
#
# Makefile.mingw
#
# Author: zup@sbox.tugraz.at
# Date 8/28/07
# Description: Makefile for win32 (mingw) version of libsipe
#
###############################################################################
#
# Configuration option:
#  - USE_SSPI defined:     NTLM, Kerberos, TLS-DSK & Single Sign-On supported
#  - USE_SSPI not defined: NTLM & TLS-DSK but without Single Sign-On support
#             [eg.: make -f Makefile.mingw USE_SSPI= ]
#
# @TODO: HAVE_GSSAPI_GSSAPI_H support for USE_SSPI=. Where to find the libraries?
#        Those would need to be packaged alongside the plugin.
#
USE_SSPI := 1

ifdef PIDGIN_TREE_TOP
# standalone MinGW build
OLD_PIDGIN_TREE_TOP := $(PIDGIN_TREE_TOP)
PIDGIN_TREE_TOP     := ../$(OLD_PIDGIN_TREE_TOP)
else
# MinGW cross-compile build (see contrib/mingw-cross-compile/README.txt)
PIDGIN_TREE_TOP := ../../..
VERSION         := $(shell cat ../../VERSION)
endif

include $(PIDGIN_TREE_TOP)/libpurple/win32/global.mak

TARGET = libsipe

DLL_INSTALL_DIR = $(PURPLE_INSTALL_PLUGINS_DIR)

# dump higher level defines
DEFINES  =
DEFINES += -DENABLE_NLS=1
DEFINES += -DPACKAGE_BUGREPORT=\"https://sourceforge.net/p/sipe/bugs/\"
DEFINES += -DPACKAGE_NAME=\"pidgin-sipe\"
DEFINES += -DPACKAGE_URL=\"http://sipe.sourceforge.net/\"
DEFINES += -DPACKAGE_VERSION=\"$(VERSION)\"
DEFINES += -DSIPE_TRANSLATIONS_URL=\"https://www.transifex.com/stefanb/pidgin-sipe/\"
#DEFINES += -DENABLE_OCS2005_MESSAGE_HACK=1

ifdef USE_SSPI
DEFINES += -DHAVE_SSPI=1
endif

##
## INCLUDE PATHS
##
INCLUDE_PATHS +=	-I. \
			-I../api \
			-I../purple \
			-I$(GTK_TOP)/include \
			-I$(GTK_TOP)/include/glib-2.0 \
			-I$(GTK_TOP)/lib/glib-2.0/include \
			-I$(LIBXML2_TOP)/include/libxml2 \
			-I$(NSPR_TOP)/include \
			-I$(NSS_TOP)/include

PURPLE_INCLUDE_PATHS += -I$(PURPLE_TOP) \
			-I$(PURPLE_TOP)/win32 \
			-I$(PIDGIN_TREE_TOP)

##
##  SOURCES, OBJECTS
##
CLEAN_C_SRC =		sip-soap.c \
			sip-transport.c \
			sipe-conf.c \
			sipe-core.c \
			sipe-domino.c \
			sipe-buddy.c \
			sipe-cal.c \
			sipe-certificate.c \
			sipe-cert-crypto-nss.c \
			sipe-chat.c \
			sipe-crypt-nss.c \
			sipe-dialog.c \
			sipe-digest-nss.c \
			sipe-ft.c \
			sipe-ft-tftp.c \
			sipe-group.c \
			sipe-groupchat.c \
			sipe-http.c \
			sipe-http-request.c \
			sipe-http-transport.c \
			sipe-im.c \
			sipe-incoming.c \
			sipe-lync-autodiscover.c \
			sipe-mime-common.c \
			sipe-notify.c \
			sipe-ocs2005.c \
			sipe-ocs2007.c \
			sipe-schedule.c \
			sipe-session.c \
			sipe-status.c \
			sipe-subscriptions.c \
			sipe-svc.c \
			sipe-tls.c \
			sipe-ucs.c \
			sipe-user.c \
			sipe-utils.c \
			sipe-ews.c \
			sipe-ews-autodiscover.c \
			sipmsg.c \
			sipe-sign.c \
			sip-sec.c \
			sip-sec-basic.c \
			sip-sec-digest.c \
			sip-sec-tls-dsk.c \
			sip-csta.c \
			sipe-webticket.c \
			sipe-xml.c \
			uuid.c \
			sipe-win32dep.c

PURPLE_C_SRC = 		../purple/purple-buddy.c \
			../purple/purple-chat.c \
			../purple/purple-connection.c \
			../purple/purple-debug.c \
			../purple/purple-dnsquery.c \
			../purple/purple-ft.c \
			../purple/purple-groupchat.c \
			../purple/purple-im.c \
			../purple/purple-markup.c \
			../purple/purple-mime.c \
			../purple/purple-network.c \
			../purple/purple-notify.c \
			../purple/purple-plugin.c \
			../purple/purple-plugin-common.c \
			../purple/purple-schedule.c \
			../purple/purple-search.c \
			../purple/purple-setting.c \
			../purple/purple-status.c \
			../purple/purple-transport.c \
			../purple/purple-user.c

C_TEST_SRC = 		sipe-xml-tests.c

ifdef USE_SSPI
CLEAN_C_SRC +=		sip-sec-sspi.c
else
CLEAN_C_SRC +=		md4.c \
			sip-sec-ntlm.c

C_TEST_SRC += 		sip-sec-ntlm-tests.c \
			../purple/tests.c
endif

C_SRC =			$(CLEAN_C_SRC) $(PURPLE_C_SRC)

RC_SRC =		libsiperc.rc

CLEAN_OBJECTS =		$(CLEAN_C_SRC:%.c=%.o)
PURPLE_OBJECTS = 	$(PURPLE_C_SRC:%.c=%.o)
RC_OBJECTS =		$(RC_SRC:%.rc=%.o)
OBJECTS = 		$(CLEAN_OBJECTS) $(PURPLE_OBJECTS) $(RC_OBJECTS)

# Only these modules need the purple headers
$(PURPLE_OBJECTS):	INCLUDE_PATHS += $(PURPLE_INCLUDE_PATHS)

TEST_OBJECTS = 		$(C_TEST_SRC:%.c=%.o)

##
## LIBRARIES
##
LIB_PATHS =		-L$(GTK_TOP)/lib \
			-L$(LIBXML2_TOP)/lib \
			-L$(NSS_TOP)/lib \
			-L$(PURPLE_TOP)

LIBS =			-lglib-2.0 \
			-lgobject-2.0 \
			-lintl \
			-lxml2 \
			-lnss3 \
			-lsmime3 \
			-lnspr4 \
			-lws2_32 \
			-lpurple

ifdef USE_SSPI
LIBS +=			-lsecur32
endif

# These flags are used in mingw build
TESTS_WARN = -Werror -Wall -Wextra -Waggregate-return -Wcast-align -Winit-self -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wundef

include $(PIDGIN_COMMON_RULES)

##
## TARGET DEFINITIONS
##
.PHONY: all clean install

all: $(TARGET).dll

_comma = ,

libsiperc.rc: libsiperc.rc.in
	sed \
		-e 's/@SIPE_VERSION@/$(VERSION)/' \
		-e 's/#SIPE_VERSION#/$(subst .,$(_comma),$(VERSION))/' \
		<$< >$@

$(OBJECTS): $(PURPLE_CONFIG_H)

$(TARGET).dll: $(PURPLE_DLL).a $(OBJECTS)
	$(CC) -shared $(OBJECTS) $(LIB_PATHS) $(LIBS) $(DLL_LD_FLAGS) -o $@.dbgsym
	$(STRIP) --strip-unneeded $@.dbgsym -o $@

##
## CLEAN RULES
##
clean: tests-clean
	rm -f $(RC_SRC)
	rm -f $(OBJECTS)
	rm -f $(TARGET).dll

rmbak:
	rm -f *~

install:
	test -z "$(DLL_INSTALL_DIR)" || mkdir -p "$(DLL_INSTALL_DIR)"
	/usr/bin/install -c $(TARGET).dll $(TARGET).dll.dbgsym '$(DLL_INSTALL_DIR)'

$(TEST_OBJECTS):

tests: tests-clean $(TEST_OBJECTS)
	$(CC) sipe-utils.o uuid.o sipe-xml.o sipe-xml-tests.o -L. $(LIB_PATHS) $(LIBS) -lsipe -o sipe-xml-tests.exe
	./sipe-xml-tests.exe
ifdef USE_SSPI
# nothing to do
else
	$(CC) ../purple/purple-debug.o ../purple/purple-markup.o ../purple/purple-network.o md4.o sipe-digest.o sipe-crypt.o sipe-mime.o sipe-sign.o sipmsg.o sipe-utils.o uuid.o sip-sec-ntlm-tests.o ../purple/tests.o  -L. $(LIB_PATHS) $(LIBS) -lsipe -o ../purple/tests.exe
	../purple/tests.exe
endif

tests-clean:
	rm -f $(TEST_OBJECTS)
	rm -f sipe-xml-tests.exe ../purple/tests.exe

include $(PIDGIN_COMMON_TARGETS)
