########################################################################
#
# Copyright (c) 2009, Secure Endpoints Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in
#   the documentation and/or other materials provided with the
#   distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

RELDIR=packages\windows\kfw_shim

!include ../../../windows/NTMakefile.w32

!if "$(CPU)"=="i386"
FILES=	\
	$(BINDIR)\comerr32.dll	\
	$(BINDIR)\gssapi32.dll	\
	$(BINDIR)\krb5_32.dll	\
#	$(BINDIR)\krbcc32.dll	\
#	$(BINDIR)\wshelp32.dll	\
#	$(BINDIR)\xpprof32.dll

!elseif "$(CPU)"=="AMD64"
FILES=	\
	$(BINDIR)\comerr64.dll	\
	$(BINDIR)\gssapi64.dll	\
	$(BINDIR)\krb5_64.dll	\
#	$(BINDIR)\krbcc64.dll	\
#	$(BINDIR)\wshelp64.dll	\
#	$(BINDIR)\xpprof64.dll

!else
!  error Unknown CPU value
!endif

GENDEF=$(PERL) $(SRC)\cf\w32-def-from-dll.pl

# ----------------------------------------------------------------------
# comerr32.dll shim

COMERR_SHIM_OBJS=$(OBJ)\comerr_shim.obj

$(BINDIR)\comerr32.dll: $(COMERR_SHIM_OBJS)
	$(DLLGUILINK) -out:$@ -def:comerr32.def $(LIBCOMERR)
	( ( if exist $@.manifest $(MT) -outputresource:$@;2 -manifest $@.manifest $(APPMANIFEST) ) && $(_VC_MANIFEST_CLEAN) && $(_CODESIGN) ) || $(RM) $@

# ----------------------------------------------------------------------
# comerr64.dll shim

$(BINDIR)\comerr64.dll: $(COMERR_SHIM_OBJS)
	$(DLLGUILINK) -out:$@ -def:comerr64.def $(LIBCOMERR)
	( ( if exist $@.manifest $(MT) -outputresource:$@;2 -manifest $@.manifest $(APPMANIFEST) ) && $(_VC_MANIFEST_CLEAN) && $(_CODESIGN) ) || $(RM) $@

# ----------------------------------------------------------------------
# gssapi32.dll shim

GSSAPI_SHIM_OBJS=$(OBJ)\gssapi_shim.obj

$(BINDIR)\gssapi32.dll: $(GSSAPI_SHIM_OBJS)
	$(DLLGUILINK) -out:$@ -def:gssapi32.def $(LIBGSSAPI)
	( ( if exist $@.manifest $(MT) -outputresource:$@;2 -manifest $@.manifest $(APPMANIFEST) ) && $(_VC_MANIFEST_CLEAN) && $(_CODESIGN) ) || $(RM) $@

# ----------------------------------------------------------------------
# gssapi64.dll shim

$(BINDIR)\gssapi64.dll: $(GSSAPI_SHIM_OBJS)
	$(DLLGUILINK) -out:$@ -def:gssapi64.def $(LIBGSSAPI)
	( ( if exist $@.manifest $(MT) -outputresource:$@;2 -manifest $@.manifest $(APPMANIFEST) ) && $(_VC_MANIFEST_CLEAN) && $(_CODESIGN) ) || $(RM) $@

# ----------------------------------------------------------------------
# krb5_32.dll shim

KRB5_SHIM_OBJS=$(OBJ)\krb5_shim.obj

$(BINDIR)\krb5_32.dll: $(KRB5_SHIM_OBJS)
	$(DLLGUILINK) -out:$@ -def:krb5_32.def $(LIBHEIMDAL)
	( ( if exist $@.manifest $(MT) -outputresource:$@;2 -manifest $@.manifest $(APPMANIFEST) ) && $(_VC_MANIFEST_CLEAN) && $(_CODESIGN) ) || $(RM) $@

# ----------------------------------------------------------------------
# krb5_64.dll shim

$(BINDIR)\krb5_64.dll: $(KRB5_SHIM_OBJS)
	$(DLLGUILINK) -out:$@ -def:krb5_64.def $(LIBHEIMDAL)
	( ( if exist $@.manifest $(MT) -outputresource:$@;2 -manifest $@.manifest $(APPMANIFEST) ) && $(_VC_MANIFEST_CLEAN) && $(_CODESIGN) ) || $(RM) $@

all:: $(FILES)

clean::
	-$(RM) $(FILES)

!ifdef DEV

comerr32.def: $(COMERR_SHIM_OBJS)
	( $(GENDEF) -mcom_err -e$(BINDIR)\com_err.dll $** "$(KFWBIN)\comerr32.dll" > $@ ) || $(RM) $@

comerr64.def: $(COMERR_SHIM_OBJS)
	( $(GENDEF) -mcom_err -e$(BINDIR)\com_err.dll $** "$(KFWBIN)\comerr64.dll" > $@ ) || $(RM) $@

gssapi32.def: $(GSSAPI_SHIM_OBJS)
	( $(GENDEF) -mgssapi -e$(BINDIR)\gssapi.dll $** "$(KFWBIN)\gssapi32.dll" > $@ ) || $(RM) $@

gssapi64.def: $(GSSAPI_SHIM_OBJS)
	( $(GENDEF) -mgssapi -e$(BINDIR)\gssapi.dll $** "$(KFWBIN)\gssapi64.dll" > $@ ) || $(RM) $@

krb5_32.def: $(KRB5_SHIM_OBJS)
	( $(GENDEF) -mheimdal -e$(BINDIR)\heimdal.dll $** "$(KFWBIN)\krb5_32.dll" > $@ ) || $(RM) $@

krb5_64.def: $(KRB5_SHIM_OBJS)
	( $(GENDEF) -mheimdal -e$(BINDIR)\heimdal.dll $** "$(KFWBIN)\krb5_64.dll" > $@ ) || $(RM) $@

!ifndef KFWBIN
gen-defs:
	echo KFWBIN should point to the directory that contains the KfW binaries
	exit /b 1
!else
!if "$(CPU)"=="i386"
gen-defs: comerr32.def gssapi32.def krb5_32.def
	echo Done.
!elseif "$(CPU)"=="AMD64"
gen-defs: comerr64.def gssapi64.def krb5_64.def
	echo Done.
!endif
!endif

!endif