#!/usr/bin/env bash set -euo pipefail command_name="${1:-build}" requested_arch="${2:-${ALPINE_ARCH:-x86_64}}" repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" alpine_version="${ALPINE_VERSION:-3.22}" repo_name="${ALPINE_REPO_NAME:-local}" build_platform="${ALPINE_BUILD_PLATFORM:-linux/amd64}" validate_arch() { case "$1" in x86_64|aarch64) ;; *) printf 'unsupported Alpine architecture: %s\n' "$1" >&2; return 2 ;; esac } run_for_arch() { local arch="$1" local subcommand="$2" local image_name validate_arch "${arch}" image_name="${ALPINE_APK_BUILDER_IMAGE:-seaweedfs-apk-builder:${arch}}" docker build \ --platform "${build_platform}" \ --build-arg "ALPINE_VERSION=${alpine_version}" \ --build-arg "BUILDER_UID=$(id -u)" \ --build-arg "BUILDER_GID=$(id -g)" \ -f "${repo_root}/scripts/apk/Dockerfile" \ -t "${image_name}" \ "${repo_root}" docker_args=(--rm --platform "${build_platform}") if [[ -t 0 && -t 1 ]]; then docker_args+=(-it) fi docker run "${docker_args[@]}" \ -e "ALPINE_ARCH=${arch}" \ -e "CARCH=${arch}" \ -e "ALPINE_REPO_NAME=${repo_name}" \ -e "PACKAGER=${PACKAGER:-Local Builder }" \ -v "${repo_root}:/work" \ -v "${repo_root}/.cache/abuild:/home/builder/.abuild" \ -v "${repo_root}/.cache/apk-distfiles:/var/cache/distfiles" \ -v "${repo_root}/packages:/home/builder/packages" \ "${image_name}" \ "${subcommand}" } mkdir -p \ "${repo_root}/.cache/abuild" \ "${repo_root}/.cache/apk-distfiles" \ "${repo_root}/packages" case "${command_name}" in build-all) for arch in ${ALPINE_ARCHES:-x86_64 aarch64}; do run_for_arch "${arch}" build done ;; build|checksum|lint|shell) run_for_arch "${requested_arch}" "${command_name}" ;; *) printf 'unknown command: %s\n' "${command_name}" >&2 printf 'usage: %s [build|build-all|checksum|lint|shell] [x86_64|aarch64]\n' "$0" >&2 exit 2 ;; esac