KERNEL_VERSION="6.7.4"
KERNEL_MAJOR="${KERNEL_VERSION%%.*}"
KERNEL_NAME="linux-$KERNEL_VERSION"
KERNEL_URL="https://cdn.kernel.org/pub/linux/kernel/v$KERNEL_MAJOR.x/$KERNEL_NAME.tar.xz"

BUSYBOX_VERSION="1.36.1"
BUSYBOX_URL="https://busybox.net/downloads/busybox-$BUSYBOX_VERSION.tar.bz2"

MUSL_VERSION="1.2.4"
MUSL_URL="https://musl.libc.org/releases/musl-$MUSL_VERSION.tar.gz"

mkdir -p components
cd components

download_kernel() {
	echo "Checking for existing Linux directory..."
	if [ -d linux ]; then
  		echo "Linux directory present, skipping."
		return
	fi

	echo "Downloading Linux $KERNEL_VERSION from $KERNEL_URL"
	curl -L $KERNEL_URL | tar -xJ
	mv $KERNEL_NAME linux
}

download_busybox() {
	echo "Checking for existing Busybox directory..."
	if [ -d busybox ]; then
		echo "Busybox directory present, skipping."
		return
	fi

	echo "Downloading Busybox $BUSYBOX_VERSION from $BUSYBOX_URL"
	curl -L $BUSYBOX_URL | tar -xj
	mv busybox-$BUSYBOX_VERSION busybox
}

download_musl() {
	echo "Checking for existing musl directory..."
	if [ -d musl ]; then
		echo "musl directory present, skipping."
		return
	fi

	echo "Downloading musl $MUSL_VERSION from $MUSL_URL"
	curl -L $MUSL_URL | tar -xz
	mv musl-$MUSL_VERSION musl
}

download_components() {
	download_kernel
	download_busybox
	download_musl
}

download_components
