#!/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