# -*- mode: makefile; -*-

!include options.w32.mk

BUILD_LOG = build.log

# C preprocessor flags
CPPFLAGS = $(CPPFLAGS)\
	-Icurl-$(CURL_VERSION)/include\
	-Ignu-bundle-$(GNU_BUNDLE_DATE)/include\
	-Ihunspell-$(HUNSPELL_VERSION)/include\
	-Ilibressl-$(LIBRESSL_VERSION)/include\
	-Isrc/include

DEST = c:\out\swirc-$(MACHINE)

# Locale location
LOCLOC = LC_MESSAGES\swirc.mo

PRODUCT_VERSION = 3.5.9
REVISION =

all: main

!include src/commands/build.w32.mk
!include src/events/build.w32.mk
!include src/build.w32.mk

!include htmlhelp/build.mk

main: $(TGTS)

.c.obj:
	$(E) ^ ^ CC^ ^ ^ ^ ^ ^ $@
	$(Q) $(CC) $(CFLAGS) $(CPPFLAGS) -c -Fo$*.obj $< 1>>$(BUILD_LOG)

.cpp.obj:
	$(E) ^ ^ CXX^ ^ ^ ^ ^ $@
	$(Q) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -Fo$*.obj $< 1>>$(BUILD_LOG)

# XXX: Add the MINGW DLLs? (Hunspell)
DIST_DEPS = swirc.exe\
	"curl-$(CURL_VERSION)\$(MACHINE)\libcurl.dll"\
	"gnu-bundle-$(GNU_BUNDLE_DATE)\$(MACHINE)\$(NAME_libcharset).dll"\
	"gnu-bundle-$(GNU_BUNDLE_DATE)\$(MACHINE)\$(NAME_libiconv).dll"\
	"gnu-bundle-$(GNU_BUNDLE_DATE)\$(MACHINE)\$(NAME_libidn).dll"\
	"gnu-bundle-$(GNU_BUNDLE_DATE)\$(MACHINE)\$(NAME_libintl).dll"\
	"hunspell-$(HUNSPELL_VERSION)\$(MACHINE)\$(NAME_libhunspell).dll"\
	"pdcurses-$(PDCURSES_VERSION)\$(MACHINE)\pdcurses.dll"\
	"src\trusted_roots.pem"

dist: $(DIST_DEPS) $(HTMLHELP_DIR)swirc.chm
!	if exists($(DEST))
		rmdir /s /q $(DEST)
!	endif
	mkdir $(DEST)
	mkdir $(DEST)\de\LC_MESSAGES
	mkdir $(DEST)\fi\LC_MESSAGES
	mkdir $(DEST)\fr\LC_MESSAGES
	mkdir $(DEST)\sv\LC_MESSAGES
	copy "curl-$(CURL_VERSION)\$(MACHINE)\libcurl.dll" $(DEST)
	copy "gnu-bundle-$(GNU_BUNDLE_DATE)\$(MACHINE)\$(NAME_libcharset).dll" $(DEST)
	copy "gnu-bundle-$(GNU_BUNDLE_DATE)\$(MACHINE)\$(NAME_libiconv).dll" $(DEST)
	copy "gnu-bundle-$(GNU_BUNDLE_DATE)\$(MACHINE)\$(NAME_libidn).dll" $(DEST)
	copy "gnu-bundle-$(GNU_BUNDLE_DATE)\$(MACHINE)\$(NAME_libintl).dll" $(DEST)
	copy "hunspell-$(HUNSPELL_VERSION)\$(MACHINE)\$(NAME_libhunspell).dll" $(DEST)
	copy "hunspell-$(HUNSPELL_VERSION)\$(MACHINE)\$(DLL_libgcc)" $(DEST)
	copy "hunspell-$(HUNSPELL_VERSION)\$(MACHINE)\$(DLL_libstdcpp)" $(DEST)
	copy "hunspell-$(HUNSPELL_VERSION)\$(MACHINE)\$(DLL_libwinpthread)" $(DEST)
	copy "hunspell-en-us\en_US.aff" $(DEST)
	copy "hunspell-en-us\en_US.dic" $(DEST)
	copy "libressl-$(LIBRESSL_VERSION)\$(MACHINE)\$(DLL_crypto)" $(DEST)
	copy "libressl-$(LIBRESSL_VERSION)\$(MACHINE)\$(DLL_ssl)" $(DEST)
	copy "pdcurses-$(PDCURSES_VERSION)\$(MACHINE)\pdcurses.dll" $(DEST)
	copy "src\trusted_roots.pem" $(DEST)
	copy "swirc.exe" $(DEST)
	copy $(HTMLHELP_DIR)swirc.chm $(DEST)
	copy swirc-locales-$(LOCALES_SNAP)\de\$(LOCLOC) $(DEST)\de\LC_MESSAGES
	copy swirc-locales-$(LOCALES_SNAP)\fi\$(LOCLOC) $(DEST)\fi\LC_MESSAGES
	copy swirc-locales-$(LOCALES_SNAP)\fr\$(LOCLOC) $(DEST)\fr\LC_MESSAGES
	copy swirc-locales-$(LOCALES_SNAP)\sv\$(LOCLOC) $(DEST)\sv\LC_MESSAGES

ALG = SHA256
MSI_ARCH = x64
SIGNTOOL_FLAGS = -f "C:\CERTS\SwircDevelopmentTeam.pfx"\
	-fd $(ALG)\
	-tr "http://timestamp.digicert.com"\
	-td $(ALG)\
	-v

setupfile: dist Swirc.wxs
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(DLL_libgcc)"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(DLL_libstdcpp)"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(DLL_libwinpthread)"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(NAME_libcharset).dll"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(NAME_libhunspell).dll"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(NAME_libiconv).dll"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(NAME_libidn).dll"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(NAME_libintl).dll"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\libcurl.dll"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(DLL_crypto)"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\$(DLL_ssl)"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\pdcurses.dll"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" "$(DEST)\swirc.exe"
	wix build -arch $(MSI_ARCH) -culture "en-us" -ext WixToolset.UI.wixext Swirc.wxs -out "Swirc-$(PRODUCT_VERSION)$(REVISION).msi"
	signtool sign $(SIGNTOOL_FLAGS) -p "$(PASSWD)" \
	    "Swirc-$(PRODUCT_VERSION)$(REVISION).msi"

clean:
	$(E) ^ ^ CLEAN
	$(RM) "*.msi"
	$(RM) "*.wixpdb"
	$(RM) "src\*.obj"
	$(RM) "src\commands\*.obj"
	$(RM) "src\events\*.obj"
	$(RM) "src\trusted_roots.pem"
	$(RM) $(BUILD_LOG)
	$(RM) $(TGTS)
	$(RM) Swirc.wixobj
	$(RM) curl-$(CURL_VERSION).cab
	$(RM) gnu-bundle-$(GNU_BUNDLE_DATE).cab
	$(RM) hunspell-$(HUNSPELL_VERSION).cab
	$(RM) hunspell-en-us.cab
	$(RM) libressl-$(LIBRESSL_VERSION).cab
	$(RM) pdcurses-$(PDCURSES_VERSION).cab
	$(RM) swirc-locales-$(LOCALES_SNAP).cab
	$(RM) swirc.res
!	if exists(cov-int)
		rmdir /s /q cov-int
!	endif
!	if exists(cppcheck-data)
		rmdir /s /q cppcheck-data
!	endif
!	if exists(curl-$(CURL_VERSION))
		rmdir /s /q curl-$(CURL_VERSION)
!	endif
!	if exists(gnu-bundle-$(GNU_BUNDLE_DATE))
		rmdir /s /q gnu-bundle-$(GNU_BUNDLE_DATE)
!	endif
!	if exists(hunspell-$(HUNSPELL_VERSION))
		rmdir /s /q hunspell-$(HUNSPELL_VERSION)
!	endif
!	if exists(hunspell-en-us)
		rmdir /s /q hunspell-en-us
!	endif
!	if exists(libressl-$(LIBRESSL_VERSION))
		rmdir /s /q libressl-$(LIBRESSL_VERSION)
!	endif
!	if exists(pdcurses-$(PDCURSES_VERSION))
		rmdir /s /q pdcurses-$(PDCURSES_VERSION)
!	endif
!	if exists(swirc-locales-$(LOCALES_SNAP))
		rmdir /s /q swirc-locales-$(LOCALES_SNAP)
!	endif
!	if exists(tmp)
		rmdir /s /q tmp
!	endif
	$(Q) cd po
	$(Q) $(MAKE) -f Makefile.vc clean
	$(Q) cd ..
