#!/bin/bash
# Sign all packages in build-output

set -e

BUILD_ROOT=$(realpath "${BASH_SOURCE[0]%/*}/..")
source "$BUILD_ROOT/lib/message-helpers.sh"

ARCH=${ARCH:-x86_64}
BUILD_OUTPUT_DIR="$BUILD_ROOT/build-output/$ARCH"
BUILD_DIR="$BUILD_ROOT/build"

print_header "Sign Packages"

# Parse arguments
while [[ $# -gt 0 ]]; do
  case $1 in
  --arch)
    ARCH="$2"
    shift 2
    ;;
  -h | --help)
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  --arch <arch>  Target architecture (x86_64 or aarch64, default: x86_64)"
    echo "  -h, --help     Show this help message"
    echo ""
    echo "This script signs all packages in build-output/"
    exit 0
    ;;
  *)
    print_error "Unknown option: $1"
    exit 1
    ;;
  esac
done

print_info "Target architecture: $ARCH"
print_info "Build output: $BUILD_OUTPUT_DIR"

# Check if build output exists
if [[ ! -d "$BUILD_OUTPUT_DIR" ]]; then
  print_error "Build output directory not found: $BUILD_OUTPUT_DIR"
  print_warning "Run bin/repo build first"
  exit 1
fi

# Check Docker is available
check_docker

# Check GPG credentials are in environment
if [[ -z "$GPG_PRIVATE_KEY" ]]; then
  print_error "GPG_PRIVATE_KEY environment variable not set"
  exit 1
fi

if [[ -z "$GPG_PASSPHRASE" ]]; then
  print_error "GPG_PASSPHRASE environment variable not set"
  exit 1
fi

# Build/update the Docker image (reuse same image as build)
build_docker_image "$BUILD_DIR"

print_info "Running package signing..."

# Ensure output directory is writable by container user
if [ "$(id -u)" -eq 0 ]; then
  chmod -R 777 "$BUILD_OUTPUT_DIR"
else
  sudo chown -R $(id -u):$(id -g) "$BUILD_OUTPUT_DIR" 2>/dev/null || chmod -R 777 "$BUILD_OUTPUT_DIR"
fi

# Run the signing script in Docker
docker run --rm \
  -e ARCH="$ARCH" \
  -e GPG_PRIVATE_KEY="$GPG_PRIVATE_KEY" \
  -e GPG_PASSPHRASE="$GPG_PASSPHRASE" \
  -v "$BUILD_ROOT/build-output:/build-output" \
  -v "$BUILD_DIR:/build:ro" \
  omarchy-aur-builder:latest /build/sign.sh

SIGN_RESULT=$?

# Summary
echo ""
if [[ $SIGN_RESULT -eq 0 ]]; then
  print_success "Package signing completed successfully!"
else
  print_error "Package signing failed"
  exit $SIGN_RESULT
fi
