#!/usr/bin/make -f
#
# discimagecreator — the one tool in this PPA that is a real SOURCE build, not a
# repackage. The main dumper builds via meson; the three helper tools (EccEdc,
# DVDAuth, unscrambler) build via their own bundled makefiles. This mirrors
# fedora/discimagecreator's spec (%build / %install), with the spec's %prep
# source fix-ups applied here at build time (see override_dh_auto_configure).
#
# Because it is a genuine compile, we do NOT skip dh_strip/dh_dwz (unlike the
# repackaged prebuilt binaries) — the binaries are stripped normally and a
# -dbgsym package is produced.

# Helper-tool commits — the extracted <name>-<commit>/ dirs the makefiles live
# in. The helpers track master like DIC itself; the same three pins live in both
# specs (scripts/dic-helpers.sh sets them and lint.sh checks they agree), and the
# archives themselves are named by the spec's Source1:/Source2:/Source3: and
# reach us through the .dsc.
ECCEDCCOMMIT    := 427dd5713718f5a4d2f500da0ce457ce1bebd8a2
DVDAUTHCOMMIT   := 708cf70429877d44619260feafa437d65b2994a1
UNSCRAMBLCOMMIT := ffbf48a271e734c8fd34128d76d4c0b6c51f541f

DESTDIR := debian/discimagecreator

%:
	dh $@ --buildsystem=meson

# %prep, in Debian's clothes: DiscImageCreator itself is the .orig tarball, and
# its three sibling projects arrive as upstream's own GitHub archives through the
# .dsc's Debtransform-Files (what Source1:/Source2:/Source3: are to the spec).
# Unpacking them here is what lets this lane build straight from upstream rather
# than from a merged tarball we assemble and host ourselves. Each archive brings
# its own <name>-<ver>/ top directory, which is exactly what the seds below and
# the makefiles expect.
#
# Nothing is downloaded here and nothing may be: the build root has no network.
execute_before_dh_auto_configure:
	tar -xzf EccEdc-$(ECCEDCCOMMIT).tar.gz
	tar -xzf DVDAuth-$(DVDAUTHCOMMIT).tar.gz
	tar -xzf unscrambler-$(UNSCRAMBLCOMMIT).tar.gz

# Port fedora's %prep source fix-ups. These edit files outside debian/, so they
# run at BINARY-build time (dpkg-source has already captured the pristine tree
# for the source package by now); the source package therefore stays clean and
# carries no quilt patches.
override_dh_auto_configure:
	# Data-directory probe path: upstream's CamelCase /usr/share/DiscImageCreator/
	# -> lowercase /usr/share/discimagecreator/ (matches where we install it).
	sed -i \
	    -e 's|/usr/local/share/DiscImageCreator/|/usr/local/share/discimagecreator/|g' \
	    -e 's|/usr/share/DiscImageCreator/|/usr/share/discimagecreator/|g' \
	    DiscImageCreator/get.cpp DiscImageCreator/xml.cpp
	# FIXED UPSTREAM in #331 (merged 2026-07-20, 0a61a018): pointer-sized typedefs
	# are gated on `__SIZEOF_POINTER__ == 8` now, not on __x86_64__, so the sed that
	# widened the gate to arm64 is gone. It existed because asking which ISA this is,
	# when the question is how wide a pointer is, gave arm64 32-bit types for 64-bit
	# pointers ("cast from 'PDISK_PARTITION_INFO' to 'UINT_PTR' loses precision").
	# The guard asserts the BROKEN form is absent rather than that the fixed one is
	# present, so a mere upstream reformat cannot make it cry wolf.
	if grep -q '^#if defined(__x86_64__)$$' DiscImageCreator/_linux/defineForLinux.h; then \
	    echo "ERROR: upstream reverted the LP64 typedef fix (#331); arm64 would get 32-bit types for 64-bit pointers." >&2; \
	    exit 1; \
	fi
	# Upstream looks up zlib and OpenSSL with method: 'system' since 2026-11-16
	# (29886483), which jammy's meson 0.61 cannot do for OpenSSL on Linux ("Dependency
	# openssl not found", OBS log 2026-11-16). Dropping the method gives meson's
	# default lookup, pkg-config first -- what every series used before. The check
	# asserts the system lookup is gone, so a reformatted line cannot slip past.
	sed -i -E "s/,[[:space:]]*method[[:space:]]*:[[:space:]]*'system'//" meson.build
	if grep -q "method[[:space:]]*:[[:space:]]*'system'" meson.build; then \
	    echo "ERROR: meson.build still asks for method: 'system'; extend the sed." >&2; \
	    exit 1; \
	fi
	# All three helper makefiles omit -fPIE; Ubuntu's default-PIE linker then
	# rejects non-PIC relocations. Append it to the first CXXFLAGS/CFLAGS
	# assignment so the implicit rules pick it up (LDFLAGS=-pie is passed in
	# override_dh_auto_build).
	sed -i -e '0,/^CFLAGS\s*:=/{/^CFLAGS\s*:=/s/$$/ -fPIE/}' \
	       -e '0,/^CXXFLAGS\s*:=/{/^CXXFLAGS\s*:=/s/$$/ -fPIE/}' \
	    EccEdc-$(ECCEDCCOMMIT)/EccEdc/makefile \
	    DVDAuth-$(DVDAUTHCOMMIT)/DVDAuth/makefile \
	    unscrambler-$(UNSCRAMBLCOMMIT)/makefile
	dh_auto_configure

# meson builds only the main DiscImageCreator; the helpers build via their own
# makefiles. CXX=g++ forces the compiler (their makefiles default to $(CXX),
# which may be unset); LDFLAGS=-pie pairs with the -fPIE injection above.
override_dh_auto_build:
	dh_auto_build
	$(MAKE) -C EccEdc-$(ECCEDCCOMMIT)/EccEdc    CXX=g++ LDFLAGS=-pie
	$(MAKE) -C DVDAuth-$(DVDAUTHCOMMIT)/DVDAuth CXX=g++ LDFLAGS=-pie
	$(MAKE) -C unscrambler-$(UNSCRAMBLCOMMIT)   CXX=g++ LDFLAGS=-pie

# No meson tests we want to run.
override_dh_auto_test:

# meson installs the dumper to /usr/bin/DiscImageCreator and its data to
# /usr/share/DiscImageCreator/. Reorganize into the libexec + lowercase-data
# layout (parity with fedora): real binaries under /usr/lib/discimagecreator/,
# /usr/bin holds only aliases (created via debian/discimagecreator.links).
execute_after_dh_auto_install:
	install -d $(DESTDIR)/usr/lib/discimagecreator
	# main binary -> libexec under its upstream-original filename; DIC uses
	# readlink(/proc/self/exe) to find ./EccEdc.out etc. next to itself.
	mv $(DESTDIR)/usr/bin/DiscImageCreator \
	   $(DESTDIR)/usr/lib/discimagecreator/DiscImageCreator.out
	install -m0755 EccEdc-$(ECCEDCCOMMIT)/EccEdc/EccEdc.out \
	   $(DESTDIR)/usr/lib/discimagecreator/EccEdc.out
	install -m0755 DVDAuth-$(DVDAUTHCOMMIT)/DVDAuth/DVDAuth.out \
	   $(DESTDIR)/usr/lib/discimagecreator/DVDAuth.out
	install -m0755 unscrambler-$(UNSCRAMBLCOMMIT)/unscrambler.out \
	   $(DESTDIR)/usr/lib/discimagecreator/unscrambler.out
	# data dir CamelCase -> lowercase, plus the extra Release_ANSI runtime files
	# the binary reads (default.dat / driveOffset.txt are meson-installed).
	mv $(DESTDIR)/usr/share/DiscImageCreator \
	   $(DESTDIR)/usr/share/discimagecreator
	install -m0644 Release_ANSI/C2ErrorProtect.txt   $(DESTDIR)/usr/share/discimagecreator/
	install -m0644 Release_ANSI/ReadErrorProtect.txt $(DESTDIR)/usr/share/discimagecreator/
	install -m0644 "Release_ANSI/DVDRawBruteforce - Drive Sheet - Sheet1.tsv" \
	   $(DESTDIR)/usr/share/discimagecreator/
	# udev rule for USB-floppy access (packaging artifact, not in the tarball).
	install -D -m0644 debian/70-discimagecreator-floppy.rules \
	   $(DESTDIR)/usr/lib/udev/rules.d/70-discimagecreator-floppy.rules
