#!/bin/bash

CROSS_COMPILER_PREFIX="arm-linux-gnueabi-"
BUSYBOX_URL="https://www.busybox.net/downloads/busybox-1.26.2.tar.bz2"
BUSYBOX_ARCHIVE="$(echo $BUSYBOX_URL | rev | cut -d'/' -f 1 | rev)"
BUSYBOX="$(echo $BUSYBOX_ARCHIVE | sed 's/\.tar.*$//g')"
NCPU=4

function fetch()
{
  wget $BUSYBOX_URL
  tar -xjvf $BUSYBOX_ARCHIVE
}

if [[ ! -d $BUSYBOX ]]; then
  fetch
  pushd $BUSYBOX > /dev/null
  patch -p1 < ../busybox-android.patch
  cp ../busybox-android.config .config
  sed "s/CONFIG_CROSS_COMPILER_PREFIX=\".*\"\$/CONFIG_CROSS_COMPILER_PREFIX=\"$CROSS_COMPILER_PREFIX\"/g" -i .config
  yes '' | make oldconfig
  popd > /dev/null
  rm $BUSYBOX_ARCHIVE
fi

pushd $BUSYBOX > /dev/null
make -j $((NCPU*2))
cp busybox ../
make clean
popd > /dev/null
