#!/bin/bash

set -e

REPO_DIR="/var/www/repo"
POOL_DIR="$REPO_DIR/pool/main/o/oxinos-studio"
DIST_DIR="$REPO_DIR/dists/stable/main/binary-amd64"

echo "📦 Moving new .deb files..."

# Move any .deb in current dir to pool
mv *.deb $POOL_DIR/ 2>/dev/null || echo "No new .deb files found"

echo "🧠 Regenerating Packages..."

mkdir -p $DIST_DIR

cd $REPO_DIR

dpkg-scanpackages pool /dev/null > $DIST_DIR/Packages
gzip -kf $DIST_DIR/Packages

echo "🔄 Reloading Nginx..."
systemctl reload nginx

echo "✅ Repo updated successfully!"
