pkg.create.flatpak:: contrib.fetch $(PKG.gui.flatpak) $(PKG.cli.flatpak) $(FLATHUB_MANIFEST)
pkg.create.flathub:: $(FLATHUB_MANIFEST)
pkg.create.deb:: $(PKG.gui.deb) $(PKG.cli.deb)
pkg.create.rpm:: $(PKG.rpm.stamp)
pkg.create.src.deb:: $(PKG.src.deb.stamp)
pkg.create.tar:: pkg.create $(PKG.cli.tar)

DEBIAN_CHANGELOG_DATE = $(shell date -R)

#
# CLI Tar Package
#
$(PKG.cli.tar): | $(dir $(PKG.cli.tar))
$(PKG.cli.tar): | $(STAGE.out.cli/)
	cd $(STAGE.out.cli/) && $(TAR.exe) cjf $(call fn.ABSOLUTE,$(PKG.cli.tar)) .


$(STAGE.out.cli/):
	-$(MKDIR.exe) -p $@
	$(CP.exe) HandBrakeCLI $(STAGE.out.cli/)
	$(call STAGE.doc,$(STAGE.out.cli/))

#
# RPM binary package rules
#
$(PKG.rpm.src.tar.bz2): $(STAGE.out.rpm.src/)
	$(TAR.exe) cjf $(PKG.rpm.src.tar.bz2) -C $(STAGE.out.rpm.src/) $(PKG.rpm.basename)

$(STAGE.out.rpm.src/): GNUmakefile
#	-$(MKDIR.exe) -p $(STAGE.out.rpm.src/)$(PKG.basename)
#	tar c --exclude build ../* | tar x -C $(STAGE.out.rpm.src/)$(PKG.basename)
	-$(RM.exe) -rf $(STAGE.out.src/)
	make contrib.fetch
	$(MKDIR.exe) -p $@
	$(GIT.exe) -C $(SRC/) archive --format=tar --prefix=$(PKG.rpm.basename)/ $(HB.repo.shorthash) | $(TAR.exe) -xC $@
	$(SRC/)scripts/repo-info.sh $(SRC/) > $@/$(PKG.rpm.basename)/version.txt
	-$(MKDIR.exe) $(STAGE.out.rpm.src/)$(PKG.rpm.basename)/download
	$(CP.exe) $(SRC/)download/*.tar.* $(STAGE.out.rpm.src/)$(PKG.rpm.basename)/download

$(PKG.native.rpm.stamp): $(PKG.rpm.src.tar.bz2)
	-$(MKDIR.exe) -p $(RPM.out/)SOURCES
	-$(MKDIR.exe) -p $(RPM.out/)SPECS
	-$(MKDIR.exe) -p $(RPM.out/)BUILD
	-$(MKDIR.exe) -p $(RPM.out/)RPMS
	-$(MKDIR.exe) -p $(RPM.out/)SRPMS
	-$(MKDIR.exe) -p $(RPMROOT.out/)
	echo "%define name $(HB.name.lower)" > $(RPM.out/)SPECS/ghb.spec
	echo "%define release $(PKG.release)" >> $(RPM.out/)SPECS/ghb.spec
	echo "%define version $(PKG.rpm.hb.version)" >> $(RPM.out/)SPECS/ghb.spec
	cat $(GTK.src/)ghb.spec >> $(RPM.out/)SPECS/ghb.spec
	$(CP.exe) $(PKG.rpm.src.tar.bz2) $(RPM.out/)SOURCES
	rpmbuild --define="_topdir $(RPM.out)" --buildroot $(RPMROOT.out) -ba $(RPM.out/)SPECS/ghb.spec
	$(TOUCH.exe) $(PKG.native.rpm.stamp)

$(PKG.rpm.stamp): | $(dir $(PKG.gui.rpm))
$(PKG.rpm.stamp): $(PKG.native.rpm.stamp)
	$(CP.exe) $(PKG.gui.native.rpm) $(PKG.gui.rpm)
	$(CP.exe) $(PKG.cli.native.rpm) $(PKG.cli.rpm)
	$(TOUCH.exe) $(PKG.rpm.stamp)

#
# Flatpak binary package rules
#
$(PKG.gui.flathub.manifest):
	$(MKDIR.exe) -p $(PKG.out.flatpak/)
	$(SRC/)scripts/create_flatpak_manifest.py -a "$(HB_URL)" -s "$(HB_SHA256)" -t $(PKG.gui.manifest.flatpak) $(foreach m,$(CONTRIBS),-c "$(word 1,$($m.FETCH.url))" -s "$(word 1,$($m.FETCH.sha256))") $(FPRUNTIME) $(PKG.gui.flathub.manifest)

$(PKG.cli.flathub.manifest):
	$(MKDIR.exe) -p $(PKG.out.flatpak/)
	$(SRC/)scripts/create_flatpak_manifest.py -a "$(HB_URL)" -s "$(HB_SHA256)" -t $(PKG.cli.manifest.flatpak) $(foreach m,$(CONTRIBS),-c "$(word 1,$($m.FETCH.url))" -s "$(word 1,$($m.FETCH.sha256))") $(FPRUNTIME) $(PKG.cli.flathub.manifest)

$(PKG.gui.flatpak): GNUmakefile $(PKG.src.tar.bz2)
	$(MKDIR.exe) -p $(STAGE.out.flatpak/)
	$(MKDIR.exe) -p $(PKG.out.flatpak/)
	$(SRC/)scripts/create_flatpak_manifest.py -a "$(abspath $(PKG.src.tar.bz2))" -t $(PKG.gui.manifest.flatpak) $(foreach m,$(CONTRIBS),-c "$(abspath $(CONTRIB.download/)$($m.FETCH.basename))") $(STAGE.out.flatpak/)fr.handbrake.ghb.json
	flatpak-builder --default-branch=$(PKG.flatpak.branch) --disable-cache --force-clean $(PGPSIGN) --repo=$(PKG.repo.flatpak) $(PKG.gui.build.flatpak) $(STAGE.out.flatpak/)fr.handbrake.ghb.json
	flatpak build-bundle $(PKG.repo.flatpak) $(PKG.gui.flatpak) fr.handbrake.ghb $(PKG.flatpak.branch)

$(PKG.cli.flatpak): GNUmakefile $(PKG.src.tar.bz2)
	$(MKDIR.exe) -p $(STAGE.out.flatpak/)
	$(MKDIR.exe) -p $(PKG.out.flatpak/)
	$(SRC/)scripts/create_flatpak_manifest.py -a "$(abspath $(PKG.src.tar.bz2))" -t $(PKG.cli.manifest.flatpak) $(foreach m,$(CONTRIBS),-c "$(abspath $(CONTRIB.download/)$($m.FETCH.basename))") $(STAGE.out.flatpak/)fr.handbrake.HandBrakeCLI.json
	flatpak-builder --default-branch=$(PKG.flatpak.branch) --disable-cache --force-clean $(PGPSIGN) --repo=$(PKG.repo.flatpak) $(PKG.cli.build.flatpak) $(STAGE.out.flatpak/)fr.handbrake.HandBrakeCLI.json
	flatpak build-bundle $(PKG.repo.flatpak) $(PKG.cli.flatpak) fr.handbrake.HandBrakeCLI $(PKG.flatpak.branch)

#
# Debian binary package rules
#
$(PKG.gui.tmp.deb): | $(dir $(PKG.gui.tmp.deb))
$(PKG.gui.tmp.deb): GNUmakefile
	-$(RM.exe) $(SRC/)debian
	ln -s $(PKG.debian) $(SRC/)debian
	fakeroot $(MAKE) -C $(SRC/) -f debian/rules clean
	$(MAKE) BUILDDIR=$(abspath $(BUILD)) CONFIGURE=configure -C $(SRC/) -f debian/rules build
	echo $(PKG.out.abs/)
	fakeroot $(MAKE) FORCEVERSION="-- -v$(HB.debversion)" BUILDDIR=$(abspath $(BUILD)) CONFIGURE=configure PKGDESTDIR=$(PKG.out.abs/ -C $(SRC/) -f debian/rules binary


$(PKG.gui.deb): | $(dir $(PKG.gui.deb))
$(PKG.gui.deb): $(PKG.gui.tmp.deb)
	$(MV.exe) $(PKG.gui.tmp.deb) $(PKG.gui.deb)

$(PKG.cli.deb): | $(dir $(PKG.cli.deb))
$(PKG.cli.deb): $(PKG.gui.tmp.deb)
	$(MV.exe) $(PKG.cli.tmp.deb) $(PKG.cli.deb)

#
# Debian source package rules
#
pkg.push.src.deb:: $(PKG.src.deb.stamp)
	(cd $(STAGE.out.src/)bionic && dput handbrake-git-snapshots $(HB.name.lower)_$(HB.debversion)-zhb-1ppa1~bionic1_source.changes )
	(cd $(STAGE.out.src/)cosmic && dput handbrake-git-snapshots $(HB.name.lower)_$(HB.debversion)-zhb-1ppa1~cosmic1_source.changes )

$(PKG.src.deb.stamp): GNUmakefile
	-$(RM.exe) -rf $(STAGE.out.src/)
	make contrib.fetch
	$(MKDIR.exe) -p $(STAGE.out.src/)bionic
	$(GIT.exe) -C $(SRC/) archive --format=tar --prefix=$(PKG.deb.basename)/ $(HB.repo.shorthash) | $(TAR.exe) -xC $(STAGE.out.src/)bionic/
	$(SRC/)scripts/repo-info.sh $(SRC/) > $(STAGE.out.src/)bionic/$(PKG.deb.basename)/version.txt
	$(CP.exe) -a $(SRC/)download $(STAGE.out.src/)bionic/$(PKG.deb.basename)
	cp -a $(PKG.debian) $(STAGE.out.src/)bionic/$(PKG.deb.basename)
	$(CP.exe) $(STAGE.out.src/)bionic/$(PKG.deb.basename)/debian/control.bionic $(STAGE.out.src/)bionic/$(PKG.deb.basename)/debian/control
	$(CP.exe) $(STAGE.out.src/)bionic/$(PKG.deb.basename)/debian/rules.bionic $(STAGE.out.src/)bionic/$(PKG.deb.basename)/debian/rules
	echo "$(HB.name.lower) (1:$(HB.debversion)-zhb-1ppa1~bionic1) bionic; urgency=low" > $(STAGE.out.src/)bionic/$(PKG.deb.basename)/debian/changelog
	echo "  * Snapshot" >> $(STAGE.out.src/)bionic/$(PKG.deb.basename)/debian/changelog
	echo "    - See commit history at https://github.com/HandBrake/HandBrake/commits/master" >> $(STAGE.out.src/)bionic/$(PKG.deb.basename)/debian/changelog
	echo "" >> $(STAGE.out.src/)bionic/$(PKG.deb.basename)/debian/changelog
	echo " -- John Stebbins <jstebbins.hb@gmail.com>  $(DEBIAN_CHANGELOG_DATE)" >> $(STAGE.out.src/)bionic/$(PKG.deb.basename)/debian/changelog
	$(TAR.exe) czf $(STAGE.out.src/)bionic/$(PKG.src.deb.tar) -C $(STAGE.out.src/)bionic $(PKG.deb.basename)
	(cd $(STAGE.out.src/)bionic/$(PKG.deb.basename) && debuild -S -kjstebbins.hb)
	$(MKDIR.exe) -p $(STAGE.out.src/)cosmic
	$(GIT.exe) -C $(SRC/) archive --format=tar --prefix=$(PKG.deb.basename)/ $(HB.repo.shorthash) | $(TAR.exe) -xC $(STAGE.out.src/)cosmic/
	$(SRC/)scripts/repo-info.sh $(SRC/) > $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/version.txt
	$(CP.exe) -a $(SRC/)download $(STAGE.out.src/)cosmic/$(PKG.deb.basename)
	cp -a $(PKG.debian) $(STAGE.out.src/)cosmic/$(PKG.deb.basename)
	$(CP.exe) $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/debian/control.cosmic $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/debian/control
	$(CP.exe) $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/debian/rules.cosmic $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/debian/rules
	echo "$(HB.name.lower) (1:$(HB.debversion)-zhb-1ppa1~cosmic1) cosmic; urgency=low" > $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/debian/changelog
	echo "  * Snapshot" >> $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/debian/changelog
	echo "    - See commit history at https://github.com/HandBrake/HandBrake/commits/master" >> $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/debian/changelog
	echo "" >> $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/debian/changelog
	echo " -- John Stebbins <jstebbins.hb@gmail.com>  $(DEBIAN_CHANGELOG_DATE)" >> $(STAGE.out.src/)cosmic/$(PKG.deb.basename)/debian/changelog
	$(TAR.exe) czf $(STAGE.out.src/)cosmic/$(PKG.src.deb.tar) -C $(STAGE.out.src/)cosmic $(PKG.deb.basename)
	(cd $(STAGE.out.src/)cosmic/$(PKG.deb.basename) && debuild -S -kjstebbins.hb)
