#!/usr/bin/make -f
# -*- makefile -*-
#

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Propagate hardening flags correctly to Bazel, build up options here
include /usr/share/dpkg/buildflags.mk
BAZEL_BUILD_FLAGS = \
	$(foreach flag,$(CPPFLAGS),--copt=$(flag)) \
	$(foreach flag,$(CXXFLAGS),--cxxopt=$(flag)) \
	$(foreach flag,$(LDFLAGS),--linkopt=$(flag))

#	$(foreach flag,$(CFLAGS),--conlyopt=$(flag))

# Clear XDG_CONFIG_DIRS for qtchooser
unexport XDG_CONFIG_DIRS

TARGETS=dictionary:zip_code_data \
	gui/tool:mozc_tool \
	renderer:init_mozc_renderer \
	renderer:renderer_client \
	renderer:renderer_server \
	renderer:table_layout \
	renderer:window_util \
	server:mozc_server \
	unix/emacs:mozc_emacs_helper \
	unix:icons \
	unix/ibus:ibus_mozc
TARGET_UIM=unix/uim:uim-mozc.so
TARGET_FCITX5=unix/fcitx5:fcitx5-mozc.so

# Extract upstream version (e.g. 3.33.6133)
DEB_VERSION := $(shell dpkg-parsechangelog -S Version)
MOZC_UPSTREAM_VERSION := $(shell printf '%s' '$(DEB_VERSION)' | sed 's/+.*//')

# Override module and repository to disable network access completely
BAZEL_OPTIONS=$(BAZEL_BUILD_FLAGS) \
	      --compilation_mode=opt \
	      --config=oss_linux \
	      --features=-compiler_param_file \
	      --noenable_workspace \
	      --override_module=abseil-cpp=../debian/third_party/abseil-cpp \
	      --override_module=apple_support=../debian/third_party/apple_support \
	      --override_module=buildozer=../debian/third_party/buildozer \
	      --override_module=googletest=../debian/third_party/gtest \
	      --override_module=platforms=../debian/third_party/platforms \
	      --override_module=protobuf=../debian/third_party/protobuf \
	      --override_module=rules_android_ndk=../debian/third_party/rules_android_ndk \
	      --override_module=rules_apple=../debian/third_party/rules_apple \
	      --override_module=rules_cc=/usr/share/bazel/rules/cc \
	      --override_module=rules_license=/usr/share/bazel/rules/license \
	      --override_module=rules_pkg=/usr/share/bazel/rules/pkg \
	      --override_module=rules_proto=/usr/share/bazel/rules/proto \
	      --override_module=rules_swift=../debian/third_party/rules_swift \
	      --override_module=zlib=../debian/third_party/zlib \
	      --override_repository=_main~_repo_rules~ja_usage_dict=third_party/ja_usage_dict \
	      --override_repository=_main~_repo_rules~material_icon_dictionary_png=third_party/material_icon_dictionary_png \
	      --override_repository=_main~_repo_rules~material_icon_dictionary_svg=third_party/material_icon_dictionary_svg \
	      --override_repository=_main~_repo_rules~material_icon_properties_png=third_party/material_icon_properties_png \
	      --override_repository=_main~_repo_rules~material_icon_properties_svg=third_party/material_icon_properties_svg \
	      --override_repository=_main~_repo_rules~material_icon_tool_png=third_party/material_icon_tool_png \
	      --override_repository=_main~_repo_rules~material_icon_tool_svg=third_party/material_icon_tool_svg \
	      --override_repository=_main~_repo_rules~zip_code_jigyosyo=third_party/zip_code_jigyosyo \
	      --override_repository=_main~_repo_rules~zip_code_ken_all=third_party/zip_code_ken_all \
	      --registry="file://$(CURDIR)/debian/empty-bazel-registry" \
	      --repo_env=BAZEL_CXXOPTS=-std=c++20 \
	      --repository_disable_download \
	      --sandbox_add_mount_pair=/usr \
	      --strip=never \
	      --subcommands

# Set default build cache directly for Bazel
export XDG_CACHE_HOME=$(CURDIR)/debian/.build-cache

%:
	dh $@

override_dh_auto_configure:
	# Setup symlinks for bazel-rules-platforms
	cd debian/third_party/platforms ; make

	# Setup debian/third_party/platforms/host/constrains.bzl
	case "$(DEB_HOST_ARCH_CPU)" in \
	  amd64) bazel_cpu=x86_64 ;; \
	  arm64) bazel_cpu=aarch64 ;; \
	  armhf) bazel_cpu=arm ;; \
	  i386) bazel_cpu=x86_32 ;; \
	  loong64) bazel_cpu=loongarch64 ;; \
	  ppc64el) bazel_cpu=ppc ;; \
	  riscv64) bazel_cpu=riscv64 ;; \
	  *) echo "unsupported Debian CPU: $(DEB_HOST_ARCH_CPU)" >&2; exit 1 ;; \
	esac; \
	case "$(DEB_HOST_ARCH_OS)" in \
	  linux) bazel_os=linux ;; \
	  *) echo "unsupported Debian OS: $(DEB_HOST_ARCH_OS)" >&2; exit 1 ;; \
	esac; \
	sed \
	  -e "s/@BAZEL_CPU@/$$bazel_cpu/g" \
	  -e "s/@BAZEL_OS@/$$bazel_os/g" \
	  $(CURDIR)/debian/third_party/platforms/host/constraints.bzl.in > $(CURDIR)/debian/third_party/platforms/host/constraints.bzl

	# Setup third_party/zip_code_jigyosyo, third_party/zip_code_ken_all
	# Keeping upstream .CSV only patch as simple, copy required BUILD.bazel and MODULE.bazel from
	# debian/third_party/zip_code_*
	mkdir -p $(CURDIR)/src/third_party/zip_code_jigyosyo
	mkdir -p $(CURDIR)/src/third_party/zip_code_ken_all
	cp $(CURDIR)/debian/third_party/zip_code_jigyosyo/* $(CURDIR)/src/third_party/zip_code_jigyosyo/
	cp $(CURDIR)/debian/third_party/zip_code_ken_all/* $(CURDIR)/src/third_party/zip_code_ken_all/

# Create REPO.bazel to be recognized as repository
setup_material_icons:
	touch $(CURDIR)/src/third_party/material_icon_dictionary_svg/REPO.bazel
	touch $(CURDIR)/src/third_party/material_icon_dictionary_png/REPO.bazel
	touch $(CURDIR)/src/third_party/material_icon_properties_svg/REPO.bazel
	touch $(CURDIR)/src/third_party/material_icon_properties_png/REPO.bazel
	touch $(CURDIR)/src/third_party/material_icon_tool_svg/REPO.bazel
	touch $(CURDIR)/src/third_party/material_icon_tool_png/REPO.bazel
	# As binary should not be included in patchset, build it from svg here
	rsvg-convert -w 48 -h 48 $(CURDIR)/src/third_party/material_icon_dictionary_svg/file/dictionary.svg \
	  -o src/third_party/material_icon_dictionary_png/file/dictionary.png
	rsvg-convert -w 48 -h 48 $(CURDIR)/src/third_party/material_icon_properties_svg/file/properties.svg \
	  -o src/third_party/material_icon_properties_png/file/properties.png
	rsvg-convert -w 48 -h 48 $(CURDIR)/src/third_party/material_icon_tool_svg/file/tool.svg \
	  -o src/third_party/material_icon_tool_png/file/tool.png

override_dh_auto_build: setup_material_icons
	# Build .mo files
	for pofile in $(CURDIR)/src/unix/fcitx5/po/*.po; do \
	  filename=`basename $$pofile`; \
	  lang=$${filename%.po}; \
	  mofile=$${pofile%.po}.mo; \
	  msgfmt $$pofile -o $$mofile; \
	done;
	msgfmt --xml -d $(CURDIR)/src/unix/fcitx5/po/ \
	  --template $(CURDIR)/src/unix/fcitx5/org.fcitx.Fcitx5.Addon.Mozc.metainfo.xml.in \
	  --output-file $(CURDIR)/src/unix/fcitx5/org.fcitx.Fcitx5.Addon.Mozc.metainfo.xml

	# Show options for debug
	echo $(BAZEL_OPTIONS)
	cd src ; \
	bazel clean --expunge; \
	for d in $(TARGETS); do \
	  bazel build $(BAZEL_OPTIONS) $${d} || exit 1; \
	done; \
	bazel build $(BAZEL_OPTIONS) $(TARGET_UIM) || exit 1; \
	bazel build $(BAZEL_OPTIONS) $(TARGET_FCITX5) || exit 1

	# Embed upstream version for ibus-mozc
	sed -i -e 's/0\.0\.0\.0/$(MOZC_UPSTREAM_VERSION)/g' $(CURDIR)/src/bazel-bin/unix/ibus/mozc.xml

override_dh_auto_test:
	:

override_dh_auto_clean:
	rm -f src/MODULE.bazel.lock
	rm -rf $(CURDIR)/debian/.build-cache
	rm -rf $(CURDIR)/src/bazel-bin
	rm -rf $(CURDIR)/src/bazel-out
	rm -rf $(CURDIR)/src/bazel-src
	rm -rf $(CURDIR)/src/bazel-testlogs
	rm -rf $(CURDIR)/debian/third_party/platforms/BUILD
	rm -rf $(CURDIR)/debian/third_party/platforms/cpu
	rm -rf $(CURDIR)/debian/third_party/platforms/os
	rm -f $(CURDIR)/src/third_party/zip_code_jigyosyo/*.bazel
	rm -f $(CURDIR)/src/third_party/zip_code_ken_all/*.bazel

override_dh_auto_install:
	desktop-file-install --dir=$(CURDIR)/debian/mozc-utils-gui/usr/share/applications \
		$(CURDIR)/debian/setup-mozc.desktop
	desktop-file-install --dir=$(CURDIR)/debian/mozc-utils-gui/usr/share/applications \
		$(CURDIR)/debian/ibus-setup-mozc-jp.desktop

# For debhelper 14 or later, make ELF writable in advance
execute_before_dh_strip-arch:
	chmod +w debian/emacs-mozc-bin/usr/bin/mozc_emacs_helper
	chmod +w debian/fcitx5-mozc/usr/lib/$(DEB_HOST_MULTIARCH)/fcitx5/fcitx5-mozc.so
	chmod +w debian/ibus-mozc/usr/lib/ibus-mozc/ibus-engine-mozc
	chmod +w debian/mozc-server/usr/lib/mozc/libinit_mozc_renderer.so
	chmod +w debian/mozc-server/usr/lib/mozc/librenderer_client.so
	chmod +w debian/mozc-server/usr/lib/mozc/librenderer_server.so
	chmod +w debian/mozc-server/usr/lib/mozc/libtable_layout.so
	chmod +w debian/mozc-server/usr/lib/mozc/libwindow_util.so
	chmod +w debian/mozc-server/usr/lib/mozc/mozc_server
	chmod +w debian/mozc-utils-gui/usr/lib/mozc/mozc_tool
	chmod +w debian/uim-mozc/usr/lib/$(DEB_HOST_MULTIARCH)/uim/plugin/libuim-mozc.so

override_dh_fixperms-arch:
	dh_fixperms
	chmod -x debian/emacs-mozc/usr/share/emacs/site-lisp/emacs-mozc/mozc.el
	chmod -x debian/ibus-mozc/usr/share/ibus/component/mozc.xml

override_dh_fixperms-indep:
	dh_fixperms
	chmod -x debian/mozc-data/usr/share/icons/mozc/*.png

.PHONY: override_dh_installemacsen
override_dh_installemacsen:
	dh_installemacsen --priority=50

override_dh_builddeb:
	if dpkg-vendor --is ubuntu; then \
		sed -i 's/^Recommends/Suggests/' debian/ibus-mozc/DEBIAN/control; \
	fi
	dh_builddeb
