#!/bin/bash -x
#
# A script to create a signed Debian repository using standard tools.
# Replaces the complexity of 'aptly' for simpler use cases.
#

set -e

# --- Configuration ---
# !!! IMPORTANT: Set your GPG Key ID here!
# Find it with: gpg --list-secret-keys --keyid-format=long
GPG_KEY_ID="5D7C9BE47836D2FA48F83C2B4A854AEAF7E0E16D"

# Repository details
DIST="sid"
COMP="main"
ARCHS="amd64 riscv64 any" # 'any' is for arch-independent packages ('all')
POOL_DIR="pool"          # Directory containing your .deb files

# --- Script Start ---
echo "--- Starting Debian repository generation ---"

# 1. Clean up old dists directory
echo "Cleaning up old dists directory..."
rm -rf "dists/$DIST"

# 2. Create directory structure
echo "Creating new directory structure..."
for arch in $ARCHS; do
    mkdir -p "dists/$DIST/$COMP/binary-$arch"
done

# 3. Generate Packages files
echo "Scanning .deb files in '$POOL_DIR/'..."
# Use apt-ftparchive to create a master Packages file. It's fast and robust.
apt-ftparchive packages "$POOL_DIR" > "dists/$DIST/Packages.master"

echo "Splitting Packages file by architecture..."
for arch in $ARCHS; do
    dest_dir="dists/$DIST/$COMP/binary-$arch"
    
    # 'any' in our config corresponds to 'all' in the Packages file's Architecture field.
    target_arch=${arch/any/all}

    # Use awk to robustly filter the master file for the correct architecture.
    awk -v arch "$target_arch" 'BEGIN { RS = ""; FS = "\n" } $0 ~ "Architecture: " arch { print $0 "\n" }' \
        "dists/$DIST/Packages.master" > "$dest_dir/Packages"

    # Compress the Packages file
    gzip -c "$dest_dir/Packages" > "$dest_dir/Packages.gz"
    bzip2 -c "$dest_dir/Packages" > "$dest_dir/Packages.bz2"
    
    # Create an empty Release file for this component (not strictly needed but good practice)
    echo "Suite: $DIST" > "$dest_dir/Release"
    echo "Component: $COMP" >> "$dest_dir/Release"
    echo "Architecture: $target_arch" >> "$dest_dir/Release"
done

# 4. Generate Contents files
echo "Generating Contents files..."
apt-ftparchive contents "$POOL_DIR" > "dists/$DIST/Contents.master"
for arch in $ARCHS; do
    target_arch=${arch/any/all}
    # Filter the master contents file for the correct architecture
    grep -E "\s($target_arch|$COMP)\s" "dists/$DIST/Contents.master" | gzip -c > "dists/$DIST/$COMP/Contents-$arch.gz"
done

# 5. Generate the main Release file
echo "Generating main Release file for '$DIST'..."
# This command inspects the subdirectories and builds the main Release file with correct checksums.
apt-ftparchive \
    -o APT::FTPArchive::Release::Origin="Your Repository" \
    -o APT::FTPArchive::Release::Label="Your Repository" \
    -o APT::FTPArchive::Release::Suite="$DIST" \
    -o APT::FTPArchive::Release::Codename="$DIST" \
    -o APT::FTPArchive::Release::Architectures="${ARCHS//any/}" \
    -o APT::FTPArchive::Release::Components="$COMP" \
    release "dists/$DIST" > "dists/$DIST/Release"

# 6. Sign the Release file
echo "Signing the Release file with GPG key $GPG_KEY_ID..."

# Create InRelease (clearsigned file)
gpg --default-key "$GPG_KEY_ID" --clearsign -o "dists/$DIST/InRelease" "dists/$DIST/Release"

# Create Release.gpg (detached signature)
gpg --default-key "$GPG_KEY_ID" -abs -o "dists/$DIST/Release.gpg" "dists/$DIST/Release"

# 7. Clean up temporary master files
rm "dists/$DIST/Packages.master"
rm "dists/$DIST/Contents.master"

echo "--- Repository generation complete! ---"
