diff --git a/add-openwrt-packages.sh b/add-openwrt-packages.sh index 8a680b4..4a45865 100755 --- a/add-openwrt-packages.sh +++ b/add-openwrt-packages.sh @@ -39,10 +39,45 @@ if [ ! -f ".config" ]; then make defconfig fi +PACKAGE_INDEX="tmp/.config-package.in" +PACKAGE_INFO="tmp/.packageinfo" + +# Ensure package metadata exists for reliable package name lookups. +if [ ! -f "$PACKAGE_INDEX" ] && [ ! -f "$PACKAGE_INFO" ]; then + echo "⚙️ Generating package metadata..." + make defconfig >/dev/null +fi + +is_already_enabled() { + local pkg="$1" + + awk -v package_y="CONFIG_PACKAGE_${pkg}=y" \ + -v package_m="CONFIG_PACKAGE_${pkg}=m" \ + -v default_y="CONFIG_DEFAULT_${pkg}=y" \ + '$0 == package_y || $0 == package_m || $0 == default_y { found=1; exit } + END { exit !found }' .config +} + +package_exists() { + local pkg="$1" + + if [ -f "$PACKAGE_INDEX" ] && + awk -v key="PACKAGE_${pkg}" '$1 == "config" && $2 == key { found=1; exit } + END { exit !found }' "$PACKAGE_INDEX"; then + return 0 + fi + + if [ -f "$PACKAGE_INFO" ] && grep -Fqx "Package: $pkg" "$PACKAGE_INFO"; then + return 0 + fi + + return 1 +} + echo "📦 Reading packages from $PKG_FILE ..." -# Read all non-empty, non-comment lines, split by any whitespace -PACKAGES=$(grep -v '^[[:space:]]*$' "$PKG_FILE" | grep -v '^#' | tr '\n' ' ') +# Read package tokens from file, while allowing inline comments. +PACKAGES=$(awk '{ sub(/#.*/, ""); for (i = 1; i <= NF; i++) print $i }' "$PKG_FILE") ADDED=() MISSING=() @@ -51,13 +86,13 @@ for pkg in $PACKAGES; do CONFIG_NAME="CONFIG_PACKAGE_${pkg}" # Skip if already enabled - if grep -q "^${CONFIG_NAME}=y" .config; then + if is_already_enabled "$pkg"; then echo "✅ Already enabled: $pkg" continue fi - # Check if package exists in package/ or feeds/ - if ! find package feeds -type d -path "*/${pkg}" -mindepth 1 -maxdepth 3 2>/dev/null | grep -q .; then + # Check if package exists in OpenWrt package metadata. + if ! package_exists "$pkg"; then echo "⚠️ Package not found: $pkg" MISSING+=("$pkg") continue