#!/bin/bash 

# generate packages file and compress it
echo 'Generating Package for main...'
apt-ftparchive --arch amd64 packages pool/main > dists/stable/main/binary-amd64/Packages
gzip -f -k dists/stable/main/binary-amd64/Packages

echo 'Generating Package for contrib...'
apt-ftparchive --arch amd64 packages pool/contrib > dists/stable/contrib/binary-amd64/Packages
gzip -f -k dists/stable/contrib/binary-amd64/Packages

apt-ftparchive --arch armhf packages pool/contrib > dists/stable/contrib/binary-armhf/Packages
gzip -f -k dists/stable/contrib/binary-armhf/Packages

if [ "$1" != '--no-non-free' ]
then
	echo 'Generating Package for non-free...'
	apt-ftparchive --arch amd64 packages pool/non-free > dists/stable/non-free/binary-amd64/Packages
	gzip -f -k dists/stable/non-free/binary-amd64/Packages
fi

# generate contents file and compress it
echo 'Generating Contents for main...'
apt-ftparchive contents pool/main > dists/stable/main/Contents-amd64
gzip -f -k dists/stable/main/Contents-amd64

echo 'Generating Contents for contrib...'
apt-ftparchive contents pool/contrib > dists/stable/contrib/Contents-all
gzip -f -k dists/stable/contrib/Contents-all

#apt-ftparchive contents pool/contrib > dists/stable/contrib/Contents-amd64
#gzip -f -k dists/stable/contrib/Contents-amd64

if [ "$1" != '--no-non-free' ]
then
	echo 'Generating Contents for non-free...'
	apt-ftparchive contents pool/non-free > dists/stable/non-free/Contents-amd64
	gzip -f -k dists/stable/non-free/Contents-amd64
fi

# generate release files
echo 'Generating Release for main...'
apt-ftparchive release dists/stable/main/binary-amd64 > dists/stable/main/binary-amd64/Release
echo 'Generating Release for contrib...'
apt-ftparchive release dists/stable/contrib/binary-amd64 > dists/stable/contrib/binary-amd64/Release

if [ "$1" != '--no-non-free' ]
then
	echo 'Generating Release for non-free...'
	apt-ftparchive release dists/stable/non-free/binary-amd64 > dists/stable/non-free/binary-amd64/Release
fi

# generate release file for repos
echo 'Generating Release for stable...'
apt-ftparchive release -c release.conf dists/stable > dists/stable/Release

# sign repo
echo 'Signing...'
gpg --pinentry-mode=loopback --batch --armor --yes --output dists/stable/Release.gpg --local-user $(id -un) --passphrase-file ~/pass --detach-sign dists/stable/Release
gpg --pinentry-mode=loopback --batch --armor --yes --clearsign --output dists/stable/InRelease --local-user $(id -un) --passphrase-file ~/pass --detach-sign dists/stable/Release

