#!/bin/bash
DIR=`pwd`
export TEMPLATECONF=$DIR/meta-renesas/meta-rzv2l/docs/template/conf/
export MACHINE=smarc-rzv2l
# Go to the directory that you have downloaded all of the above files into... then:
mkdir ./archive
mv RTK* oss* r11* ./archive
mv nodejs_patches*gz ./archive
mv meta-ei.zip ./archive
cd ./archive
tar xzpf ./nodejs_patches_for_EdgeImpulse_20240805.tar.gz
cd $DIR
unzip ./archive/RTK0EF0045Z0024AZJ-v3.0.6.zip
unzip ./archive/RTK0EF0045Z13001ZJ-v1.2.2_EN.zip
unzip ./archive/RTK0EF0045Z15001ZJ-v1.2.2_EN.zip
unzip ./archive/r11an0549ej0750-rzv2l-drpai-sp.zip
unzip ./archive/meta-ei.zip
tar zxf ./RTK0EF0045Z0024AZJ-v3.0.6/rzv_vlp_v3.0.6.tar.gz
tar zxf ./RTK0EF0045Z13001ZJ-v1.2.2_EN/meta-rz-features_graphics_v1.2.2.tar.gz
tar zxf ./RTK0EF0045Z15001ZJ-v1.2.2_EN/meta-rz-features_codec_v1.2.2.tar.gz
tar zxf ./rzv2l_drpai-driver/meta-rz-drpai.tar.gz
7z x ./archive/oss_pkg_rzv_drpai_v7.50.7z
cd $DIR/meta-renesas
patch -p1 < ${DIR}/RTK0EF0045Z0024AZJ-v3.0.6/0001-rz-common-recipes-debian-buster-glibc-update-to-v2.2.patch
cd ${DIR}/meta-openembedded/meta-oe/recipes-devtools/
tar -zxvf ${DIR}/archive/nodejs_patches_for_EdgeImpulse/nodejs_18.17.1.tar.gz
mv nodejs nodejs_12.22.12
ln -s nodejs_18.17.1 nodejs
cd ${DIR}
patch -p1 < ${DIR}/archive/nodejs_patches_for_EdgeImpulse/nodejs_18.17.1.bb.patch
cd ${DIR}/poky/meta/recipes-support/
tar -zxvf ${DIR}/archive/nodejs_patches_for_EdgeImpulse/icu_70.1.tar.gz
mv icu icu_66.1
ln -s icu_70.1 icu
cd ${DIR}
patch -p1 < ${DIR}/archive/nodejs_patches_for_EdgeImpulse/icu_70.1.bb.patch
cd ${DIR}
cp ${DIR}/archive/nodejs_patches_for_EdgeImpulse/0002_add_TRUE_FALSE_to_libical-3.0.7_icalrecur.h.patch ${DIR}/poky/meta/recipes-support/libical/libical
patch -p1 < ${DIR}/archive/nodejs_patches_for_EdgeImpulse/libical_3.0.7.bb.patch
source poky/oe-init-build-env build
cd $DIR/build
bitbake-layers add-layer ../meta-qt5
bitbake-layers add-layer ../meta-rz-features/meta-rz-graphics
bitbake-layers add-layer ../meta-rz-features/meta-rz-codecs
bitbake-layers add-layer ../meta-rz-features/meta-rz-drpai
bitbake-layers add-layer ../meta-openembedded/meta-filesystems
bitbake-layers add-layer ../meta-openembedded/meta-networking
bitbake-layers add-layer ../meta-virtualization
bitbake-layers add-layer ../meta-ei
cd $DIR/build
echo "" >> ./conf/local.conf
echo "IMAGE_INSTALL_append = \" \\" >> ./conf/local.conf
echo " nodejs \\" >> ./conf/local.conf
echo " nodejs-npm \\" >> ./conf/local.conf
echo " \"" >> ./conf/local.conf
echo "" >> ./conf/local.conf
echo "" >> ./conf/local.conf
echo "IMAGE_INSTALL_append = \" \\" >> ./conf/local.conf
echo " nvme-cli \\" >> ./conf/local.conf
echo " sudo \\" >> ./conf/local.conf
echo " curl \\" >> ./conf/local.conf
echo " zlib \\" >> ./conf/local.conf
echo " binutils \\" >> ./conf/local.conf
echo " drpaitvm \\" >> ./conf/local.conf
echo " git git-perltools \\" >> ./conf/local.conf
echo " \"" >> ./conf/local.conf
echo "" >> ./conf/local.conf
echo "WHITELIST_GPL-3.0 += \" cpp gcc gcc-dev mpfr g++ cpp make make-dev binutils libbfd \"" >> ./conf/local.conf
echo "IMAGE_INSTALL_append = \" zlib gcc g++ make cpp packagegroup-core-buildessential \"" >> ./conf/local.conf
echo "IMAGE_INSTALL_append = \" python3 python3-pip python3-core python3-modules \"" >> ./conf/local.conf
echo "IMAGE_INSTALL_append = \" gstreamer1.0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good \"" >> ./conf/local.conf
echo "IMAGE_INSTALL_append = \" gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly \"" >> ./conf/local.conf
echo "EXTRA_IMAGE_FEATURES ?= \" debug-tweaks dev-pkgs tools-debug tools-sdk \"" >> ./conf/local.conf
echo "DISTRO_FEATURES ?= \" usbgadget usbhost wifi opengl \"" >> ./conf/local.conf
echo "IMAGE_ROOTFS_EXTRA_SPACE_append_qemuall = \" + 3000000\"" >> ./conf/local.conf
#-# glibc2.31 instead of glibc2.28
sed -i 's/^CIP_MODE = "Buster"/CIP_MODE = "Bullseye"/g' ./conf/local.conf