diff --git a/add-external-repos.sh b/add-external-repos.sh new file mode 100644 index 0000000..19767db --- /dev/null +++ b/add-external-repos.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# Update or clone specified OpenWRT package repositories + +set -e # Exit immediately if any command fails +umask 022 # Set default file permissions (files=644, dirs=755) + +# Define repositories in the format: "git_url branch local_dir" +REPOS=( + "https://github.com/muink/luci-app-netspeedtest.git master package/luci-app-netspeedtest" + "https://github.com/EasyTier/luci-app-easytier.git main package/luci-app-easytier" +) + +for REPO_INFO in "${REPOS[@]}"; do + set -- $REPO_INFO + REPO_URL=$1 + REPO_BRANCH=$2 + LOCAL_DIR=$3 + + echo "----------------------------------------" + echo "Processing $LOCAL_DIR ($REPO_BRANCH)" + echo "----------------------------------------" + + if [ -d "$LOCAL_DIR/.git" ]; then + echo "Repository exists, updating..." + pushd "$LOCAL_DIR" > /dev/null + git fetch origin "$REPO_BRANCH" + git checkout "$REPO_BRANCH" + git pull --ff-only origin "$REPO_BRANCH" + popd > /dev/null + else + echo "Repository not found, cloning..." + git clone --depth 1 --branch "$REPO_BRANCH" --single-branch "$REPO_URL" "$LOCAL_DIR" + fi + + echo "Done: $LOCAL_DIR" + echo +done + +echo "✅ All repositories updated successfully." + diff --git a/add-openwrt-packages.sh b/add-openwrt-packages.sh new file mode 100644 index 0000000..8a680b4 --- /dev/null +++ b/add-openwrt-packages.sh @@ -0,0 +1,86 @@ +#!/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 diff --git a/apply-dahdi-patches.sh b/apply-dahdi-patches.sh new file mode 100644 index 0000000..b7775dc --- /dev/null +++ b/apply-dahdi-patches.sh @@ -0,0 +1,148 @@ +#!/bin/bash +# Apply DAHDI driver patch for OpenWRT 24.10.4 +# Fixes MAX macro naming issues in dahdi-linux modules +# Author: ChatGPT (for Zhe Yuan) + +set -e +umask 022 + +PATCH_DIR="package/feeds/telephony/dahdi-linux/patches" +PATCH_FILE="$PATCH_DIR/300-fix-dahdi-max-attempts.patch" + +echo "========================================" +echo " DAHDI Linux Patch Applier for OpenWRT" +echo "========================================" + +# --- Check if inside OpenWRT source directory --- +if [ ! -f "feeds.conf.default" ] || [ ! -d "package" ]; then + echo "[ERROR] This script must be run inside the OpenWRT root directory." + echo "Please 'cd' to your OpenWRT source root and run again." + exit 1 +fi + +# --- Check if dahdi patch directory exists --- +if [ ! -d "$PATCH_DIR" ]; then + echo "[ERROR] Patch directory not found:" + echo " $PATCH_DIR" + echo + echo "Please make sure telephony feed is installed:" + echo " ./scripts/feeds update telephony" + echo " ./scripts/feeds install -a" + exit 1 +fi + +# --- Create patch file --- +echo "[INFO] Writing DAHDI patch to: $PATCH_FILE" +cat > "$PATCH_FILE" <<'EOF' +--- a/drivers/dahdi/wctdm24xxp/base.c ++++ b/drivers/dahdi/wctdm24xxp/base.c +@@ -1516,16 +1516,16 @@ static int wait_access(struct wctdm *wc, struct wctdm_module *const mod) + { + unsigned char data = 0; + int count = 0; + +- #define MAX 10 /* attempts */ ++ #define MAX_ATTEMPTS 10 /* attempts */ + + /* Wait for indirect access */ +- while (count++ < MAX) { ++ while (count++ < MAX_ATTEMPTS) { + data = wctdm_getreg(wc, mod, I_STATUS); + if (!data) + return 0; + } + +- if (count > (MAX-1)) { ++ if (count > (MAX_ATTEMPTS-1)) { + dev_notice(&wc->vb.pdev->dev, + " ##### Loop error (%02x) #####\n", data); + } + +--- a/drivers/dahdi/opvxa1200/base.c ++++ b/drivers/dahdi/opvxa1200/base.c +@@ -868,12 +868,12 @@ static int wait_access(struct wctdm *wc, int card) + long origjiffies; + int count = 0; + +- #define MAX 6000 /* attempts */ ++ #define MAX_ATTEMPTS 6000 /* attempts */ + + + origjiffies = jiffies; + /* Wait for indirect access */ +- while (count++ < MAX) ++ while (count++ < MAX_ATTEMPTS) + { + data = __wctdm_getreg(wc, card, I_STATUS); + +@@ -882,7 +882,7 @@ static int wait_access(struct wctdm *wc, int card) + + } + +- if(count > (MAX-1)) printk(KERN_NOTICE " ##### Loop error (%02x) #####\n", data); ++ if(count > (MAX_ATTEMPTS-1)) printk(KERN_NOTICE " ##### Loop error (%02x) #####\n", data); + + return 0; + } + +--- a/drivers/dahdi/wcaxx-base.c ++++ b/drivers/dahdi/wcaxx-base.c +@@ -1066,16 +1066,16 @@ static int wait_access(struct wcaxx *wc, struct wcaxx_module *const mod) + unsigned char data = 0; + int count = 0; + +- #define MAX 10 /* attempts */ ++ #define MAX_ATTEMPTS 10 /* attempts */ + + /* Wait for indirect access */ +- while (count++ < MAX) { ++ while (count++ < MAX_ATTEMPTS) { + data = wcaxx_getreg(wc, mod, I_STATUS); + if (!data) + return 0; + } + +- if (count > (MAX-1)) { ++ if (count > (MAX_ATTEMPTS-1)) { + dev_notice(&wc->xb.pdev->dev, + " ##### Loop error (%02x) #####\n", data); + } + +--- a/drivers/dahdi/wctdm.c ++++ b/drivers/dahdi/wctdm.c +@@ -623,11 +623,11 @@ static int wait_access(struct wctdm *wc, int card) + unsigned char data = 0; + int count = 0; + +- #define MAX 6000 /* attempts */ ++ #define MAX_ATTEMPTS 6000 /* attempts */ + + + /* Wait for indirect access */ +- while (count++ < MAX) ++ while (count++ < MAX_ATTEMPTS) + { + data = __wctdm_getreg(wc, card, I_STATUS); + +@@ -636,7 +636,7 @@ static int wait_access(struct wctdm *wc, int card) + + } + +- if(count > (MAX-1)) printk(KERN_NOTICE " ##### Loop error (%02x) #####\n", data); ++ if(count > (MAX_ATTEMPTS-1)) printk(KERN_NOTICE " ##### Loop error (%02x) #####\n", data); + + return 0; + } +EOF + +echo "[SUCCESS] Patch written successfully!" +echo +echo "========================================" +echo "✅ DAHDI patch applied successfully!" +echo " File: $PATCH_FILE" +echo "========================================" +echo +echo "👉 Next step:" +echo " make package/feeds/telephony/dahdi-linux/{clean,prepare} V=s" +echo " make package/feeds/telephony/dahdi-linux/compile V=s" +echo diff --git a/download-config.sh b/download-config.sh new file mode 100644 index 0000000..c68378b --- /dev/null +++ b/download-config.sh @@ -0,0 +1,96 @@ +#!/bin/bash +# Download OpenWRT config.buildinfo, generate .config and run defconfig +# Author: Zhe Yuan + +set -e +umask 022 + +# --- Check if inside OpenWRT source directory --- +if [ ! -f "feeds.conf.default" ] || [ ! -d "package" ]; then + echo "[ERROR] Must run inside OpenWRT root directory." + exit 1 +fi + +# --- Detect Git version --- +VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "") +VERSION="${VERSION#v}" +IS_LATEST=false + +if [ -z "$VERSION" ]; then + BRANCH=$(git rev-parse --abbrev-ref HEAD) + if [[ "$BRANCH" =~ openwrt-([0-9]+\.[0-9]+) ]]; then + PARTIAL_VERSION="${BASH_REMATCH[1]}" + # Find latest patch tag + LATEST_TAG=$(git tag -l "v${PARTIAL_VERSION}.*" | sort -V | tail -n1) + if [ -z "$LATEST_TAG" ]; then + echo "[ERROR] Cannot find latest tag for $PARTIAL_VERSION.*" + exit 1 + fi + VERSION="${LATEST_TAG#v}" + IS_LATEST=true + elif [[ "$BRANCH" == *SNAPSHOT* ]]; then + VERSION="SNAPSHOT" + else + read -rp "Cannot detect version, input manually (e.g., 24.10.4 or SNAPSHOT): " VERSION + fi +fi + +echo "[INFO] OpenWRT version: $VERSION" + +# --- Devices list --- +# Format: Name Target FirmwareDevice +DEVICES=( +"Linksys_MX8500 qualcommax/ipq807x linksys_mx8500" +"Cudy_TR3000_128MB mediatek/filogic cudy_tr3000-v1" +"Cudy_TR3000_256MB mediatek/filogic cudy_tr3000-256mb-v1" +) + +echo "Available devices:" +for i in "${!DEVICES[@]}"; do + NAME=$(echo "${DEVICES[$i]}" | awk '{print $1}') + echo " $i) $NAME" +done + +read -rp "Select device number: " DEV_INDEX +if [[ -z "${DEVICES[$DEV_INDEX]}" ]]; then + echo "[ERROR] Invalid selection." + exit 1 +fi + +# --- Extract device info --- +read -r DEV_NAME DEV_TARGET DEV_FW <<<"${DEVICES[$DEV_INDEX]}" +echo "[INFO] Selected device: $DEV_NAME" + +# --- Construct URLs --- +if [[ "$VERSION" == "SNAPSHOT" ]]; then + CONFIG_URL="https://downloads.openwrt.org/snapshots/targets/$DEV_TARGET/config.buildinfo" + FIRMWARE_URL="https://downloads.openwrt.org/snapshots/targets/$DEV_TARGET/openwrt-SNAPSHOT-$DEV_TARGET-$DEV_FW-squashfs-sysupgrade.bin" +else + CONFIG_URL="https://downloads.openwrt.org/releases/$VERSION/targets/$DEV_TARGET/config.buildinfo" + FIRMWARE_URL="https://downloads.openwrt.org/releases/$VERSION/targets/$DEV_TARGET/openwrt-$VERSION-$DEV_TARGET-$DEV_FW-squashfs-sysupgrade.bin" +fi + +echo "[INFO] Config URL: $CONFIG_URL" +echo "[INFO] Firmware URL: $FIRMWARE_URL" + +# --- Download config.buildinfo --- +echo "[INFO] Downloading config.buildinfo..." +wget -q --show-progress -O ".config" "$CONFIG_URL" +echo "[INFO] Saved as .config" + +# --- Run make defconfig --- +echo "[INFO] Running make defconfig..." +make defconfig >/dev/null +echo "[INFO] defconfig done." + +# --- Check firmware existence --- +if curl --head --silent --fail "$FIRMWARE_URL" >/dev/null; then + echo "[INFO] Firmware exists: $FIRMWARE_URL" +else + echo "[WARN] Firmware not found or URL invalid." +fi + +# --- Print Firmware Selector URL --- +TARGET_ESC=$(echo $DEV_TARGET | sed 's|/|%2F|g') +FS_URL="https://firmware-selector.openwrt.org/?version=$VERSION&target=$TARGET_ESC&id=$DEV_FW" +echo "[INFO] Check Installed Packages at: $FS_URL" diff --git a/prepare-openwrt-env.sh b/prepare-openwrt-env.sh new file mode 100644 index 0000000..bf90d8a --- /dev/null +++ b/prepare-openwrt-env.sh @@ -0,0 +1,112 @@ +#!/bin/bash +# Automatically prepare OpenWRT build dependencies for Debian/Ubuntu +# Supports both x86_64 and arm64 +# Detects Ubuntu 22.04, 24.04, Debian 12/13 and newer +# Author: Zhe Yuan + +set -e +umask 022 + +echo "========================================" +echo " OpenWRT Build Environment Preparation" +echo "========================================" + +# --- Detect architecture --- +ARCH=$(uname -m) +echo "[INFO] Detected architecture: $ARCH" + +# --- Detect OS version --- +if [ -f /etc/os-release ]; then + . /etc/os-release + DISTRO="$ID" + VERSION_ID="${VERSION_ID:-unknown}" +else + echo "[ERROR] Cannot detect system version (no /etc/os-release)" + exit 1 +fi + +echo "[INFO] Detected distribution: $DISTRO $VERSION_ID" + +# --- Determine if Python 3.12+ is installed (no distutils) --- +PY_VER=$(python3 -V 2>/dev/null | awk '{print $2}' | cut -d. -f1,2) +HAS_DISTUTILS=$(apt-cache search python3-distutils 2>/dev/null | grep -c distutils || true) + +# --- Base packages (common to all) --- +COMMON_PACKAGES="build-essential clang flex bison g++ gawk \ +gettext git libssl-dev rsync swig unzip zlib1g-dev file wget" + +# --- Determine specific package set --- +if [[ "$ARCH" == "x86_64" || "$ARCH" == "amd64" ]]; then + echo "[INFO] Preparing environment for x86_64." + + if [[ "$VERSION_ID" == "24.04" || ( "$DISTRO" == "debian" && "${VERSION_ID%%.*}" -ge 13 ) ]]; then + echo "[INFO] Detected new environment (Python 3.12+, no python3-distutils)." + EXTRA_PACKAGES="gcc-multilib g++-multilib libncurses5-dev python3-setuptools" + else + echo "[INFO] Detected older Ubuntu/Debian (with python3-distutils)." + EXTRA_PACKAGES="gcc-multilib g++-multilib libncurses-dev python3-distutils python3-setuptools" + fi + + INSTALL_PACKAGES="$COMMON_PACKAGES $EXTRA_PACKAGES" + +elif [[ "$ARCH" == "aarch64" || "$ARCH" == "arm64" ]]; then + echo "[INFO] Preparing environment for ARM64." + + # Note: arm64 has no native multilib; install cross multilib packages + MULTILIB_PACKAGES=" + gcc-multilib-i686-linux-gnu gcc-multilib-s390x-linux-gnu \ + gcc-multilib-x86-64-linux-gnu gcc-multilib-x86-64-linux-gnux32 \ + g++-multilib-i686-linux-gnu g++-multilib-s390x-linux-gnu \ + g++-multilib-x86-64-linux-gnu g++-multilib-x86-64-linux-gnux32 \ + libc6-dev-i386-amd64-cross libc6-dev-i386-cross libc6-dev-i386-x32-cross" + + if [[ "$VERSION_ID" == "24.04" || ( "$DISTRO" == "debian" && "${VERSION_ID%%.*}" -ge 13 ) ]]; then + EXTRA_PACKAGES="libncurses5-dev python3-setuptools" + else + EXTRA_PACKAGES="libncurses-dev python3-distutils python3-setuptools" + fi + + INSTALL_PACKAGES="$COMMON_PACKAGES $EXTRA_PACKAGES $MULTILIB_PACKAGES golang" + +else + echo "[WARN] Unknown architecture: $ARCH" + echo "[WARN] Attempting to use x86_64 package list." + INSTALL_PACKAGES="$COMMON_PACKAGES gcc-multilib g++-multilib libncurses-dev python3-setuptools" +fi + +# --- Display package list --- +echo +echo "========================================" +echo "📦 Packages to install:" +echo "$INSTALL_PACKAGES" +echo "========================================" +echo + +echo "[INFO] Updating APT repositories..." +sudo apt update -y + +echo "[INFO] Installing packages..." +sudo apt install -y $INSTALL_PACKAGES + +# --- Post installation notice --- +echo +echo "========================================" +echo "✅ OpenWRT build environment setup complete!" +echo "========================================" +echo + +if [[ "$ARCH" == "x86_64" || "$ARCH" == "amd64" ]]; then + echo "👉 [NOTE] Go language is optional on x86_64." + echo " If you plan to compile packages requiring Go (e.g. firewall4, netifd), install it manually:" + echo " sudo apt install golang -y" +else + echo "👉 [INFO] Go language installed for ARM64 cross compilation." +fi + +echo +echo "⚙️ After running 'make menuconfig', set Go toolchain path if required:" +echo " Example: /usr/lib/go-1.18" +echo +echo "👉 Next step:" +echo " ./prepare-openwrt.sh # to clone and set up OpenWRT source" +echo diff --git a/prepare-openwrt.sh b/prepare-openwrt.sh new file mode 100644 index 0000000..2cbdf83 --- /dev/null +++ b/prepare-openwrt.sh @@ -0,0 +1,161 @@ +#!/bin/bash +# Automatically prepare OpenWRT build environment. +# - Clone or update repository +# - Support selecting release/beta/snapshot versions +# - Adjust directory layout automatically if run from openwrt-build-helper +# - Update and install feeds +# - Prepare .config for build + +set -e +umask 022 + +REPO_URL="https://github.com/openwrt/openwrt.git" +DEFAULT_DIR="openwrt" + +echo "========================================" +echo " OpenWRT Build Environment Setup Script" +echo "========================================" + +# --- Function: detect and normalize directory structure --- +normalize_structure() { + local current_dir=$(basename "$(pwd)") + + if [ "$current_dir" == "openwrt-build-helper" ]; then + echo "[INFO] Detected helper directory structure." + + mkdir -p helper + # Move everything except helper into helper/ + for f in * .*; do + [[ "$f" == "." || "$f" == ".." || "$f" == "helper" ]] && continue + mv "$f" helper/ 2>/dev/null || true + done + + cd .. + mv openwrt-build-helper openwrt + cd openwrt + echo "[INFO] Directory renamed to 'openwrt', helper files moved to 'openwrt/helper/'." + + elif [ "$current_dir" == "openwrt" ]; then + echo "[INFO] Running directly inside openwrt build directory." + else + echo "[INFO] Not inside openwrt folder, ensuring 'openwrt' subdirectory exists." + mkdir -p openwrt + cd openwrt + fi +} + +# --- Function: choose version --- +choose_version() { + echo + echo "Select OpenWRT version type:" + echo " 1) Stable release (e.g., 24.10.4)" + echo " 2) Beta (e.g., 24.10-SNAPSHOT or openwrt-24.10)" + echo " 3) Latest snapshot (master branch)" + echo -n "Enter choice [1-3, default=1]: " + read -r choice + + case "$choice" in + 2) + echo -n "Enter beta version (e.g. 24.10 or openwrt-24.10): " + read -r VERSION + SELECTED_TYPE="beta" + ;; + 3) + SELECTED_TYPE="snapshot" + VERSION="master" + ;; + *) + echo -n "Enter stable version number (e.g. 24.10.4, default: latest): " + read -r VERSION + SELECTED_TYPE="stable" + ;; + esac +} + +# --- Start --- +normalize_structure +choose_version + +REPO_DIR="$DEFAULT_DIR" + +# If already inside openwrt repo, use current path +if [ -d ".git" ] && [ -f "feeds.conf.default" ]; then + REPO_DIR="." +fi + +# --- Clone or update repository --- +if [ "$REPO_DIR" == "." ]; then + echo "[INFO] Existing OpenWRT repository detected. Updating..." + git fetch --tags origin +else + if [ -d "$REPO_DIR/.git" ]; then + echo "[INFO] Repository exists, updating..." + pushd "$REPO_DIR" >/dev/null + git fetch --tags origin + git pull --ff-only + popd >/dev/null + else + echo "[INFO] Cloning repository..." + git clone "$REPO_URL" "$REPO_DIR" + fi +fi + +pushd "$REPO_DIR" >/dev/null + +# --- Version selection logic --- +if [ "$SELECTED_TYPE" == "snapshot" ]; then + echo "[INFO] Checking out latest master (snapshot)..." + git checkout master + git pull origin master +elif [ "$SELECTED_TYPE" == "beta" ]; then + # Try both openwrt- and v-SNAPSHOT + if git rev-parse --verify "openwrt-${VERSION}" >/dev/null 2>&1; then + git checkout "openwrt-${VERSION}" + elif git rev-parse --verify "v${VERSION}-SNAPSHOT" >/dev/null 2>&1; then + git checkout "v${VERSION}-SNAPSHOT" + else + echo "[ERROR] Beta branch or tag not found for version $VERSION." + exit 1 + fi +else + # Stable version + if [ -z "$VERSION" ]; then + LATEST_TAG=$(git tag -l "v*" | sort -V | tail -n 1) + echo "[INFO] No version specified, using latest stable tag: $LATEST_TAG" + VERSION="$LATEST_TAG" + else + VERSION="v${VERSION}" + fi + + if git rev-parse --verify "$VERSION" >/dev/null 2>&1; then + git checkout "$VERSION" + else + echo "[ERROR] Version tag $VERSION not found." + exit 1 + fi +fi + +# --- Update feeds --- +echo "[INFO] Updating and installing all feeds..." +./scripts/feeds update -a +./scripts/feeds install -a + +# --- Run defconfig --- +echo "[INFO] Running make defconfig..." +make defconfig + +CURRENT_VERSION=$(git describe --tags 2>/dev/null || git rev-parse --abbrev-ref HEAD) + +popd >/dev/null + +echo +echo "========================================" +echo "✅ OpenWRT source is ready." +echo " Version: $CURRENT_VERSION" +echo "========================================" +echo +echo "👉 Next steps:" +echo " cd openwrt" +echo " make menuconfig" +echo " make -j\$(nproc) download world" +echo diff --git a/update-go-path.sh b/update-go-path.sh new file mode 100644 index 0000000..2cd1b5f --- /dev/null +++ b/update-go-path.sh @@ -0,0 +1,74 @@ +#!/bin/bash +# Automatically detect Go installation and update .config for OpenWRT +# Author: Zhe Yuan + +set -e +umask 022 + +CONFIG_FILE=".config" + +echo "========================================" +echo " Go Path Auto Updater for OpenWRT" +echo "========================================" + +# --- Check for .config file --- +if [ ! -f "$CONFIG_FILE" ]; then + echo "[ERROR] No .config file found in current directory!" + echo "Please run this script inside your OpenWRT build root." + exit 1 +fi + +# --- Detect available Go installations --- +GO_DIRS=(/usr/lib/go-*/) +if [ ${#GO_DIRS[@]} -eq 0 ]; then + echo "[ERROR] No Go installation found under /usr/lib/go-*" + echo "Please install Go using: sudo apt install golang -y" + exit 1 +fi + +# --- Extract version numbers --- +GO_VERSIONS=() +for dir in "${GO_DIRS[@]}"; do + ver=$(basename "$dir" | sed -E 's/^go-([0-9]+\.[0-9]+).*$/\1/') + GO_VERSIONS+=("$ver") +done + +# --- Sort versions and pick the latest --- +IFS=$'\n' sorted=($(sort -V <<<"${GO_VERSIONS[*]}")) +LATEST_VER="${sorted[-1]}" +LATEST_PATH="/usr/lib/go-${LATEST_VER}/" + +echo "[INFO] Found Go versions: ${GO_VERSIONS[*]}" +echo "[INFO] Using latest Go version: $LATEST_VER" +echo "[INFO] Path: $LATEST_PATH" + +# --- Check if multiple versions exist --- +if [ ${#GO_VERSIONS[@]} -gt 1 ]; then + echo "[WARN] Multiple Go versions detected!" + echo "If you prefer to use an older version, modify manually:" + echo " CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT=\"/usr/lib/go-X.XX/\"" + echo +fi + +# --- Update .config --- +if grep -q '^CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT=' "$CONFIG_FILE"; then + echo "[INFO] Updating existing CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT..." + sed -i "s|^CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT=.*|CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT=\"${LATEST_PATH}\"|" "$CONFIG_FILE" +else + echo "[INFO] Adding CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT..." + echo "CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT=\"${LATEST_PATH}\"" >> "$CONFIG_FILE" +fi + +# --- Verify result --- +echo +echo "[SUCCESS] Go path updated in .config:" +grep 'CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT' "$CONFIG_FILE" + +echo +echo "========================================" +echo "✅ Go path update complete!" +echo "========================================" +echo +echo "👉 If you wish to override manually, edit .config and change:" +echo " CONFIG_GOLANG_EXTERNAL_BOOTSTRAP_ROOT=\"/usr/lib/go-X.XX/\"" +echo