set -e
set -u

OUTDIR=/minimal-linux
KERNEL_REPO=git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
KERNEL_VERSION=v6.1.14
BUSYBOX_VERSION=1_33_1

#ARCH=arm64
#CROSS_COMPILE=aarch64-none-linux-gnu-
ARCH=x86_64
CROSS_COMPILE=

if [ $# -lt 1 ]
then
	echo "Using default directory ${OUTDIR} for output"
else
	OUTDIR=$1
	echo "Using passed directory ${OUTDIR} for output"
fi

mkdir -p ${OUTDIR}

cd "$OUTDIR"
if [ ! -d "${OUTDIR}/linux-stable" ]; then
    #Clone only if the repository does not exist.
	echo "CLONING GIT LINUX STABLE VERSION ${KERNEL_VERSION} IN ${OUTDIR}"
	git clone ${KERNEL_REPO} --depth 1 --single-branch --branch ${KERNEL_VERSION}
fi
if [ ! -e ${OUTDIR}/linux-stable/arch/${ARCH}/boot/Image ]; then
    cd linux-stable
    echo "Checking out version ${KERNEL_VERSION}"
    git checkout ${KERNEL_VERSION}
    # https://unix.stackexchange.com/questions/657882/how-do-i-cross-compile-the-linux-kernel
    #make ARCH="$ARCH" CROSS_COMPILE="$CROSS_COMPILE" menuconfig
    #make ARCH="$ARCH" CROSS_COMPILE="$CROSS_COMPILE" config
    # use default config
    make ARCH="$ARCH" CROSS_COMPILE="$CROSS_COMPILE" defconfig
    make ARCH="$ARCH" CROSS_COMPILE="$CROSS_COMPILE"
fi

echo "Adding the Image in outdir"

cp ${OUTDIR}/linux-stable/arch/x86/boot/bzImage ${OUTDIR}

echo "Creating the staging directory for the root filesystem"
cd "$OUTDIR"
if [ -d "${OUTDIR}/rootfs" ]
then
	echo "Deleting rootfs directory at ${OUTDIR}/rootfs and starting over"
    sudo rm  -rf ${OUTDIR}/rootfs
fi

# TODO: Create necessary base directories
mkdir rootfs/
mkdir rootfs/bin
mkdir rootfs/dev
mkdir rootfs/etc
mkdir rootfs/home
mkdir rootfs/lib
mkdir rootfs/lib/modules
mkdir rootfs/lib64
mkdir rootfs/lib64/modules
mkdir rootfs/proc
mkdir rootfs/sys
mkdir rootfs/sbin
mkdir rootfs/tmp
mkdir rootfs/usr
mkdir rootfs/usr/lib
mkdir rootfs/usr/bin 
mkdir rootfs/usr/sbin
mkdir rootfs/var
mkdir rootfs/var/log 

cd "$OUTDIR"
if [ ! -d "${OUTDIR}/busybox" ]
then
git clone git://busybox.net/busybox.git
    cd busybox
    git checkout ${BUSYBOX_VERSION}
    # TODO:  Configure busybox
    # https://busybox.net/FAQ.html#configure
    make ARCH="$ARCH" CROSS_COMPILE="$CROSS_COMPILE" defconfig
else
    cd busybox
fi

# TODO: Make and install busybox
make ARCH="$ARCH" CROSS_COMPILE="$CROSS_COMPILE"
make ARCH="$ARCH" CROSS_COMPILE="$CROSS_COMPILE" CONFIG_PREFIX="${OUTDIR}/rootfs" install
cd ..
pwd

echo "Library dependencies"
${CROSS_COMPILE}readelf -a ${OUTDIR}/rootfs/bin/busybox | grep "program interpreter"
${CROSS_COMPILE}readelf -a ${OUTDIR}/rootfs/bin/busybox | grep "Shared library"

# TODO: Add library dependencies to rootfs
SYSROOT=$(${CROSS_COMPILE}gcc -print-sysroot)
echo "SYSROOT is : $SYSROOT"
cp -a ${SYSROOT}/lib64/ld-linux-x86-64.so.2 ${OUTDIR}/rootfs/lib
echo "cp 1 result $#"
cp -r ${SYSROOT}/lib64/ ${OUTDIR}/rootfs/lib64
echo "cp 2 result $#"

# TODO: Make device nodes
cd ${OUTDIR}/rootfs
sudo mknod -m 666 dev/null c 1 3
sudo mknod -m 600 dev/console c 5 1
ls -l dev

# TODO: Chown the root directory
cd "$OUTDIR/rootfs"
sudo chown -R root:root *

# TODO: Create initramfs.cpio.gz
find . | cpio -H newc -ov --owner root:root > ${OUTDIR}/initramfs.cpio
cd "$OUTDIR"
gzip -f initramfs.cpio