#!/bin/bash # Add specified OpenWRT packages (from a text file) to .config without removing existing ones # Author: Zhe Yuan # Usage: # ./add-openwrt-packages.sh packages.txt # Description: # - Reads package names from a text file (supports both newline or space separation) # - Adds missing CONFIG_PACKAGE_xxx=y entries to .config # - Keeps existing settings intact # - Reports missing packages # - Runs make defconfig at the end to sync dependencies set -e umask 022 # Check argument if [ "$#" -ne 1 ]; then echo "❌ Usage: $0 " exit 1 fi PKG_FILE="$1" # Verify file exists if [ ! -f "$PKG_FILE" ]; then echo "❌ Error: File not found: $PKG_FILE" exit 1 fi # Verify we are in OpenWRT source root if [ ! -d "package" ] || [ ! -f "Makefile" ]; then echo "❌ Error: Please run this script in the OpenWRT source root directory." exit 1 fi # Ensure .config exists if [ ! -f ".config" ]; then echo "⚙️ Generating initial .config..." make defconfig fi 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' ' ') ADDED=() MISSING=() for pkg in $PACKAGES; do CONFIG_NAME="CONFIG_PACKAGE_${pkg}" # Skip if already enabled if grep -q "^${CONFIG_NAME}=y" .config; 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 echo "⚠️ Package not found: $pkg" MISSING+=("$pkg") continue fi # Append package config echo "${CONFIG_NAME}=y" >> .config ADDED+=("$pkg") echo "➕ Added: $pkg" done # Run defconfig to sync dependencies echo echo "⚙️ Running make defconfig..." make defconfig >/dev/null echo echo "✅ Done!" echo "📋 Packages newly added: ${#ADDED[@]}" [ ${#ADDED[@]} -gt 0 ] && echo " → ${ADDED[*]}" if [ ${#MISSING[@]} -gt 0 ]; then echo "⚠️ Packages not found (check feeds or spelling):" for m in "${MISSING[@]}"; do echo " - $m" done fi