#!/bin/bash
set -e
cd "$(dirname "$0")"
cd ..
echo $PWD
echo "cleaning filesystem and creating initram directories"
rm -rf ./dependencies/initram/
mkdir -p ./dependencies/
mkdir -p ./outputs/
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
./dependencies/initram/bin/busybox --install -s "./dependencies/initram/bin/"
cp ./config/init.sh ./dependencies/initram/init
chmod +x ./dependencies/initram/init
echo "copying kernel"
cp ./dependencies/linux/arch/x86_64/boot/bzImage ./outputs/bzImage.efi
cd ./dependencies/initram
cd "$(dirname "$0")"
cd ..
cd ./dependencies/initram
echo $PWD
find . -type f -name "*" -exec chmod +x {} +
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../../outputs/initramfs.cpio.gz
echo "created initramfs."
