#!/bin/bash

set -e

# Color codes
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

print_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

print_info() {
    echo -e "${YELLOW}→ $1${NC}"
}

echo "==================================="
echo "EveBox Repository Setup Script"
echo "==================================="
echo ""

# Install required packages
print_info "Checking required packages..."
PACKAGES="reprepro dpkg-dev gnupg apt-utils"
for pkg in $PACKAGES; do
    if ! dpkg -l | grep -q "^ii  $pkg"; then
        print_info "Installing $pkg..."
        sudo apt-get update
        sudo apt-get install -y $pkg
    else
        print_success "$pkg already installed"
    fi
done

# Initialize repository with reprepro
print_info "Initializing repository structure..."
if [ -f "conf/distributions" ]; then
    reprepro export stable
    reprepro export unstable
    print_success "Repository structure initialized"
else
    echo "ERROR: conf/distributions not found. Creating default configuration..."
    mkdir -p conf
    cat > conf/distributions << 'EOF'
Origin: EveBox
Label: EveBox
Codename: stable
Architectures: amd64 arm64
Components: main
Description: EveBox Stable Repository
SignWith: 0F23ABFF8F00056D52F1A82E4A72C4B48B15B19F

Origin: EveBox
Label: EveBox
Codename: unstable
Architectures: amd64 arm64
Components: main
Description: EveBox Unstable Repository
SignWith: 0F23ABFF8F00056D52F1A82E4A72C4B48B15B19F
EOF
    cat > conf/options << 'EOF'
verbose
basedir .
EOF
    reprepro export stable
    reprepro export unstable
    print_success "Repository configuration created and initialized"
fi

# Check GPG key
GPG_KEY_ID="0F23ABFF8F00056D52F1A82E4A72C4B48B15B19F"
print_info "Checking GPG key..."
if ! gpg --list-secret-keys "$GPG_KEY_ID" &> /dev/null; then
    print_info "GPG key $GPG_KEY_ID not found. Please run ./setup-gpg-key.sh to set it up."
else
    print_success "GPG key $GPG_KEY_ID found"

    # Export public key
    gpg --armor --export "$GPG_KEY_ID" > evebox-repo.asc
    print_success "Public key exported to evebox-repo.asc"
fi

# Create Release files
print_info "Creating Release files..."
for dist in stable unstable; do
    if [ -d "dists/$dist" ]; then
        print_success "Release files for $dist created"
    fi
done

# Create a sample sources.list entry (deb822 format)
print_info "Creating sample sources configuration (deb822 format)..."
cat > evebox.sources << 'EOF'
## EveBox APT Repository (deb822 format)
## Place this file in /etc/apt/sources.list.d/

## EveBox Stable Repository
Types: deb deb-src
URIs: https://your-repo-url/
Suites: stable
Components: main
Architectures: amd64 arm64
Signed-By: /usr/share/keyrings/evebox-archive-keyring.gpg

## EveBox Unstable Repository (uncomment to enable)
# Types: deb deb-src
# URIs: https://your-repo-url/
# Suites: unstable
# Components: main
# Architectures: amd64 arm64
# Signed-By: /usr/share/keyrings/evebox-archive-keyring.gpg
EOF
print_success "Sample sources configuration created: evebox.sources (deb822 format)"

# Display summary
echo ""
echo "==================================="
echo "Repository Setup Complete!"
echo "==================================="
echo ""
echo "Next steps:"
echo "1. If you haven't already, run: ./setup-gpg-key.sh"
echo "2. Add packages using: ./add-package.sh <package.deb>"
echo "3. Serve this directory via web server (Apache/Nginx)"
echo ""
echo "Repository structure:"
echo "  stable:   for production-ready packages"
echo "  unstable: for development/testing packages"
echo ""
echo "Supported architectures: amd64, arm64"
echo ""

# List current packages
echo "Current packages in repository:"
echo "--------------------------------"
echo "Stable:"
reprepro list stable 2>/dev/null || echo "  (none)"
echo ""
echo "Unstable:"
reprepro list unstable 2>/dev/null || echo "  (none)"