#!/bin/sh
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PKG_FILE="$SCRIPT_DIR/extra-packages.txt"

# Defaults
DESKTOP=0
BRANCH="vendor"

# Parse args
for arg in "$@"; do
  case "$arg" in
    --desktop)
      DESKTOP=1
      ;;
    --current)
      BRANCH="current"
      ;;
    --vendor)
      BRANCH="vendor"
      ;;
    *)
      echo "Unknown option: $arg" >&2
      echo "Usage: $0 [--desktop] [--current|--vendor]" >&2
      exit 1
      ;;
  esac
done

# Packages file
if [ ! -f "$PKG_FILE" ]; then
  echo "ERROR: package list not found: $PKG_FILE" >&2
  exit 1
fi

# Read packages: strip comments, empty lines, join to one line
EXTRA_PACKAGES="$(sed \
  -e 's/#.*//' \
  -e '/^[[:space:]]*$/d' \
  "$PKG_FILE" | tr '\n' ' ' | sed 's/[[:space:]]\+/ /g; s/^ //; s/ $//')"

echo "Build configuration:"
echo "  BRANCH=$BRANCH"
echo "  DESKTOP=$DESKTOP"
echo "  EXTRA_PACKAGES=$EXTRA_PACKAGES"
echo

if [ "$DESKTOP" -eq 1 ]; then
  # Desktop build (XFCE)
  ./compile.sh \
    BOARD=napi2 \
    BRANCH="$BRANCH" \
    RELEASE=noble \
    BUILD_MINIMAL=yes \
    BUILD_DESKTOP=yes \
    DESKTOP_ENVIRONMENT=xfce \
    DESKTOP_ENVIRONMENT_CONFIG_NAME=config_base \
    DESKTOP_APPGROUPS_SELECTED=browsers,desktop_tools,multimedia \
    KERNEL_CONFIGURE=no \
    EXTRA_PACKAGES="$EXTRA_PACKAGES"
else
  # Minimal build
  ./compile.sh \
    BOARD=napi2 \
    BRANCH="$BRANCH" \
    RELEASE=noble \
    BUILD_MINIMAL=yes \
    BUILD_DESKTOP=no \
    KERNEL_CONFIGURE=no \
    EXTRA_PACKAGES="$EXTRA_PACKAGES"
fi

