#!/usr/bin/env bash # SPDX-License-Identifier: (GPL-2.0-only OR MIT) # Copyright (C) 2026 Amlogic, Inc. All rights reserved ################################################################################ # Amlbian WPA Supplicant Configuration Package # # Purpose: Build wpa_supplicant configuration and systemd service package # Provides: wpa_supplicant.conf to /etc, wpa_supplicant.service to /lib/systemd/system ################################################################################ set -e # Get script directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" AMLBIAN_DIR="$(dirname "$(dirname "${SCRIPT_DIR}")")" BASE_DIR="$(dirname "$(dirname "${AMLBIAN_DIR}")")" # Source common functions if [[ -f "${AMLBIAN_DIR}/lib/function-common.sh" ]]; then source "${AMLBIAN_DIR}/lib/function-common.sh" fi # ============================================================================== # Shared Helper Functions # ============================================================================== # ------------------------------------------------------------------------------ # Step 1: Package Information Definition # ------------------------------------------------------------------------------ # Initialize package metadata for WPA Supplicant configuration function _wpa_supplicant_init_package_info() { PKG_MAINTAINER="Armbian Build " PKG_DESCRIPTION="Amlogic wpa_supplicant configuration and service" PKG_NAME="aml-wpa-supplicant-config" PKG_VERSION="2.10" PKG_ARCH="${ARCH:-arm64}" COMPONENT="wpa-supplicant" # Cross compilation settings CROSS_COMPILE="${CROSS_COMPILE:-aarch64-linux-gnu-}" # Source directory for supplicant SUPPLICANT_SRC_DIR="${SCRIPT_DIR}/files" } # ------------------------------------------------------------------------------ # Step 2: Build Directory Structure Setup # ------------------------------------------------------------------------------ # Create layered build directory structure # Parameters: # $1 - base name (e.g. "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}") # Sets: TMP_DIR, BASE_BUILD_DIR, SOURCE_DIR, BUILD_DIR, INSTALL_DIR, PATCH_DIR, # DEB_PACKAGE_DIR, DEBIAN_DIR, ETC_DIR, SYSTEMD_DIR function _wpa_supplicant_create_build_dirs() { local base_name="$1" TMP_DIR="${SRC}/.tmp" mkdir -p "${TMP_DIR}" # 基础构建目录 BASE_BUILD_DIR="${TMP_DIR}/${base_name}" rm -rf ${BASE_BUILD_DIR} # 分层目录 SOURCE_DIR="${BASE_BUILD_DIR}/source" # 源码目录 BUILD_DIR="${BASE_BUILD_DIR}/build" # 编译目录 INSTALL_DIR="${BASE_BUILD_DIR}/install" # 安装目录 PATCH_DIR="${BASE_BUILD_DIR}/patch" # 补丁目录 # 创建所有目录 mkdir -p "${SOURCE_DIR}" mkdir -p "${BUILD_DIR}" mkdir -p "${INSTALL_DIR}" mkdir -p "${PATCH_DIR}" # 在BUILD_DIR下创建deb/包名目录 DEB_PACKAGE_DIR="${BUILD_DIR}/deb/${base_name}" mkdir -p "${DEB_PACKAGE_DIR}" # 安装目录的子目录 DEBIAN_DIR="${INSTALL_DIR}/DEBIAN" ETC_DIR="${INSTALL_DIR}/etc/" SYSTEMD_DIR="${INSTALL_DIR}/lib/systemd/system/" mkdir -p "${DEBIAN_DIR}" mkdir -p "${ETC_DIR}" mkdir -p "${SYSTEMD_DIR}" } # Display build directory info # Parameters: # $1 - log prefix function _wpa_supplicant_display_build_info() { local log_prefix="$1" display_alert "${log_prefix} Source dir: ${SOURCE_DIR}" "info" display_alert "${log_prefix} Build dir: ${BUILD_DIR}" "info" display_alert "${log_prefix} Install dir: ${INSTALL_DIR}" "info" display_alert "${log_prefix} Patch dir: ${PATCH_DIR}" "info" display_alert "${log_prefix} Deb package dir: ${DEB_PACKAGE_DIR}" "info" display_alert "${log_prefix} Etc dir: ${ETC_DIR}" "info" display_alert "${log_prefix} Systemd dir: ${SYSTEMD_DIR}" "info" } # ------------------------------------------------------------------------------ # Step 3: Source File Validation and Copying # ------------------------------------------------------------------------------ # Validate and copy source files # Parameters: # $1 - source directory path # Sets: CONF_SRC, SERVICE_SRC function _wpa_supplicant_validate_and_copy_sources() { local src_dir="$1" # Source file paths WPA_SOURCE_DIR="${src_dir}" CONF_SRC="${WPA_SOURCE_DIR}/wpa_supplicant.conf" SERVICE_SRC="${WPA_SOURCE_DIR}/wpa_supplicant.service" # Check if source files exist if [[ ! -f "${CONF_SRC}" ]]; then display_alert "COMPILE-${COMPONENT} Configuration file not found" "${CONF_SRC}" "error" exit 1 fi if [[ ! -f "${SERVICE_SRC}" ]]; then display_alert "COMPILE-${COMPONENT} Service file not found" "${SERVICE_SRC}" "error" exit 1 fi # Copy source to source directory display_alert "COMPILE-${COMPONENT} Copying source files to source directory" "info" cp -f "${CONF_SRC}" "${SOURCE_DIR}/" cp -f "${SERVICE_SRC}" "${SOURCE_DIR}/" } # ------------------------------------------------------------------------------ # Step 4: Configuration and Service File Installation # ------------------------------------------------------------------------------ # Install configuration and service files function _wpa_supplicant_install_files() { display_alert "COMPILE-${COMPONENT} Installing configuration file to ${ETC_DIR}" "info" install -m 0644 "${CONF_SRC}" "${ETC_DIR}/wpa_supplicant.conf" display_alert "COMPILE-${COMPONENT} Installing systemd service file to ${SYSTEMD_DIR}" "info" install -m 0644 "${SERVICE_SRC}" "${SYSTEMD_DIR}/wpa_supplicant.service" } # ------------------------------------------------------------------------------ # Step 5: Debian Packaging Scripts Creation # ------------------------------------------------------------------------------ # Create Debian control file function _wpa_supplicant_create_control_file() { display_alert "COMPILE-${COMPONENT} Creating control file DEBIAN/control" "info" cat > "${DEBIAN_DIR}/control" << EOF Package: ${PKG_NAME} Version: ${PKG_VERSION} Architecture: ${PKG_ARCH} Maintainer: ${PKG_MAINTAINER} Section: admin Priority: optional Description: ${PKG_DESCRIPTION} This package provides wpa_supplicant configuration file and systemd service. It installs wpa_supplicant.conf to /etc and wpa_supplicant.service to /lib/systemd/system. EOF } # Create post-installation script function _wpa_supplicant_create_postinst() { display_alert "COMPILE-${COMPONENT} Creating post-installation script: DEBIAN/postinst" "info" cat > "${DEBIAN_DIR}/postinst" << EOF #!/bin/bash set -e if [ "\$1" = "configure" ]; then # Reload systemd daemon systemctl daemon-reload 2>/dev/null || true # Enable service if it exists if [ -f "/lib/systemd/system/wpa_supplicant.service" ]; then echo "wpa_supplicant service installed. You can enable it with:" echo " systemctl enable wpa_supplicant.service" fi fi exit 0 EOF chmod 755 "${DEBIAN_DIR}/postinst" } # Create pre-removal script function _wpa_supplicant_create_prerm() { display_alert "COMPILE-${COMPONENT} Creating pre-removal script: DEBIAN/prerm" "info" cat > "${DEBIAN_DIR}/prerm" << EOF #!/bin/bash set -e if [ "\$1" = "remove" ] || [ "\$1" = "deconfigure" ]; then # Stop service if running systemctl stop wpa_supplicant.service 2>/dev/null || true systemctl disable wpa_supplicant.service 2>/dev/null || true fi exit 0 EOF chmod 755 "${DEBIAN_DIR}/prerm" } # ------------------------------------------------------------------------------ # Step 6: Debian Package Building # ------------------------------------------------------------------------------ # Build Debian package from install directory # Parameters: # $1 - log prefix (e.g. "COMPILE-WPA-SUPP") # $2 - output deb filename (without path) # Sets: OUTPUT_DEB function _wpa_supplicant_build_deb_package() { local log_prefix="$1" local output_deb_name="$2" # 将install目录内容拷贝到deb目录 display_alert "${log_prefix} Copying install directory to deb package directory" "info" cp -r "${INSTALL_DIR}/"* "${DEB_PACKAGE_DIR}/" # 基于deb目录打包 display_alert "${log_prefix} Building Debian package" "info" dpkg-deb --build "${DEB_PACKAGE_DIR}" # Move to output directory OUTPUT_DEB="${SRC}/output/${output_deb_name}" mkdir -p "$(dirname "${OUTPUT_DEB}")" mv "${DEB_PACKAGE_DIR}.deb" "${OUTPUT_DEB}" display_alert "${log_prefix} Package built successfully ${output_deb_name}" "info" display_alert "${log_prefix} Build directory preserved ${BASE_BUILD_DIR}" "info" } # ============================================================================== # Main Build Function (Entry Point) # ============================================================================== # Main WPA Supplicant configuration package build function function amlbian_build_wpa_supplicant() { set -e # ---- Step 1: Version and Package Information ---- _wpa_supplicant_init_package_info display_alert "COMPILE-${COMPONENT} Building wpa_supplicant configuration package version ${PKG_VERSION} for ${PKG_ARCH}" "info" # ---- Step 2: Build Directory Setup ---- _wpa_supplicant_create_build_dirs "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}" _wpa_supplicant_display_build_info "COMPILE-${COMPONENT}" # ---- Step 3: Source File Validation and Copying ---- _wpa_supplicant_validate_and_copy_sources "${SUPPLICANT_SRC_DIR}" # ---- Step 4: Configuration and Service File Installation ---- _wpa_supplicant_install_files # ---- Step 5: Debian Packaging Scripts ---- _wpa_supplicant_create_control_file _wpa_supplicant_create_postinst _wpa_supplicant_create_prerm # ---- Step 6: Build Debian Package ---- _wpa_supplicant_build_deb_package "COMPILE-${COMPONENT}" "${PKG_NAME}_${PKG_VERSION}_${PKG_ARCH}.deb" display_alert "COMPILE-${COMPONENT} WPA supplicant configuration package build completed" "success" return 0 } export amlbian_build_wpa_supplicant