#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.
#export DH_VERBOSE = 1


# See FEATURE AREAS in dpkg-buildflags(1).
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# See ENVIRONMENT in dpkg-buildflags(1).
# Package maintainers to append CFLAGS.
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# Package maintainers to append LDFLAGS.
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

DEB_UPSTREAM_VERSION ?= $(shell dpkg-parsechangelog \
			| sed -rne 's/^Version: ([0-9.]+)[-+].*$$/\1/p')
%:
	dh $@


# dh_make generated override targets.
# This is an example for Cmake (see <https://bugs.debian.org/641051>).
#override_dh_auto_configure:
#	dh_auto_configure -- \
#	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)
override_dh_auto_build:
	mkdir wechat-files
	bsdtar -xOf ""com.tencent.wechat_${DEB_UPSTREAM_VERSION}_${DEB_TARGET_ARCH}.deb"" ./data.tar.xz|xz -cd > wechat-files/data.tar
	tar -C "wechat-files" --no-same-owner -xf ./wechat-files/data.tar ./opt/apps/com.tencent.wechat
	mv wechat-files/opt/apps/com.tencent.wechat/files/ wechat-files/opt/wechat-beta/
	mv wechat-files/opt/apps/com.tencent.wechat/entries/icons wechat-files/opt/wechat-beta/
	for res in 16 32 48 64 128 256; do mv wechat-files/opt/wechat-beta/icons/hicolor/$${res}x$${res}/apps/com.tencent.wechat.png wechat-files/opt/wechat-beta/icons/hicolor/$${res}x$${res}/apps/wechat-universal.png; done
	cp debian/wechat-universal.sh wechat-files/opt/wechat-beta/common.sh
	chmod +x wechat-files/opt/wechat-beta/common.sh
	sed -i "s|usr/lib/flatpak-xdg-utils|usr/libexec/flatpak-xdg-utils|g" wechat-files/opt/wechat-beta/common.sh
	sed -i "s|usr/lib/wechat-universal|opt/wechat-universal|g" wechat-files/opt/wechat-beta/common.sh
	rm -f wechat-files/opt/wechat-beta/libuosdevicea.so
	patchelf --replace-needed libtiff.so.5 libtiff.so.6 wechat-files/opt/wechat-beta/wechat
	patchelf --set-rpath '$$ORIGIN' wechat-files/opt/wechat-beta/libconfService.so
	patchelf --set-rpath '$$ORIGIN' wechat-files/opt/wechat-beta/libilink_network.so
	patchelf --set-rpath '$$ORIGIN' wechat-files/opt/wechat-beta/libvoipChannel.so
	patchelf --set-rpath '$$ORIGIN' wechat-files/opt/wechat-beta/libvoipCodec.so
	patchelf --set-rpath '$$ORIGIN' wechat-files/opt/wechat-beta/libwxtrans.so
	patchelf --set-rpath '$$ORIGIN:$$ORIGIN/../..' wechat-files/opt/wechat-beta/RadiumWMPF/runtime/libilink_network.so

override_dh_dwz:
	dh_dwz -X/opt/wechat-universal/

override_dh_strip:
	dh_strip -X/opt/wechat-universal/
