# Manual variables
VERSION := "$(shell dpkg-parsechangelog --file debian/changelog --show-field Version | awk -F: '{print $$NF}' | awk -F~ '{print $$1}')"


# Automatic variables
ARCH    = "$(shell uname -m)"
TARBALL = "firefox-$(VERSION).tar.xz"
URL     = "https://download-installer.cdn.mozilla.net/pub/firefox/releases/$(VERSION)/linux-$(ARCH)"
SHA_URL = "https://download-installer.cdn.mozilla.net/pub/firefox/releases/$(VERSION)/SHA512SUMS"

all:
	true

clean:
	true

distclean:
	rm -rf vendor

install:
	tar \
		--extract \
		--file="vendor/$(TARBALL)" \
		--one-top-level="$(DESTDIR)/usr/lib/firefox" \
		--strip-components=1 \
		--verbose

vendor:
	rm -rf "$@.partial" "$@"
	mkdir "$@.partial"

	curl \
		-o "$@.partial/$(TARBALL)" \
		"$(URL)/en-US/$(TARBALL)"

	# SHA512SUM herunterladen und überprüfen
	curl -s "$(SHA_URL)" | grep "linux-x86_64\/en-US\/$(TARBALL)" | sed 's/linux-x86_64\/en-US\//$@.partial\//'  > "$@.partial/SHA512SUM"
	sha512sum -c "$@.partial/SHA512SUM" || (echo "Checksum verification failed!" && exit 1)

	ls -1 langpacks | while read pkg_lang; do \
		cat "langpacks/$${pkg_lang}" | while read xpi_lang; do \
			curl \
				-o "$@.partial/langpack-$${xpi_lang}@firefox.mozilla.org.xpi" \
				"$(URL)/xpi/$${xpi_lang}.xpi"; \
		done \
	done

	touch "$@.partial"
	mv -T "$@.partial" "$@"

.PHONY: all clean distclean install
