#!/bin/bash
set -euo pipefail

REPO="/var/www/repo"
cd "$REPO"

DIST="stable"
COMP="main"
ARCH="amd64"
PKGDIR="dists/$DIST/$COMP/binary-$ARCH"

mkdir -p "pool/$COMP" "$PKGDIR"

apt-ftparchive packages "pool/$COMP" > "$PKGDIR/Packages"
gzip -9 -k -f "$PKGDIR/Packages"
xz  -9 -k -f "$PKGDIR/Packages"

apt-ftparchive -c apt-ftparchive.conf release "dists/$DIST" > "dists/$DIST/Release"

# Права: владелец rustam, группа www-data, чтобы nginx читал, а rustam писал
chown -R rustam:www-data dists pool
chmod -R 775 dists pool
find dists pool -type d -exec chmod g+s {} \;

echo "OK: repo updated"
