#!/bin/bash
set -e
cd "$(dirname "$0")"
cd ..
echo $PWD
echo "cleaning filesystem and creating initram directories"
rm -rf ./dependencies/initram/
mkdir -p ./outputs/
mkdir -p ./dependencies/
mkdir -p ./dependencies/initram/
mkdir -p ./dependencies/initram/{bin,dev,etc,lib,proc,sbin,sys,tmp,usr}
mkdir -p ./dependencies/initram/usr/{bin,sbin}
mkdir -p ./dependencies/initram/etc/{bin,sbin}
mkdir -p ./dependencies/initram/lib/modules/
echo "installing busybox"
cp ./busybox ./dependencies/initram/bin/
chmod +x ./dependencies/initram/bin/busybox
chmod 4755 ./dependencies/initram/bin/busybox
./dependencies/initram/bin/busybox --install -s "./dependencies/initram/bin/"

cp ./config/init.sh ./dependencies/initram/init
chmod +x ./dependencies/initram/init
#chmod 4755 ./dependencies/initram/bin/init
echo "copying kernel"
cp ./dependencies/linux/arch/x86_64/boot/bzImage ./outputs/bzImage
cd ./dependencies/initram/
find ./bin -type f -name "*" -exec chmod +x {} +

find . -print0 | cpio --null -o --format=newc --owner root:root | zstd -19 > ../../outputs/initramfs.cpio.gz
echo "created initramfs."
