# This is a common include file used by Img modules.
# PHIMGTPE should be defined before inclusion as the image type, e.g. png

!ifndef PHIMGTYPE
!error PHIMGTYPE not defined!
!endif

!if "$(PROJECT)" == ""
PROJECT = tkimg$(PHIMGTYPE)
!endif

!ifndef PRJ_PACKAGE_TCLNAME
PRJ_PACKAGE_TCLNAME = img::$(PHIMGTYPE)
!endif

!ifndef CPACKAGE
CPACKAGE = Tkimg$(PHIMGTYPE)
!endif

NEED_TK_SOURCE = 1
NEED_TCL_SOURCE = 1

!ifndef PRJ_OBJS
PRJ_OBJS = $(TMP_DIR)\$(PHIMGTYPE).obj
!endif

!include "..\..\win\rules-ext.vc"

!if [echo TKIMG_VERSION = \> versions.vc] \
   || [nmakehlp -V ..\..\base\configure.ac ^[tkimg^] >> versions.vc]
!error Could not determine tkimg base version.
!endif
!include versions.vc

PRJ_INCLUDES = $(PRJ_INCLUDES) -I"$(TMP_DIR)" -I"..\..\base"
PRJ_DEFINES  = $(PRJ_DEFINES) -DPACKAGE_TCLNAME="\"$(PRJ_PACKAGE_TCLNAME)\"" -DTKIMG_VERSION="\"$(TKIMG_VERSION)"\"
PRJ_LIBS = $(PRJ_LIBS) "..\..\base\win\$(BUILDDIRTOP)\tkimgstub$(TKIMG_VERSION:.=).lib"

!ifdef REQUIRE_LIBJPEG
!if [echo LIBJPEG_VERSION = \> versions.vc] \
   || [nmakehlp -V ..\..\libjpeg\configure.ac ^[jpegtcl^] >> versions.vc]
!error Could not determine libjpeg base version.
!endif
!include versions.vc
PRJ_INCLUDES = $(PRJ_INCLUDES) -I"$(TMP_DIR)" -I../../libjpeg
PRJ_LIBS = $(PRJ_LIBS) "..\..\libjpeg\win\$(BUILDDIRTOP)\jpegtclstub$(LIBJPEG_VERSION:.=).lib"
!endif

!ifdef REQUIRE_ZLIB
!if [echo ZLIB_VERSION = \> versions.vc] \
   || [nmakehlp -V ..\..\zlib\configure.ac ^[zlibtcl^] >> versions.vc]
!error Could not determine zlib base version.
!endif
!include versions.vc
PRJ_INCLUDES = $(PRJ_INCLUDES) -I"$(TMP_DIR)" -I../../zlib
PRJ_LIBS = $(PRJ_LIBS) "..\..\zlib\win\$(BUILDDIRTOP)\zlibtclstub$(ZLIB_VERSION:.=).lib"
!endif

!ifdef REQUIRE_LIBPNG
!if [echo LIBPNG_VERSION = \> versions.vc] \
   || [nmakehlp -V ..\..\libpng\configure.ac ^[pngtcl^] >> versions.vc]
!error Could not determine libpng base version.
!endif
!include versions.vc
PRJ_INCLUDES = $(PRJ_INCLUDES) -I"$(TMP_DIR)" -I../../libpng
PRJ_LIBS = $(PRJ_LIBS) "..\..\libpng\win\$(BUILDDIRTOP)\pngtclstub$(LIBPNG_VERSION:.=).lib"
!endif

!ifdef REQUIRE_LIBTIFF
!if [echo LIBTIFF_VERSION = \> versions.vc] \
   || [nmakehlp -V ..\..\libtiff\configure.ac ^[tifftcl^] >> versions.vc]
!error Could not determine libtiff base version.
!endif
!include versions.vc
PRJ_INCLUDES = $(PRJ_INCLUDES) -I"$(TMP_DIR)" -I../../libtiff -I../../libtiff/libtiff
PRJ_LIBS = $(PRJ_LIBS) "..\..\libtiff\win\$(BUILDDIRTOP)\tifftclstub$(LIBTIFF_VERSION:.=).lib"
!endif

# We have our own install target as we want installation to go into
# a common directory.
DISABLE_TARGET_install = 1
!include "$(_RULESDIR)\targets.vc"

# Do not use the default-pkgindex target from Tcl, but include a copy here.
# This target has changed on 2020/12/09 (and was part of Tcl 8.6.11)
# to include a "string totitle", which breaks the naming convention of
# the Img package.
# The "string totitle" was rollbacked in 2021/01/04, so this workaround
# is practically only needed for Tcl 8.6.11.
# See https://core.tcl-lang.org/tcl/info/4dfa9000e1e2db8c

!if $(MULTIPLATFORM_INSTALL)
img-pkgindex:
	@echo package ifneeded $(PRJ_PACKAGE_TCLNAME) $(DOTVERSION) \
	    [list load [file join $$dir $(PLATFORM_IDENTIFY) $(PRJLIBNAME)]] > $(OUT_DIR)\pkgIndex.tcl
!else
img-pkgindex:
	@echo package ifneeded $(PRJ_PACKAGE_TCLNAME) $(DOTVERSION) \
	    [list load [file join $$dir $(PRJLIBNAME)]] > $(OUT_DIR)\pkgIndex.tcl
!endif
pkgindex: img-pkgindex

$(PRJ_OBJS): $(TMP_DIR)\init.c
$(TMP_DIR)\init.c:
	nmakehlp -s << ..\..\init.c > $(TMP_DIR)\init.c
@CPACKAGE@       $(CPACKAGE)
%PACKAGE%        $(PACKAGE_NAME)
%PHIMGTYPE%      $(PHIMGTYPE)
%PHIMGTYPE_BETA% $(PHIMGTYPE_BETA)
<<

# Special install targets to install all DLL's into the main img directory
# When a component is build by itself, e.g. png, it will install into the
# directory named after the component. When installed through the "master"
# Img makefile, it installs in the Img directory. The following takes
# care of the details.
!ifdef TKIMG_INSTALL_SUBDIR
IMG_INSTALL_DIR = $(_INSTALLDIR)\$(TKIMG_INSTALL_SUBDIR)
!else
IMG_INSTALL_DIR = $(SCRIPT_INSTALL_DIR)
!endif
install: $(OUT_DIR)\pkgIndex.tcl
	@echo Installing binaries to '$(IMG_INSTALL_DIR)'
	@if not exist "$(IMG_INSTALL_DIR)" mkdir "$(IMG_INSTALL_DIR)"
	@$(CPY) $(PRJLIB) "$(IMG_INSTALL_DIR)" >NUL
	@echo Installing libraries to '$(IMG_INSTALL_DIR)'
	@if exist $(LIBDIR) $(CPY) $(LIBDIR)\*.tcl "$(IMG_INSTALL_DIR)"
!ifdef TKIMG_INSTALL_SUBDIR
	@echo Adding package index to '$(IMG_INSTALL_DIR)\pkgIndex.tcl'
	@type $(OUT_DIR)\pkgIndex.tcl >> "$(IMG_INSTALL_DIR)\pkgIndex.tcl"
!else
	@echo Installing package index in '$(IMG_INSTALL_DIR)'
	@$(CPY) $(OUT_DIR)\pkgIndex.tcl $(IMG_INSTALL_DIR)
!endif
