#!/bin/bash
umask 022

set -e
set -o xtrace

EXTRAOPTS='--db=/usr/local/repocache.db -o APT::FTPArchive::AlwaysStat=1'
GPG_KEY="F3594A65343002C55E35FB1E9E7E89B50CCF8DAA"

user="$(whoami)"
uid="$(id -u ${user})"
gid="$(id -g ${user})"

script_full_path=$(dirname "$0")
cd "$script_full_path" || exit 1

# make a temp file to unlock gpg
rm -f fr.txt frtroll.txt
echo "fr" > fr.txt
gpg -abs -u $GPG_KEY --clearsign -o frtroll.txt fr.txt
rm -f fr.txt frtroll.txt

for ogdist in iphoneos-arm64/{18,19,20}00 appletvos-arm64-rootless/{18..20}00; do
    if [[ "${ogdist}" == "iphoneos-arm64-rootless"* ]]; then
	arch=iphoneos-arm64
        codename=iphoneos-arm64
        dist=$(echo "${ogdist}" | cut -f2 -d '/')
    elif [[ "${ogdist}" == "appletvos-arm64-rootless"* ]]; then
	arch=appletvos-arm64-rootless
        codename=iphoneos-arm64
        dist=$(echo "${ogdist}" | cut -f2 -d '/')
    elif [[ "${ogdist}" == "iphoneos-arm"* ]]; then
        arch=iphoneos-arm
        codename=iphoneos-arm64
        dist="${ogdist}"
    elif [[ "${ogdist}" == "appletvos-arm64"* ]]; then
        arch=appletvos-arm64
        codename=appletvos-arm64
        dist="${ogdist}"
    fi

    binary=binary-${arch}
    contents=Contents-${arch}
    mkdir -p dists/${dist}

    rm -f dists/${dist}/{Release{,.gpg},InRelease}
    cp CydiaIcon*.png dists/${dist}
    #cp -a sileo-featured.json dists/${dist}

	for comp in main testing; do
		echo "[$comp/$dist] Starting build..."
		mkdir -p dists/${dist}/${comp}/${binary}
		rm -f dists/${dist}/${comp}/${binary}/{Packages{,.xz,.zst},Release{,.gpg}}

		sudo apt-ftparchive $EXTRAOPTS packages pool/${comp}/${dist} > \
			dists/${dist}/${comp}/${binary}/Packages 2>/dev/null
		sudo xz -c9 dists/${dist}/${comp}/${binary}/Packages > dists/${dist}/${comp}/${binary}/Packages.xz
		sudo zstd -q -c19 dists/${dist}/${comp}/${binary}/Packages > dists/${dist}/${comp}/${binary}/Packages.zst

		sudo apt-ftparchive $EXTRAOPTS contents pool/${comp}/${dist} > \
			dists/${dist}/${comp}/${contents}
		sudo xz -c9 dists/${dist}/${comp}/${contents} > dists/${dist}/${comp}/${contents}.xz
		sudo zstd -q -c19 dists/${dist}/${comp}/${contents} > dists/${dist}/${comp}/${contents}.zst

		sudo apt-ftparchive $EXTRAOPTS \
			-o APT::FTPArchive::Release::Origin="palera1n strap" \
			-o APT::FTPArchive::Release::Label="palera1n strap" \
			-o APT::FTPArchive::Release::Suite="stable" \
			-o APT::FTPArchive::Release::Version="1.0" \
			-o APT::FTPArchive::Release::Codename="${codename}" \
			-o APT::FTPArchive::Release::Architectures="${arch}" \
			-o APT::FTPArchive::Release::Components="main" \
			-o APT::FTPArchive::Release::Description="Procursus packages for palera1n" \
			release dists/${dist}/${comp}/${binary} > dists/${dist}/${comp}/${binary}/Release 2>/dev/null
    done

    sudo apt-ftparchive $EXTRAOPTS \
		-o APT::FTPArchive::Release::Origin="palera1n strap" \
		-o APT::FTPArchive::Release::Label="palera1n strap" \
		-o APT::FTPArchive::Release::Suite="stable" \
		-o APT::FTPArchive::Release::Version="1.0" \
		-o APT::FTPArchive::Release::Codename="${codename}" \
		-o APT::FTPArchive::Release::Architectures="${arch}" \
		-o APT::FTPArchive::Release::Components="main" \
		-o APT::FTPArchive::Release::Description="Procursus packages for palera1n" \
        release dists/${dist} > dists/${dist}/Release 2>/dev/null

	sudo chown -R ${uid}:${gid} dists/ 

    gpg -abs -u $GPG_KEY -o dists/${dist}/Release.gpg dists/${dist}/Release
    gpg -abs -u $GPG_KEY --clearsign -o dists/${dist}/InRelease dists/${dist}/Release
done
