#!/bin/bash
#
# download and build busybox
#
# thomas@linuxmuster.net
# 20231202
#

# read build environment
for i in build/conf.d/*; do source "$i" || exit 1; done

# download
ARCHIVE="$CACHE/$BBARCHIVE"
SHA256="$CACHE/$BBSHA256"
if [ ! -s "$ARCHIVE" ]; then
  echo "Downloading $BBARCHIVE ..."
  curl -L -o "$ARCHIVE" "$BBURL/$BBARCHIVE"
  echo "Downloading $BBSHA256 ..."
  curl -L -o "$SHA256" "$BBURL/$BBSHA256"
fi

# check
echo "Checking sha256sum ..."
[ "$(awk '{print $1}' "$SHA256")" = "$(sha256sum "$ARCHIVE" | awk '{print $1}')" ] || exit 1

# extract
if [ -d "$BBSRCDIR" ]; then
  echo "$BBSRCDIR exists, skipping archive extraction."
else
  echo "Extracting $BBARCHIVE ..."
  tar xf "$ARCHIVE" -C "$SRC" || exit 1
  cp "$BBCONFIG" "$BBSRCDIR/.config"
fi

# compile
if [ -s "$BBBIN" ]; then
  echo "Busybox binary exists, skipping build."
else
  echo "Compiling busybox ..."
  cd "$BBSRCDIR"
  make oldconfig || exit 1
  make $J_OPT install || exit 1
fi
