#!/usr/bin/env bash # scripts/00-prepare.sh — host checks, directory layout, source download. source "$(dirname "${BASH_SOURCE[0]}")/lib/common.sh" HOST_TOOLS=(bash ld bison gawk gcc g++ grep gzip m4 make patch perl python3 sed tar makeinfo xz find) check_host() { step "Host requirements" local missing=() for cmd in "${HOST_TOOLS[@]}"; do command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd") done # need at least one downloader if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then missing+=("curl-or-wget") fi if ((${#missing[@]})); then warn "Missing host build tools: ${missing[*]}" cat <= 9 )) || warn "GCC $gccver is old; >= 9 recommended" kver=$(uname -r) log "host kernel: $kver, gcc: $(gcc -dumpversion)" # disk space local avail_gb avail_gb=$(df -BG --output=avail "$AEROS_TOP" | tail -1 | tr -dc '0-9') (( avail_gb >= 30 )) || warn "only ${avail_gb}G free under $AEROS_TOP; >= 30G recommended" ok "host looks usable (${avail_gb}G free)" } make_layout() { step "Directory layout" mkdir -pv "$AEROS_ROOT" "$AEROS_SOURCES" "$AEROS_OUT" "$AEROS_LOGDIR" mkdir -pv "$AEROS_ROOT"/{etc,var,tools} "$AEROS_ROOT"/usr/{bin,lib,sbin} for d in bin lib sbin; do [[ -e "$AEROS_ROOT/$d" ]] || ln -sv "usr/$d" "$AEROS_ROOT/$d" done case "$AEROS_ARCH" in x86_64) mkdir -pv "$AEROS_ROOT/usr/lib64" [[ -e "$AEROS_ROOT/lib64" ]] || ln -sv usr/lib64 "$AEROS_ROOT/lib64" ;; esac ok "layout ready at $AEROS_ROOT" } # Build a list of mirror candidates for a URL (handles slow/blocked hosts). mirror_candidates() { local url="$1" # GNU: ftp.gnu.org is often slow/blocked; ftpmirror picks a fast mirror. case "$url" in https://ftp.gnu.org/gnu/*) echo "${url/https:\/\/ftp.gnu.org\/gnu\//https://ftpmirror.gnu.org/}" echo "${url/https:\/\/ftp.gnu.org\/gnu\//https://mirrors.kernel.org/gnu/}" ;; esac echo "$url" # original tried last } # download_one : try mirrors with curl, then wget; hard timeouts # (--speed-limit/--speed-time aborts a stalled transfer instead of hanging). download_one() { local url="$1" dest="$2" cand while read -r cand; do [[ -n "$cand" ]] || continue if command -v curl >/dev/null 2>&1; then if curl -fL --retry 2 --retry-delay 2 --connect-timeout 15 \ --max-time 900 --speed-time 30 --speed-limit 1024 \ -o "$dest" "$cand"; then return 0; fi fi if command -v wget >/dev/null 2>&1; then if wget -q --tries=2 --connect-timeout=15 --read-timeout=30 \ -O "$dest" "$cand"; then return 0; fi fi warn " mirror failed: $cand" done < <(mirror_candidates "$url") return 1 } fetch_sources() { step "Fetch upstream sources" mkdir -p "$AEROS_SOURCES" command -v curl >/dev/null 2>&1 || command -v wget >/dev/null 2>&1 \ || die "need curl or wget to fetch sources" local fails=0 for name in "${!PKG[@]}"; do [[ "${PKG[$name]}" == *"|http"* ]] || continue # skip note entries local url tb dest url="$(pkg_url "$name")" tb="$(pkg_tarball "$name")" dest="$AEROS_SOURCES/$tb" if [[ -s "$dest" ]]; then ok "have $tb"; continue; fi log "download $name -> $tb" if download_one "$url" "$dest.part"; then mv "$dest.part" "$dest" else warn "FAILED: $name ($url)"; rm -f "$dest.part"; ((fails++)) fi done # optional checksum verification if [[ -f "$AEROS_TOP/config/checksums.sha256" ]]; then log "verifying checksums" ( cd "$AEROS_SOURCES" && sha256sum -c "$AEROS_TOP/config/checksums.sha256" ) \ || die "checksum verification failed" else warn "no config/checksums.sha256 — skipping integrity check" warn "generate one with: (cd sources && sha256sum * > ../config/checksums.sha256)" fi (( fails == 0 )) || die "$fails source(s) failed to download" ok "all sources present in $AEROS_SOURCES" } main() { case "${1:-all}" in check) check_host ;; fetch) fetch_sources ;; all) check_host; make_layout; fetch_sources ;; *) die "usage: 00-prepare.sh [check|fetch|all]" ;; esac } main "$@"