#!/bin/sh

# create_deb_repo.sh
# 2023-12-17
# by Gernot WALZL

# This script creates a repository of packages for Debian.
# The folder "pool" is expected to be in the current working directory.
# https://wiki.debian.org/DebianRepository/Format

# As specified in /etc/os-release
VERSION_CODENAME=${VERSION_CODENAME:-'bookworm'}
ARCHS=${ARCHS:-'all amd64 arm64 i386'}
SIGN=${SIGN:-'yes'}

# Create Packages index
for ARCH in $ARCHS; do
  mkdir -p "dists/$VERSION_CODENAME/main/binary-$ARCH"
  dpkg-scanpackages -a "$ARCH" pool > "dists/$VERSION_CODENAME/main/binary-$ARCH/Packages"
  gzip -fkn9 "dists/$VERSION_CODENAME/main/binary-$ARCH/Packages"
done

# Create Release file
cat > "apt-release-$VERSION_CODENAME.conf" <<EOF
APT {
  FTPArchive {
    Release {
      Origin "Debian";
      Label "Debian";
      Suite "stable";
      Codename "$VERSION_CODENAME";
      Architectures "$ARCHS";
      Components "main";
    };
  };
};
EOF
rm "dists/$VERSION_CODENAME/Release"
RELEASE=$(apt-ftparchive -c "apt-release-$VERSION_CODENAME.conf" release "dists/$VERSION_CODENAME")
echo "$RELEASE" > "dists/$VERSION_CODENAME/Release"

# Sign Release file
if [ "$SIGN" = "yes" ]; then
  cd "dists/$VERSION_CODENAME"
  gpg --digest-algo SHA256 --clearsign -o InRelease Release
  gpg --digest-algo SHA256 -abs -o Release.gpg Release
fi