#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/architecture.mk

# tascam_fw / tascam_fpga helper scripts (usx2yloader) must land in /lib/udev/
# so the udev rule's RUN+="tascam_fw" can find them.
UDEVDIR := $(shell pkg-config --variable=udevdir udev)

SUBDIRS_alsa-tools            := as10k1 hda-verb seq/sbiload us428control hdajacksensetest
SUBDIRS_alsa-tools-gui        := envy24control hdajackretask hdspconf hdspmixer rmedigicontrol
SUBDIRS_alsa-firmware-loaders := sb16_csp hdsploader mixartloader pcxhrloader \
                                  sscape_ctl usx2yloader vxloader
SUBDIRS_ld10k1                := ld10k1

SUBDIRS := $(SUBDIRS_alsa-tools) $(SUBDIRS_alsa-tools-gui) \
           $(SUBDIRS_alsa-firmware-loaders) $(SUBDIRS_ld10k1)

DOCS := README README.profiles TODO NEWS output.doc

%:
	dh $@

override_dh_auto_configure:
	set -e; for subdir in $(SUBDIRS); do \
	  echo "Configuring $$subdir"; \
	  dh_auto_configure --sourcedir=$$subdir; \
	done

override_dh_auto_build:
	set -e; for subdir in $(SUBDIRS); do \
	  echo "Building $$subdir"; \
	  dh_auto_build --sourcedir=$$subdir; \
	done

override_dh_auto_install:
	# alsa-tools, alsa-tools-gui, and alsa-firmware-loaders each have a simple
	# 1:many subdir → 1 binary-package mapping, so install straight into the
	# package staging directory; no .install splitting required.
	set -e; for subdir in $(SUBDIRS_alsa-tools); do \
	  dh_auto_install --sourcedir=$$subdir --destdir=debian/alsa-tools; \
	done
	set -e; for subdir in $(SUBDIRS_alsa-tools-gui); do \
	  dh_auto_install --sourcedir=$$subdir --destdir=debian/alsa-tools-gui; \
	done
	# Override hotplugdir so usx2yloader's tascam helper scripts are placed
	# in /lib/udev/ rather than the obsolete /etc/hotplug/usb/.
	set -e; for subdir in $(SUBDIRS_alsa-firmware-loaders); do \
	  dh_auto_install --sourcedir=$$subdir \
	    --destdir=debian/alsa-firmware-loaders -- hotplugdir=$(UDEVDIR); \
	done
	# ld10k1 is split across ld10k1 / liblo10k1-0 / liblo10k1-dev; route
	# through debian/tmp so the existing .install files can distribute files.
	dh_auto_install --sourcedir=ld10k1 --destdir=debian/tmp

override_dh_auto_clean:
	set -e; for subdir in $(SUBDIRS); do \
	  dh_auto_clean --sourcedir=$$subdir; \
	done

override_dh_installdocs:
	dh_installdocs --exclude=Makefile
	# Install per-subdir docs preserving tool identity inside each package's
	# doc directory, matching the historical layout.
	set -e; for pkg in alsa-tools alsa-tools-gui alsa-firmware-loaders; do \
	  case $$pkg in \
	    alsa-tools)            subdirs="$(SUBDIRS_alsa-tools)" ;; \
	    alsa-tools-gui)        subdirs="$(SUBDIRS_alsa-tools-gui)" ;; \
	    alsa-firmware-loaders) subdirs="$(SUBDIRS_alsa-firmware-loaders)" ;; \
	  esac; \
	  for subdir in $$subdirs; do \
	    for doc in $(DOCS); do \
	      if [ -s $$subdir/$$doc ]; then \
	        install -Dm644 $$subdir/$$doc \
	          debian/$$pkg/usr/share/doc/$$pkg/$$subdir/$$doc; \
	      fi; \
	    done; \
	  done; \
	done

override_dh_installexamples:
	dh_installexamples --exclude=Makefile.am --exclude=Makefile.in

execute_after_dh_installexamples:
	chmod 644 debian/ld10k1/usr/share/doc/ld10k1/examples/emu10k1MIDIEffects/pontodo5
	# Strip embedded build paths to ensure reproducible builds.
	sed -i \
	  -e "s,-fdebug-prefix-map=$(CURDIR)=\.,,g" \
	  -e "s,-ffile-prefix-map=$(CURDIR)=\.,,g" \
	  -e "s,abs_.*$(CURDIR).*,,g" \
	  -e "s,$(CURDIR).*missing --run,,g" \
	  -e "s,$(CURDIR),./,g" \
	  debian/ld10k1/usr/share/doc/ld10k1/examples/emu10k1MIDIEffects/Makefile

execute_after_dh_install:
	# lo10k1 reads PIPE_NAME from /etc/default/ld10k1; the shell wrapper must
	# be on PATH while the real ELF is renamed out of the way.
	cd debian/ld10k1/usr/bin && mv -f lo10k1 lo10k1.bin && mv lo10k1.sh lo10k1

override_dh_compress:
	dh_compress -X.asm

override_dh_makeshlibs:
	dh_makeshlibs -- -c4

override_dh_shlibdeps:
	dh_shlibdeps -l debian/liblo10k1-0/usr/lib/$(DEB_HOST_MULTIARCH)
