#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk

# Always naming the Rust target lets `dpkg-buildpackage -a arm64` cross-build from amd64.
RUST_TYPE_amd64 = x86_64-unknown-linux-gnu
RUST_TYPE_arm64 = aarch64-unknown-linux-gnu
RUST_TARGET = $(RUST_TYPE_$(DEB_HOST_ARCH))
RUST_BUILD  = $(RUST_TYPE_$(DEB_BUILD_ARCH))

# ibisOffice needs a newer Rust than Debian's, so the build takes it from rustup, inside the build tree unless RUSTUP_HOME and CARGO_HOME name a rustup to reuse.
RUST_VERSION = 1.99.1
export RUSTUP_HOME ?= $(CURDIR)/debian/.rustup
export CARGO_HOME  ?= $(CURDIR)/debian/.cargo
export RUSTUP_TOOLCHAIN = $(RUST_VERSION)
export PATH := $(CARGO_HOME)/bin:$(PATH)

export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = $(DEB_BUILD_GNU_TYPE)-gcc
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = aarch64-linux-gnu-gcc
export PKG_CONFIG = $(DEB_HOST_GNU_TYPE)-pkg-config
export PKG_CONFIG_ALLOW_CROSS = 1

# The paths of the build machine are not to go into the executables, as upstream's scripts have it.
export RUSTFLAGS = --remap-path-prefix=$(CARGO_HOME)/registry/src=crates.io --remap-path-prefix=$(CARGO_HOME)/git/checkouts=git --remap-path-prefix=$(RUSTUP_HOME)=rustup --remap-path-prefix=$(CURDIR)=.

TARGET_DIR = $(CURDIR)/debian/target
TOOLS      = $(CURDIR)/debian/.tools
WEB        = $(CURDIR)/debian/web
# wasm-bindgen-cli must be the version of the wasm-bindgen crate the lock pins.
WASM_BINDGEN_VERSION = $(shell awk '/^\[\[package\]\]/ { name = "" } /^name = / { name = $$3 } /^version = / && name == "\"wasm-bindgen\"" { gsub( /"/, "", $$3 ); print $$3 }' Cargo.lock)

APP   = debian/ibis-office
WEBPKG = debian/ibis-office-web

%:
	dh $@

override_dh_auto_configure:

# Idempotent: rustup is fetched only when missing, and an installed toolchain is kept.
.PHONY: rust-toolchain
rust-toolchain:
	if [ ! -x "$(CARGO_HOME)/bin/rustup" ]; then \
		curl -sSf -o debian/rustup-init https://static.rust-lang.org/rustup/dist/$(RUST_BUILD)/rustup-init && \
		curl -sSf -o debian/rustup-init.sha256 https://static.rust-lang.org/rustup/dist/$(RUST_BUILD)/rustup-init.sha256 && \
		echo "$$(cut -d' ' -f1 debian/rustup-init.sha256)  debian/rustup-init" | sha256sum -c - && \
		chmod +x debian/rustup-init && \
		debian/rustup-init -y --no-modify-path --default-toolchain none --profile minimal; \
	fi
	rustup toolchain install $(RUST_VERSION) --profile minimal

override_dh_auto_build-arch: rust-toolchain
	test -n "$(RUST_TARGET)" || { echo "unsupported DEB_HOST_ARCH: $(DEB_HOST_ARCH)" >&2; exit 1; }
	rustup target add $(RUST_TARGET)
	cargo build --release --locked -p ibis-office-app --target $(RUST_TARGET) --target-dir $(TARGET_DIR)

# The browser version as upstream's scripts/build-web.sh builds it, into a copy of www/. gpui's scheduler pulls in wasm_thread, which turns on an unstable feature for wasm32, so the compiler is told to allow it.
override_dh_auto_build-indep: rust-toolchain
	rustup target add wasm32-unknown-unknown
	RUSTC_BOOTSTRAP=1 cargo rustc --release --locked -p ibis-office-app --lib --crate-type cdylib --target wasm32-unknown-unknown --target-dir $(TARGET_DIR) -- -C link-arg=-zstack-size=8388608
	test -x $(TOOLS)/bin/wasm-bindgen || cargo install wasm-bindgen-cli --version $(WASM_BINDGEN_VERSION) --locked --root $(TOOLS)
	rm -rf $(WEB)
	cp -r www $(WEB)
	$(TOOLS)/bin/wasm-bindgen $(TARGET_DIR)/wasm32-unknown-unknown/release/ibis_office_app.wasm --out-dir $(WEB)/pkg --target web
	wasm-opt -Oz --enable-bulk-memory --enable-nontrapping-float-to-int $(WEB)/pkg/ibis_office_app_bg.wasm -o $(WEB)/pkg/ibis_office_app_bg.wasm
	stat -c %s $(WEB)/pkg/ibis_office_app_bg.wasm > $(WEB)/pkg/ibis_office_app_bg.wasm.size
	npm ci --no-audit --no-fund
	node_modules/.bin/tsc -p $(WEB)
	rm -f $(WEB)/main.ts $(WEB)/tsconfig.json
	mkdir -p $(WEB)/assets/artborders
	cp -r "$$(cargo metadata --format-version 1 --locked | python3 -c 'import json, os, sys; meta = json.load( sys.stdin ); pkg = next( p for p in meta[ "packages" ] if p[ "name" ] == "gpui-kit-assets" ); print( os.path.join( os.path.dirname( pkg[ "manifest_path" ] ), "assets" ) )')/icons" $(WEB)/assets/icons
	unzip -q -o crates/ibis-office-app/data/artborders/OfficeOpenXML-WordprocessingMLArtBorders.zip -d $(WEB)/assets/artborders
	cp crates/ibis-office-app/data/artborders/ECMA-NOTICE.txt $(WEB)/assets/artborders/

# The tests need the network for their dependencies and a GPU; run them by hand.
override_dh_auto_test:

override_dh_auto_install-arch:
	install -Dm755 $(TARGET_DIR)/$(RUST_TARGET)/release/ibisOffice $(APP)/usr/bin/ibisOffice
	install -Dm644 -t $(APP)/usr/share/applications debian/desktop/*.desktop
	for size in 16 24 32 48 256; do \
		install -Dm644 crates/ibis-office-app/windows/png/ico/ibisOffice_icon_win_$${size}px_transparent.png $(APP)/usr/share/icons/hicolor/$${size}x$${size}/apps/ibisoffice.png; \
	done
	install -Dm644 -t $(APP)/usr/share/ibisOffice/artborders crates/ibis-office-app/data/artborders/OfficeOpenXML-WordprocessingMLArtBorders.zip crates/ibis-office-app/data/artborders/ECMA-NOTICE.txt
	install -Dm644 -t $(APP)/usr/share/doc/ibis-office LICENSE COMMERCIAL-LICENSE.md NOTICE
	install -Dm644 crates/ibis-office-app/linux/license.md $(APP)/usr/share/doc/ibis-office/third-party-licenses.md
	install -Dm644 crates/ibis-office-app/fonts/carlito/OFL.txt $(APP)/usr/share/doc/ibis-office/Carlito-OFL.txt

override_dh_auto_install-indep:
	install -d $(WEBPKG)/usr/share/ibis-office
	cp -r $(WEB) $(WEBPKG)/usr/share/ibis-office/web
	install -Dm644 -t $(WEBPKG)/usr/share/doc/ibis-office-web LICENSE COMMERCIAL-LICENSE.md NOTICE

# The rustup and the tools downloaded stay, so the next build does not fetch them again; `rm -rf debian/.rustup debian/.cargo debian/.tools` drops them.
override_dh_auto_clean:
	rm -rf $(TARGET_DIR) $(WEB) node_modules debian/rustup-init debian/rustup-init.sha256
	dh_clean
