#!/usr/bin/make -f
################################################################################
# LoongOffice source package rules file
#
# Produces 1 all-in-one package:
#   - loongoffice          (main application with SDK and Chinese help merged)
#
# Build strategy:
#   1. Configure with --enable-epm --with-package-format=deb to let LO's
#      instsetoo_native generate split .deb packages
#   2. In install target, extract ALL EPM-generated .debs into debian/tmp/
#   3. In binary-arch, copy from debian/tmp/ to debian/loongoffice/ after
#      dh_prep, then let remaining dh_* tools handle packaging
################################################################################

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk

SHELL := /bin/bash

CURDIR := $(shell pwd)
SOURCE_TREE := $(CURDIR)
STAMP_DIR := debian/stampdir

# Package name
MAINPKG := loongoffice

# Installation directory (matches build.sh: /opt/loongoffice)
OODIRNAME := loongoffice
OODIR := opt/$(OODIRNAME)

# Version components
OOVER := 25.8
OOFULLVER := 25.8.7.0.0

# Build path
BUILD_PATH := /usr/lib/gcc-loongarch64-linux-gnu:$$PATH

# Architecture (from dpkg-architecture)
DEB_ARCH := $(DEB_HOST_ARCH)

# Map deb architecture to EPM's arch naming
# loong64 in dpkg -> loongarch64 in EPM output
ifeq ($(DEB_ARCH),loong64)
  EPM_ARCH := loongarch64
else
  EPM_ARCH := $(DEB_ARCH)
endif

# Batch print extension (auto-detect latest deb in tarballs/)
BATCHPRINT_DEB := $(lastword $(sort $(wildcard $(SOURCE_TREE)/external/tarballs/loongbatchprint_*_loong64.deb)))

# EPM output directories (generated by LO's instsetoo_native with --enable-epm)
EPM_DEB_DIR := $(SOURCE_TREE)/workdir/installation/LoongOffice/deb/install/LoongOffice_$(OOFULLVER)_Linux_deb/DEBS
EPM_DEB_LANG_DIR := $(SOURCE_TREE)/workdir/installation/LoongOffice_languagepack/deb/install/LoongOffice_$(OOFULLVER)_Linux_deb_langpack_zh-CN/DEBS
EPM_DEB_HELP_DIR := $(SOURCE_TREE)/workdir/installation/LoongOffice_helppack/deb/install/LoongOffice_$(OOFULLVER)_Linux_deb_helppack_zh-CN/DEBS
EPM_DEB_SDK_DIR := $(SOURCE_TREE)/workdir/installation/LoongOffice_SDK/deb/install/LoongOffice_$(OOFULLVER)_Linux_deb_sdk/DEBS

################################################################################
# Build targets
################################################################################

.PHONY: build build-arch build-indep install binary binary-arch binary-indep clean

build: build-arch build-indep
	touch $(STAMP_DIR)/build

build-arch: $(STAMP_DIR)/build-arch
$(STAMP_DIR)/build-arch: $(STAMP_DIR)/prepare
	dh_testdir

	# Configure
	PATH=$(BUILD_PATH) \
	./autogen.sh \
		--prefix=/opt \
		--libdir=/opt \
		--with-vendor='Loongson' \
		--with-lang="zh-CN" \
		--with-help \
		--disable-firebird-sdbc \
		--with-system-postgresql \
		--disable-librelogo \
		--enable-release-build \
		--enable-qt6 \
		--enable-kf6 \
		--enable-qt6-multimedia \
		--enable-gstreamer-1-0 \
		--enable-odk \
		--enable-epm \
		--enable-ext-ofdreader \
		--enable-ext-batchprint \
		--with-package-format=deb \
		--disable-fetch-external

	# Build
	PATH=$(BUILD_PATH) \
	$(MAKE) build

	touch $@

build-indep: $(STAMP_DIR)/build-indep
$(STAMP_DIR)/build-indep: $(STAMP_DIR)/build-arch
	dh_testdir
	touch $@

################################################################################
# Install target
# Extract ALL EPM-generated .deb files into debian/tmp/
################################################################################

install: $(STAMP_DIR)/install
$(STAMP_DIR)/install: $(STAMP_DIR)/build-indep
	dh_testdir
	dh_testroot

	# Clean any previous install
	rm -rf debian/tmp

	# Verify EPM output exists
	if [ ! -d "$(EPM_DEB_DIR)" ]; then \
		echo "ERROR: EPM output directory not found: $(EPM_DEB_DIR)"; \
		echo "Make sure --enable-epm --with-package-format=deb are working correctly"; \
		exit 1; \
	fi

	# Extract ALL main office packages (including en-US, dicts, etc.)
	for deb in $(EPM_DEB_DIR)/*.deb; do \
		echo "Extracting: $$deb"; \
		dpkg-deb -x "$$deb" debian/tmp; \
	done

	# Extract ALL language packages (zh-CN)
	if [ -d "$(EPM_DEB_LANG_DIR)" ]; then \
		for deb in $(EPM_DEB_LANG_DIR)/*.deb; do \
			echo "Extracting: $$deb"; \
			dpkg-deb -x "$$deb" debian/tmp; \
		done; \
	fi

	# Extract zh-CN help pack
	if [ -d "$(EPM_DEB_HELP_DIR)" ]; then \
		for deb in $(EPM_DEB_HELP_DIR)/*.deb; do \
			echo "Extracting: $$deb"; \
			dpkg-deb -x "$$deb" debian/tmp; \
		done; \
	fi

	# Extract SDK package (merged into main)
	if [ -d "$(EPM_DEB_SDK_DIR)" ]; then \
		for deb in $(EPM_DEB_SDK_DIR)/*.deb; do \
			echo "Extracting: $$deb"; \
			dpkg-deb -x "$$deb" debian/tmp; \
		done; \
	fi

	# Extract batch print extension (from make fetch, skip if not enabled or invalid)
	if [ -f "$(BATCHPRINT_DEB)" ] && dpkg-deb -I "$(BATCHPRINT_DEB)" >/dev/null 2>&1; then \
		echo "Extracting: $(BATCHPRINT_DEB)"; \
		dpkg-deb -x "$(BATCHPRINT_DEB)" debian/tmp; \
	else \
		echo "INFO: batch print deb not found, skipping (enable with --enable-ext-batchprint)"; \
	fi

	touch $@

################################################################################
# Binary targets - use debhelper
# After dh_prep clears staging dir, copy files from debian/tmp/
################################################################################

binary: binary-arch

binary-arch: $(STAMP_DIR)/binary-arch
$(STAMP_DIR)/binary-arch: $(STAMP_DIR)/install
	dh_testdir
	dh_testroot
	# Verify debian/tmp/ exists (stamp may be stale from a previous build)
	if [ ! -d debian/tmp ]; then \
		echo "ERROR: debian/tmp/ not found, install target may have been skipped"; \
		echo "Removing stale install stamp and re-running install..."; \
		rm -f $(STAMP_DIR)/install; \
		$(MAKE) $(STAMP_DIR)/install; \
	fi
	# Clean package staging dir only (NOT dh_prep, which also deletes debian/tmp/)
	rm -rf debian/$(MAINPKG)
	rm -f debian/$(MAINPKG).debhelper.log debian/$(MAINPKG).substvars
	mkdir -p debian/$(MAINPKG)
	cp -a debian/tmp/. debian/$(MAINPKG)/
	# Replace /opt/bin/loongoffice with /usr/bin/loongoffice (in PATH)
	rm -f debian/$(MAINPKG)/opt/bin/loongoffice
	rmdir debian/$(MAINPKG)/opt/bin 2>/dev/null || true
	mkdir -p debian/$(MAINPKG)/usr/bin
	ln -sf /opt/loongoffice/program/soffice debian/$(MAINPKG)/usr/bin/loongoffice
	dh_installchangelogs -p$(MAINPKG)
	dh_installdocs -p$(MAINPKG)
	dh_installman -p$(MAINPKG)
	dh_link -p$(MAINPKG)
	dh_fixperms -p$(MAINPKG)
	dh_strip -p$(MAINPKG)
	dh_compress -p$(MAINPKG) -X.py -X.oxt -X.rdb -X.xcu -X.xcs
	dh_installdeb -p$(MAINPKG)
	# Skip dh_shlibdeps: LoongOffice bundles its own .so libraries,
	# dpkg-shlibdeps cannot resolve them and control has no ${shlibs:Depends}
	dh_gencontrol -p$(MAINPKG)
	dh_md5sums -p$(MAINPKG)
	dh_builddeb -p$(MAINPKG)

	touch $@

################################################################################
# Clean target
################################################################################

clean:
	dh_testdir
	rm -rf $(STAMP_DIR)
	rm -rf debian/$(MAINPKG) debian/tmp debian/extract
	rm -rf debian/.debhelper
	rm -f debian/*.debhelper
	dh_clean

################################################################################
# Utility targets
################################################################################

$(STAMP_DIR):
	mkdir -p $(STAMP_DIR)

$(STAMP_DIR)/prepare: | $(STAMP_DIR)
	mkdir -p $(STAMP_DIR)
	chmod 755 autogen.sh
	touch $@

FORCE:
