########################################################################
#
# Copyright (c) 2010-2013, 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\installer

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

!ifdef BUILD_INSTALLERS

VERSIOND=$(VER_PRODUCT_MAJOR)-$(VER_PRODUCT_MINOR)-$(VER_PRODUCT_AUX)-$(VER_PRODUCT_PATCH)
VERSION=$(VER_PRODUCT_MAJOR).$(VER_PRODUCT_MINOR).$(VER_PRODUCT_AUX).$(VER_PRODUCT_PATCH)
POLPREFIX=policy.$(VER_PRODUCT_MAJOR).$(VER_PRODUCT_MINOR)

!if "$(CPU)"=="AMD64"
PLATFORM=x64
!else
PLATFORM=x86
!endif

!if "$(BUILD)"=="rel"
DEBUGOPT=
!else
DEBUGOPT=-$(BUILD)
!endif

######################################################################
# Heimdal Merge Modules

!if exist(..\..\..\thirdparty\installer\heimdal-assemblies.wxs)
ASM_WXS=..\..\..\thirdparty\installer\heimdal-assemblies.wxs
!else
ASM_WXS=heimdal-assemblies.wxs
!endif

ASSEMBLYMODULE=$(INSTDIR)\Heimdal.msm

$(ASSEMBLYMODULE): $(OBJ)\heimdal-assemblies.wixobj
	$(LIGHT) -out $@ $**
	$(_CODESIGN)

$(OBJ)\heimdal-assemblies.wixobj: $(ASM_WXS)
	$(CANDLE) -arch $(PLATFORM) -o $@ $**	\
		-dVersion=$(VERSION)		\
		-dBinDir=$(BINDIR)		\
		-dAsmKrbName=$(ASMKRBNAME)		\
		-dPlatform=$(PLATFORM)

$(SDKREDISTDIR)\Heimdal.msm: $(ASSEMBLYMODULE)
	$(CP) $** $@

!if exist(..\..\..\thirdparty\installer\heimdal-policy.wxs)
POLICY_WXS=..\..\..\thirdparty\installer\heimdal-policy.wxs
!else
POLICY_WXS=heimdal-policy.wxs
!endif

POLICYMODULE=$(INSTDIR)\Heimdal.Policy.msm

$(POLICYMODULE): $(OBJ)\heimdal-policy.wixobj
	$(LIGHT) -out $@ $**
	$(_CODESIGN)

$(OBJ)\heimdal-policy.wixobj: $(POLICY_WXS)
	$(CANDLE) -arch $(PLATFORM) -o $@ $**	\
		-dVersion=$(VERSION)		\
		-dBinDir=$(BINDIR)		\
		-dAsmKrbName=$(ASMKRBNAME)		\
		-dPlatform=$(PLATFORM)		\
		-dPolPrefix=$(POLPREFIX)

COMMANDMODULE=$(INSTDIR)\Heimdal.Command.msm

$(COMMANDMODULE): $(OBJ)\heimdal-command.wixobj
	$(LIGHT) -out $@ $**
	$(_CODESIGN)

# OBJECT COMPILED BELOW

$(SDKREDISTDIR)\Heimdal.Command.msm: $(COMMANDMODULE)
	$(CP) $** $@

GSSMODULE=$(INSTDIR)\Heimdal.GSS.msm

$(GSSMODULE): $(OBJ)\heimdal-gss.wixobj
	$(LIGHT) -out $@ $**
	$(_CODESIGN)

# OBJECT COMPILED BELOW

all:: $(ASSEMBLYMODULE) $(SDKREDISTDIR)\Heimdal.msm

all:: $(POLICYMODULE)

all:: $(COMMANDMODULE) $(SDKREDISTDIR)\Heimdal.Command.msm

all:: $(GSSMODULE)

clean::
	-$(RM) $(ASSEMBLYMODULE)
	-$(RM) $(POLICYMODULE)
	-$(RM) $(COMMANDMODULE)
	-$(RM) $(GSSMODULE)
	-$(RM) $(SDKREDISTDIR)\*.*

######################################################################
# Runtime modules

!if [ $(PERL) $(SRC)\cf\w32-detect-vc-version.pl $(CC) ]==19
VCVER=VC2019
!elseif [ $(PERL) $(SRC)\cf\w32-detect-vc-version.pl $(CC) ]==18
VCVER=VC2018
!elseif [ $(PERL) $(SRC)\cf\w32-detect-vc-version.pl $(CC) ]==17
VCVER=VC2017
!elseif [ $(PERL) $(SRC)\cf\w32-detect-vc-version.pl $(CC) ]==16
VCVER=VC100
!elseif [ $(PERL) $(SRC)\cf\w32-detect-vc-version.pl $(CC) ]==15
VCVER=VC90
!elseif [ $(PERL) $(SRC)\cf\w32-detect-vc-version.pl $(CC) ]==14
VCVER=VC80
!else
!  error Cant determine Visual C compiler version
!endif

$(SDKREDISTDIR)\heimdal-version.mak:
   @echo HEIMDAL_VCVER=$(VCVER)  > $@
!if "$(BUILD)"!="rel"
   @echo HEIMDAL_CRT_DEBUG=yes   >> $@
!endif

all:: $(SDKREDISTDIR)\heimdal-version.mak

!if "$(BUILD)"=="rel"
CRTNAME=CRT
!else
CRTNAME=DebugCRT
!endif

!if "$(MMDIR)"==""
MMDIR=$(ProgramFiles)\Common Files\Merge Modules
!if !exist($(MMDIR))
MMDIR=$(SystemDrive)\Program Files (x86)\Common Files\Merge Modules
!endif
!endif

#
# Don't specify a runtime module when the Universal C Runtime
# is available.
#
!if "$(APPVER)"=="10.0"
RUNTIMEMODULE32=""
RUNTIMEMODULE64=""
!else
! if exist("$(MMDIR)")
RUNTIMEMODULE32="$(MMDIR)\Microsoft_$(VCVER)_$(CRTNAME)_x86.msm"
!  if "$(VCVER)"=="VC90" || "$(VCVER)"=="VC80"
RUNTIMEMODULE64="$(MMDIR)\Microsoft_$(VCVER)_$(CRTNAME)_x86_x64.msm"
!  else
RUNTIMEMODULE64="$(MMDIR)\Microsoft_$(VCVER)_$(CRTNAME)_x64.msm"
!  endif
! else
RUNTIMEMODULE32="$(MSSDK)\Redist\VC\microsoft.vcxx.crt.x86_msm.msm"
RUNTIMEMODULE64="$(MSSDK)\Redist\VC\microsoft.vcxx.crt.x64_msm.msm"
! endif
!endif

######################################################################
# Heimdal installer

CANDLEOPTS=	\
	-dVersion=$(VERSION)			\
	-dBinDir=$(BINDIR)			\
	-dDocDir=$(DOCDIR)			\
	-dSrcDir=$(SRC)				\
	-dPlatform=$(PLATFORM)			\
	-dAsmKrbName=$(ASMKRBNAME)		\
	-dPolPrefix=$(POLPREFIX)

!if "$(CPU)"=="AMD64"

INSTDIR32=$(INSTDIR:AMD64=i386)

CANDLEOPTS=$(CANDLEOPTS) -dTarget64	\
	-dInstDir32=$(INSTDIR32)	\
	-dInstDir64=$(INSTDIR)			\
	-dRuntimeModule32=$(RUNTIMEMODULE32)	\
	-dRuntimeModule64=$(RUNTIMEMODULE64)

!ifdef MULTIPLATFORM_INSTALLER
CANDLEOPTS_MULTI= -dTarget32
TYPEOPT=-full

INSTDEPS= \
	$(INSTDIR32)\Heimdal.msm \
	$(INSTDIR32)\Heimdal.GSS.msm	\
	$(INSTDIR32)\Heimdal.Policy.msm	\
	$(ASSEMBLYMODULE) $(GSSMODULE) $(POLICYMODULE)
!else
!  message Excluding 32-bit components from installer
TYPEOPT=

INSTDEPS= \
	$(ASSEMBLYMODULE) $(GSSMODULE) $(POLICYMODULE)
!endif


!else

CANDLEOPTS=$(CANDLEOPTS) -dTarget32		\
	-dInstDir32=$(INSTDIR)			\
	-dRuntimeModule32=$(RUNTIMEMODULE32)	\

INSTDEPS= \
	$(ASSEMBLYMODULE) $(GSSMODULE) $(POLICYMODULE)
!endif

!if exist($(SRC)\thirdparty\MKShim)
THIRDPARTYOBJS=$(THIRDPARTYOBJS) $(INSTDIR)\MKShim.wixlib
THIRDPARTYOPTS=$(THIRDPARTYOPTS) -dMKShim=1

!if "$(CPU)"=="AMD64"
THIRDPARTYOBJS=$(THIRDPARTYOBJS) $(INSTDIR32)\MKShim.wixlib
!endif
!endif

INSTALLER=$(INSTDIR)\Heimdal-$(CPU)$(DEBUGOPT)$(TYPEOPT)-$(VERSIOND).msi
WIXOBJ=$(OBJ)\heimdal-installer$(TYPEOPT).wixobj

$(INSTALLER): $(WIXOBJ) $(THIRDPARTYOBJS) $(INSTDEPS) lang\en-us.wxl
	$(LIGHT) -out $@ $(WIXOBJ) $(THIRDPARTYOBJS)	\
		-sval -cultures:en-us -loc lang\en-us.wxl	\
		-dWixUILicenseRtf=lang\license-en-us.rtf	\
		-ext WixUIExtension
	$(_CODESIGN)

$(WIXOBJ): heimdal-installer.wxs
	$(CANDLE) -arch $(PLATFORM) -o $@ heimdal-installer.wxs \
		$(CANDLEOPTS) $(CANDLEOPTS_MULTI) $(THIRDPARTYOPTS)

!if exist(thirdparty\installer\heimdal-command.wxs)
CMD_WXS=thirdparty\installer\heimdal-command.wxs
!else
CMD_WXS=heimdal-command.wxs
!endif

$(OBJ)\heimdal-command.wixobj: $(CMD_WXS)
	$(CANDLE) -arch $(PLATFORM) -o $@ $(CMD_WXS) \
		$(CANDLEOPTS) $(THIRDPARTYOPTS)

!if exist(thirdparty\installer\heimdal-gss.wxs)
GSS_WXS=thirdparty\installer\heimdal-gss.wxs
!else
GSS_WXS=heimdal-gss.wxs
!endif

$(OBJ)\heimdal-gss.wixobj: $(GSS_WXS)
	$(CANDLE) -arch $(PLATFORM) -o $@ $(GSS_WXS) \
		$(CANDLEOPTS) $(THIRDPARTYOPTS)

all:: $(INSTALLER)

clean::
	-$(RM) $(INSTALLER)

!endif				# BUILD_INSTALLERS
