#!/usr/bin/env bash

cd $(dirname "$0")

EXTRAOPTS=''
FTPARCHIVE='apt-ftparchive'

for ogdist in iphoneos-arm64/rootless; do
	dist=$(echo "${ogdist}" | cut -f2 -d '/')
	arch="iphoneos-arm64"

	echo $dist
        binary=binary-${arch}
        contents=Contents-${arch}
	mkdir -p dists/${dist}
	rm -f dists/${dist}/{Release{,.gpg},InRelease}

	cp -a CydiaIcon*.png dists/${dist}

  for comp in main; do
		echo "pool/$comp/$ogdist"
	    	if [ ! -d pool/${comp}/${ogdist} ]; then
			echo cont
			continue;
		fi
		mkdir -p dists/${dist}/${comp}/${binary}
		rm -f dists/${dist}/${comp}/${binary}/{Packages{,.xz,.zst},Release{,.gpg}}

		$FTPARCHIVE $EXTRAOPTS packages pool/${comp}/${ogdist} > dists/${dist}/${comp}/${binary}/Packages
		xz -c9 dists/${dist}/${comp}/${binary}/Packages > dists/${dist}/${comp}/${binary}/Packages.xz
		zstd -q -c19 dists/${dist}/${comp}/${binary}/Packages > dists/${dist}/${comp}/${binary}/Packages.zst
		
		$FTPARCHIVE $EXTRAOPTS contents pool/${comp}/${ogdist} > dists/${dist}/${comp}/${contents}
		xz -c9 dists/${dist}/${comp}/${contents} > dists/${dist}/${comp}/${contents}.xz
		zstd -q -c19 dists/${dist}/${comp}/${contents} > dists/${dist}/${comp}/${contents}.zst

		$FTPARCHIVE $EXTRAOPTS release -c config/${arch}-basic.conf dists/${dist}/${comp}/${binary} > dists/${dist}/${comp}/${binary}/Release
  done

	$FTPARCHIVE $EXTRAOPTS release -c config/$(echo "${dist}" | cut -f1 -d '/').conf dists/${dist} > dists/${dist}/Release

	gpg -v -abs -u 3ACFE9CC2BB46B35EE59BB0D98EC314AA672FF55 -o dists/${dist}/Release.gpg dists/${dist}/Release

	gpg -v -abs -u 3ACFE9CC2BB46B35EE59BB0D98EC314AA672FF55 --clearsign -o dists/${dist}/InRelease dists/${dist}/Release 
done

