#!/bin/bash
set -x
DIR=`pwd`
# Go to the directory that you have downloaded all of the above files into... then:
mkdir ./archive
mv RTK* ./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/RTK0EF0180F05000SJ_linux-src.zip
tar zxf rzv2h_ai-sdk_yocto_recipe_v5.00.tar.gz
unzip ./archive/meta-ei.zip
cd $DIR
TEMPLATECONF=$DIR/meta-renesas/meta-rzv2h/docs/template/conf/
export MACHINE=rzv2h-evk-ver1
source poky/oe-init-build-env
cd $DIR/build
bitbake-layers add-layer ../meta-rz-features/meta-rz-graphics
bitbake-layers add-layer ../meta-rz-features/meta-rz-drpai
bitbake-layers add-layer ../meta-rz-features/meta-rz-opencva
bitbake-layers add-layer ../meta-rz-features/meta-rz-codecs
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
patch -p1 < ../0001-tesseract.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}
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/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 " drpaitvm \\" >> ./conf/local.conf
echo " binutils \\" >> ./conf/local.conf
echo " \"" >> ./conf/local.conf
echo "" >> ./conf/local.conf
#
# Extra image configuration defaults
#
# The EXTRA_IMAGE_FEATURES variable allows extra packages to be added to the generated
# images. Some of these options are added to certain image types automatically. The
# variable can contain the following options:
# "dbg-pkgs" - add -dbg packages for all installed packages
# (adds symbol information for debugging/profiling)
# "src-pkgs" - add -src packages for all installed packages
# (adds source code for debugging)
# "dev-pkgs" - add -dev packages for all installed packages
# (useful if you want to develop against libs in the image)
# "ptest-pkgs" - add -ptest packages for all ptest-enabled packages
# (useful if you want to run the package test suites)
# "tools-sdk" - add development tools (gcc, make, pkgconfig etc.)
# "tools-debug" - add debugging tools (gdb, strace)
# "eclipse-debug" - add Eclipse remote debugging support
# "tools-profile" - add profiling tools (oprofile, lttng, valgrind)
# "tools-testapps" - add useful testing tools (ts_print, aplay, arecord etc.)
# "debug-tweaks" - make an image suitable for development
# e.g. ssh root access has a blank password
# There are other application targets that can be used here too, see
# meta/classes/image.bbclass and meta/classes/core-image.bbclass for more details.
#
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