RELDIR=WINNT\install\NSIS
!INCLUDE ..\..\..\config\NTMakefile.$(SYS_NAME)
!INCLUDE ..\..\..\config\NTMakefile.version

EXEDIR = $(DESTDIR)\WinInstall\Config
!IFNDEF NSISDIR
NSISDIR = C:\Program Files\NSIS
!ENDIF

# Revise the rule for compiling sources to use static libraries
C2OBJ   = $(cc) /Fo$@ /Fd$*.pdb $(cflags) $(cdebug) $(cvars) $(afscflags) $(afscdefs) /c

LOOPBACK_LIBS = wbemuuid.lib setupapi.lib msi.lib ole32.lib shell32.lib oleaut32.lib user32.lib

!IFNDEF MSIDNNLS
!ERROR Please set IDNMREDISTDIR to the directory where Microsoft IDN Mitigation APIs Redistributables are to be found.
# $(MSIDNNLS)\REDIST\idndl.(platform).exe should exist.
!ENDIF

$(OUT)\Service.obj: Service.cpp
	   $(C2OBJ) Service.cpp

$(OUT)\Service.res: Service.rc AFS_component_version_number.h

$(EXEDIR)\Service.exe: $(OUT)\Service.obj $(OUT)\Service.res
      $(EXECONLINK)
      $(_VC_MANIFEST_EMBED_EXE)
        $(CODESIGN_USERLAND)
        $(SYMSTORE_IMPORT)

$(OUT)\Killer.res: Killer.rc AFS_component_version_number.h

$(OUT)\Killer.obj: Killer.cpp
      $(C2OBJ) Killer.cpp

$(EXEDIR)\Killer.exe: $(OUT)\Killer.obj $(OUT)\Killer.res
      $(EXECONLINK)
        $(_VC_MANIFEST_EMBED_EXE)
        $(CODESIGN_USERLAND)
        $(SYMSTORE_IMPORT)

$(OUT)\AdminGroup.res: AdminGroup.rc AFS_component_version_number.h

$(OUT)\AdminGroup.obj: AdminGroup.cpp
      $(C2OBJ) AdminGroup.cpp

$(EXEDIR)\AdminGroup.exe: $(OUT)\AdminGroup.obj $(OUT)\AdminGroup.res
      $(EXECONLINK)
        $(_VC_MANIFEST_EMBED_EXE)
        $(CODESIGN_USERLAND)
        $(SYMSTORE_IMPORT)

!IF ("$(AFSDEV_BUILDTYPE)"=="FREE")
!IF ("$(CPU)" == "i386")
MSI_PLATFORM=Intel
ARCH=x86
!ELSE IF ("$(CPU)" == "amd64")
MSI_PLATFORM=x64
ARCH=x64
!ELSE
MSI_PLATFORM=$(CPU)
ARCH=$(CPU)
!ENDIF
MSI_CONFIG=Retail
!ELSE
!IF ("$(CPU)" == "i386")
MSI_PLATFORM=Intel
ARCH=x86
!ELSE IF ("$(CPU)" == "amd64")
MSI_PLATFORM=x64
ARCH=x64
!ELSE
MSI_PLATFORM=$(CPU)
ARCH=$(CPU)
!ENDIF
MSI_CONFIG=Debug
!ENDIF

!IF ("$(CPU)" == "i386")
IDNMREDIST=$(MSIDNNLS)\REDIST\idndl.x86.exe
!ELSE IF ("$(CPU)" == "amd64")
IDNMREDIST=$(MSIDNNLS)\REDIST\idndl.amd64.exe
!ELSE
! error Unknown CPU value
!ENDIF

!if ("$(AFSVER_CL)"=="1400") || ("$(AFSVER_CL)"=="1500")
MSVCMSI=$(EXEDIR)\vcruntime-$(AFSVER_CL)$(AFSDEV_BUILDTYPE).msi

$(MSVCMSI): $(OUT)\vcruntime.wixobj
	light -out $@ $**
	$(CODESIGN_USERLAND)

$(OUT)\vcruntime.wixobj: vcruntime.wxs
	candle -out $@ $** -dPlatform=$(MSI_PLATFORM) -dConfig=$(MSI_CONFIG) -dVCVer=$(AFSVER_CL)

!else
MSVCMSI=
!endif

runtime: $(MSVCMSI)


prebuild:
!IF ("$(AFSDEV_BUILDTYPE)" == "FREE")
!IF ("$(AFSVER_CL)"=="1400")
   $(COPY) "%VSINSTALLDIR%\SDK\v2.0\BootStrapper\Packages\vcredist_$(ARCH)\vcredist_$(ARCH).exe" $(EXEDIR)
!ELSE IF ("$(AFSVER_CL)"=="1310")
   $(COPY) %SystemRoot%\System32\Msvcr71.dll $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MFC71.DLL $(EXEDIR)
!ELSE IF ("$(AFSVER_CL)"=="1300")
   $(COPY) %SystemRoot%\System32\Msvcp70.dll $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MSVCRT.DLL $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MFC70.DLL $(EXEDIR)
!ELSE IF ("$(AFSVER_CL)"=="1200")
   $(COPY) %SystemRoot%\System32\MSVCRT.DLL $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MFC42.DLL $(EXEDIR)
!ELSE
!ERROR Unknown Compiler Version
!ENDIF
!ELSE # NOT FREE - CHECKED
!IF ("$(AFSVER_CL)"=="1500")
# Do nothing
!ELSE IF ("$(AFSVER_CL)"=="1400")
# Do nothing.
!ELSE IF ("$(AFSVER_CL)"=="1310")
   $(COPY) %SystemRoot%\System32\Msvcr71d.dll $(EXEDIR)
   $(COPY) %SystemRoot%\System32\Msvcr71d.pdb $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MFC71D.DLL $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MFC71D.pdb $(EXEDIR)
!ELSE IF ("$(AFSVER_CL)"=="1300")
   $(COPY) %SystemRoot%\System32\Msvcp70d.dll $(EXEDIR)
   $(COPY) %SystemRoot%\System32\Msvcp70d.pdb $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MSVCRTD.DLL $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MSVCRTD.pdb $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MFC70D.DLL $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MFC70D.pdb $(EXEDIR)
!ELSE IF ("$(AFSVER_CL)"=="1200")
   $(COPY) %SystemRoot%\System32\MSVCRTD.DLL $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MSVCRTD.pdb $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MFC42D.DLL $(EXEDIR)
   $(COPY) %SystemRoot%\System32\MFC42D.pdb $(EXEDIR)
!ELSE
!ERROR Unknown Compiler Version
!ENDIF
!ENDIF
   $(DEL) $(OUT)\nsi-includes.nsi
   echo !define AFS_DESTDIR $(DESTDIR) > $(OUT)\nsi-includes.nsi
   echo !define AFS_VERSION $(AFSPRODUCT_VERSION) >> $(OUT)\nsi-includes.nsi
   echo !define AFS_MAJORVERSION $(AFSPRODUCT_VER_MAJOR) >>$(OUT)\nsi-includes.nsi
   echo !define AFS_MINORVERSION $(AFSPRODUCT_VER_MINOR) >>$(OUT)\nsi-includes.nsi
   echo !define AFS_PATCHLEVEL $(AFSPRODUCT_VER_PATCH) >>$(OUT)\nsi-includes.nsi
   echo !define IDNMREDIST "$(IDNMREDIST)" >>$(OUT)\nsi-includes.nsi
   $(DESTDIR)\bin\util_cr.exe _echo "!define " >>$(OUT)\nsi-includes.nsi
   "$(NSISDIR)\makensis.exe" /VERSION >>$(OUT)\nsi-includes.nsi
   echo. >>$(OUT)\nsi-includes.nsi
!if ("$(AFSVER_CL)" == "1500")
   echo !define CL_1500 1 >> $(OUT)\nsi-includes.nsi
   echo !define MSVCMSI "$(MSVCMSI)" >> $(OUT)\nsi-includes.nsi
!else if ("$(AFSVER_CL)" == "1400")
   echo !define CL_1400 1 >> $(OUT)\nsi-includes.nsi
   echo !define MSVCMSI "$(MSVCMSI)" >> $(OUT)\nsi-includes.nsi
!else if ("$(AFSVER_CL)" == "1310")
   echo !define CL_1310 1 >> $(OUT)\nsi-includes.nsi
!else if ("$(AFSVER_CL)" == "1300")
   echo !define CL_1300 1 >> $(OUT)\nsi-includes.nsi
!endif
!if ("$(AFSIFS)" == "TRUE")
   echo !define AFSIFS 1 >> $(OUT)\nsi-includes.nsi
!endif
!if ("$(AFSDEV_BUILDTYPE)" == "CHECKED")
   echo !define DEBUG 1 >>$(OUT)\nsi-includes.nsi
!endif

!if ("$(AFSDEV_BUILDTYPE)" == "CHECKED")
NSISEXE=$(DESTDIR)\WinInstall\OpenAFSforWindows-DEBUG.exe
!else
NSISEXE=$(DESTDIR)\WinInstall\OpenAFSforWindows.exe
!endif

$(NSISEXE): prebuild $(MSVCMSI)
	"$(NSISDIR)\makensis.exe" /DINCLUDEDIR=$(OUT) OpenAFS.nsi
	$(CODESIGN_USERLAND)

install: $(OUT)\Service.obj $(EXEDIR)\Service.exe $(OUT)\Killer.obj $(EXEDIR)\Killer.exe $(EXEDIR)\AdminGroup.exe runtime $(NSISEXE)

clean::
   $(DEL) $(OUT)\Service.obj
   $(DEL) $(OUT)\Service.res
   $(DEL) $(EXEDIR)\Service.exe
   $(DEL) $(OUT)\Killer.obj
   $(DEL) $(OUT)\Killer.res
   $(DEL) $(EXEDIR)\Killer.exe
   $(DEL) $(OUT)\AdminGroup.obj
   $(DEL) $(OUT)\AdminGroup.res
   $(DEL) $(EXEDIR)\AdminGroup.exe
   $(DEL) nsi-include.nsi
   $(DEL) AFS_component_version_number.h
#   $(DEL) $(EXEDIR)\Msvcr71.dll
#   $(DEL) $(EXEDIR)\MFC42.DLL
!if ("$(AFSVER_CL)"=="1400") && ("$(AFSDEV_BUILDTYPE)"!="FREE")
   $(DEL) $(OUT)\vcruntime.wixobj
   $(DEL) $(MSVCMSI)
!endif
   $(DEL) $(NSISEXE)
