#!/usr/bin/make -f
# -*- makefile -*-

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
	dh $@ --exclude=.la

override_dh_install:
	find . -name "*.la" -delete
	cd debian && find . -type f \( -name "FILEFORMAT" -o -name "totp.html" -o -name "README.md" \) -delete \
	&& find . -type d -name google-authenticator -delete
	dh_install -X".la"

get-orig-source:
	# Fetch the latest tag from GitHub
	$(eval TAG := $(shell uscan --no-download --dehs | xmllint --xpath 'string(/dehs/upstream-version)' -))
	@if [ -z "$(TAG)" ]; then \
		echo "Error: Could not detect latest tag"; \
		exit 1; \
	fi

	# Get the release date from GitHub API
	$(eval SHA := $(shell curl -s "https://api.github.com/repos/google/google-authenticator-libpam/git/refs/tags/$(TAG)" | jq -r '.object.sha'))
	$(eval RELEASEDATE := $(shell curl -s "https://api.github.com/repos/google/google-authenticator-libpam/git/commits/$(SHA)" | jq -r '.committer.date' | cut -d'T' -f1 | tr -d '-'))
	@if [ -z "$(RELEASEDATE)" ]; then \
		echo "Error: Could not fetch release date for tag $(TAG)"; \
		exit 1; \
	fi

	# Download the tarball
	wget -O google-authenticator-libpam-$(TAG).tar.gz \
		https://github.com/google/google-authenticator-libpam/archive/refs/tags/$(TAG).tar.gz

	# Rename to Debian orig format with date prefix
	mv google-authenticator-libpam-$(TAG).tar.gz \
		../google-authenticator-libpam_$(RELEASEDATE)-$(TAG).orig.tar.gz

	@echo "Successfully created ../google-authenticator-libpam_$(RELEASEDATE)-$(TAG).orig.tar.gz"

