#!/bin/sh

__BITBAKE_LAST_LINE=0

# Emit a useful diagnostic if something fails:
bb_sh_exit_handler() {
    ret=$?
    if [ "$ret" != 0 ]; then
        echo "WARNING: exit code $ret from a shell command."
    fi
    exit $ret
}

bb_bash_exit_handler() {
    ret=$?
    { set +x; } > /dev/null
    trap "" DEBUG
    if [ "$ret" != 0 ]; then
        echo "WARNING: ${BASH_SOURCE[0]}:${__BITBAKE_LAST_LINE} exit $ret from '$1'"

        echo "WARNING: Backtrace (BB generated script): "
        for i in $(seq 1 $((${#FUNCNAME[@]} - 1))); do
            if [ "$i" -eq 1 ]; then
                echo -e "	#$((i)): ${FUNCNAME[$i]}, ${BASH_SOURCE[$((i-1))]}, line ${__BITBAKE_LAST_LINE}"
            else
                echo -e "	#$((i)): ${FUNCNAME[$i]}, ${BASH_SOURCE[$((i-1))]}, line ${BASH_LINENO[$((i-1))]}"
            fi
        done
    fi
    exit $ret
}

bb_bash_debug_handler() {
    local line=${BASH_LINENO[0]}
    # For some reason the DEBUG trap trips with lineno=1 when scripts exit; ignore it
    if [ "$line" -eq 1 ]; then
        return
    fi

    # Track the line number of commands as they execute. This is so we can have access to the failing line number
    # in the EXIT trap. See http://gnu-bash.2382.n7.nabble.com/trap-echo-quot-trap-exit-on-LINENO-quot-EXIT-gt-wrong-linenumber-td3666.html
    if [ "${FUNCNAME[1]}" != "bb_bash_exit_handler" ]; then
        __BITBAKE_LAST_LINE=$line
    fi
}

case $BASH_VERSION in
"") trap 'bb_sh_exit_handler' 0
    set -e
    ;;
*)  trap 'bb_bash_exit_handler "$BASH_COMMAND"' 0
    trap '{ bb_bash_debug_handler; } 2>/dev/null' DEBUG
    set -e
    shopt -s extdebug
    ;;
esac
export AR="aarch64-poky-linux-gcc-ar"
export AS="aarch64-poky-linux-as "
export BUILD_AR="ar"
export BUILD_AS="as "
export BUILD_CC="gcc "
export BUILD_CCLD="gcc "
export BUILD_CFLAGS="-isystem/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/usr/include -O2 -pipe"
export BUILD_CPP="gcc  -E"
export BUILD_CPPFLAGS="-isystem/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/usr/include"
export BUILD_CXX="g++ "
export BUILD_CXXFLAGS="-isystem/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/usr/include -O2 -pipe"
export BUILD_FC="gfortran "
export BUILD_LD="ld "
export BUILD_LDFLAGS="-L/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/usr/lib                         -L/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/lib                         -Wl,--enable-new-dtags                         -Wl,-rpath-link,/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/usr/lib                         -Wl,-rpath-link,/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/lib                         -Wl,-rpath,/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/usr/lib                         -Wl,-rpath,/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/lib                         -Wl,-O1 -Wl,--allow-shlib-undefined -Wl,--dynamic-linker=/home/sim/yocto/scarthgap/build/tmp/sysroots-uninative/x86_64-linux/lib/ld-linux-x86-64.so.2 -pthread"
export BUILD_NM="nm"
export BUILD_RANLIB="ranlib -D"
export BUILD_STRIP="strip"
export CC="aarch64-poky-linux-gcc  -mcpu=cortex-a72+crc -mbranch-protection=standard -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot"
export CCLD="aarch64-poky-linux-gcc  -mcpu=cortex-a72+crc -mbranch-protection=standard -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot"
export CFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types -fcanon-prefix-map  -fmacro-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fmacro-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot=  -fmacro-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot=  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native= "
export CPP="aarch64-poky-linux-gcc -E --sysroot=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot  -mcpu=cortex-a72+crc -mbranch-protection=standard -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security"
export CPPFLAGS=""
export CXX="aarch64-poky-linux-g++  -mcpu=cortex-a72+crc -mbranch-protection=standard -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot"
export CXXFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types -fcanon-prefix-map  -fmacro-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fmacro-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot=  -fmacro-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot=  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native=  -fvisibility-inlines-hidden"
export FC="aarch64-poky-linux-gfortran  -mcpu=cortex-a72+crc -mbranch-protection=standard -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot"
export GIT_CEILING_DIRECTORIES="/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git:/home/sim/yocto/scarthgap/build/tmp"
export HOME="/home/sim"
export LC_ALL="en_US.UTF-8"
export LD="aarch64-poky-linux-ld --sysroot=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot "
export LDFLAGS="-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -fcanon-prefix-map  -fmacro-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fmacro-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git=/usr/src/debug/rpi-config/git  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot=  -fmacro-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot=  -fdebug-prefix-map=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native=  -Wl,-z,relro,-z,now"
export LOGNAME="sim"
export MAKE="make"
export NM="aarch64-poky-linux-gcc-nm"
export OBJCOPY="aarch64-poky-linux-objcopy"
export OBJDUMP="aarch64-poky-linux-objdump"
export OMP_NUM_THREADS="8"
export PATH="/home/sim/yocto/scarthgap/build/tmp/sysroots-uninative/x86_64-linux/usr/bin:/home/sim/yocto/scarthgap/poky/scripts:/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/usr/bin/aarch64-poky-linux:/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot/usr/bin/crossscripts:/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/usr/sbin:/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/usr/bin:/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/sbin:/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot-native/bin:/home/sim/yocto/scarthgap/poky/bitbake/bin:/home/sim/yocto/scarthgap/build/tmp/hosttools"
export PERL_HASH_SEED="0"
export PKG_CONFIG_DIR="/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot/usr/lib/pkgconfig"
export PKG_CONFIG_DISABLE_UNINSTALLED="yes"
export PKG_CONFIG_LIBDIR="/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot/usr/lib/pkgconfig"
export PKG_CONFIG_PATH="/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot/usr/lib/pkgconfig:/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot/usr/share/pkgconfig"
export PKG_CONFIG_SYSROOT_DIR="/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/recipe-sysroot"
export PKG_CONFIG_SYSTEM_INCLUDE_PATH="/usr/include"
export PKG_CONFIG_SYSTEM_LIBRARY_PATH="/usr/lib:/usr/lib"
export PSEUDO_DISABLED="1"
export PSEUDO_UNLOAD="1"
export PYTHONHASHSEED="0"
export RANLIB="aarch64-poky-linux-gcc-ranlib"
export READELF="aarch64-poky-linux-readelf"
unset SHELL
export SOURCE_DATE_EPOCH="1629022265"
export STRINGS="aarch64-poky-linux-strings"
export STRIP="aarch64-poky-linux-strip"
export TZ="UTC"
export USER="sim"
export base_bindir="/usr/bin"
export base_libdir="/usr/lib"
export base_prefix=""
export base_sbindir="/usr/sbin"
export bindir="/usr/bin"
export datadir="/usr/share"
export docdir="/usr/share/doc"
export exec_prefix="/usr"
export includedir="/usr/include"
export infodir="/usr/share/info"
export libdir="/usr/lib"
export libexecdir="/usr/libexec"
export localstatedir="/var"
export mandir="/usr/share/man"
export nonarch_base_libdir="/usr/lib"
export nonarch_libdir="/usr/lib"
export prefix="/usr"
export sbindir="/usr/sbin"
export servicedir="/srv"
export sharedstatedir="/com"
export sysconfdir="/etc"
export systemd_system_unitdir="/usr/lib/systemd/system"
export systemd_unitdir="/usr/lib/systemd"
export systemd_user_unitdir="/usr/lib/systemd/user"

# line: 44, file: /home/sim/yocto/scarthgap/poky/meta-raspberrypi/recipes-bsp/bootfiles/rpi-config_git.bb
do_deploy() {
    install -d /home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/deploy-rpi-config/bootfiles
    CONFIG=/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/deploy-rpi-config/bootfiles/config.txt

    cp /home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/git/config.txt $CONFIG

    if [ -n "${KEY_DECODE_MPG2}" ]; then
        sed -i '/#decode_MPG2=/ c\decode_MPG2=${KEY_DECODE_MPG2}' $CONFIG
    fi
    if [ -n "${KEY_DECODE_WVC1}" ]; then
        sed -i '/#decode_WVC1=/ c\decode_WVC1=${KEY_DECODE_WVC1}' $CONFIG
    fi
    if [ -n "${DISABLE_OVERSCAN}" ]; then
        sed -i '/#disable_overscan=/ c\disable_overscan=${DISABLE_OVERSCAN}' $CONFIG
    fi
    if [ "${DISABLE_SPLASH}" = "1" ]; then
        sed -i '/#disable_splash=/ c\disable_splash=${DISABLE_SPLASH}' $CONFIG
    fi

    # Set overclocking options
    if [ -n "${ARM_FREQ}" ]; then
        sed -i '/#arm_freq=/ c\arm_freq=${ARM_FREQ}' $CONFIG
    fi
    if [ -n "${GPU_FREQ}" ]; then
        sed -i '/#gpu_freq=/ c\gpu_freq=${GPU_FREQ}' $CONFIG
    fi
    if [ -n "${CORE_FREQ}" ]; then
        sed -i '/#core_freq=/ c\core_freq=${CORE_FREQ}' $CONFIG
    fi
    if [ -n "${SDRAM_FREQ}" ]; then
        sed -i '/#sdram_freq=/ c\sdram_freq=${SDRAM_FREQ}' $CONFIG
    fi
    if [ -n "${OVER_VOLTAGE}" ]; then
        sed -i '/#over_voltage=/ c\over_voltage=${OVER_VOLTAGE}' $CONFIG
    fi

    # GPU memory
    if [ -n "${GPU_MEM}" ]; then
        sed -i '/#gpu_mem=/ c\gpu_mem=${GPU_MEM}' $CONFIG
    fi
    if [ -n "${GPU_MEM_256}" ]; then
        sed -i '/#gpu_mem_256=/ c\gpu_mem_256=${GPU_MEM_256}' $CONFIG
    fi
    if [ -n "${GPU_MEM_512}" ]; then
        sed -i '/#gpu_mem_512=/ c\gpu_mem_512=${GPU_MEM_512}' $CONFIG
    fi
    if [ -n "${GPU_MEM_1024}" ]; then
        sed -i '/#gpu_mem_1024=/ c\gpu_mem_1024=${GPU_MEM_1024}' $CONFIG
    fi

    # Set boot delay
    if [ -n "${BOOT_DELAY}" ]; then
        sed -i '/#boot_delay=/ c\boot_delay=${BOOT_DELAY}' $CONFIG
    fi
    if [ -n "${BOOT_DELAY_MS}" ]; then
        sed -i '/#boot_delay_ms=/ c\boot_delay_ms=${BOOT_DELAY_MS}' $CONFIG
    fi

    # Set HDMI and composite video options
    if [ -n "${HDMI_FORCE_HOTPLUG}" ]; then
        sed -i '/#hdmi_force_hotplug=/ c\hdmi_force_hotplug=${HDMI_FORCE_HOTPLUG}' $CONFIG
    fi
    if [ -n "${HDMI_DRIVE}" ]; then
        sed -i '/#hdmi_drive=/ c\hdmi_drive=${HDMI_DRIVE}' $CONFIG
    fi
    if [ -n "${HDMI_GROUP}" ]; then
        sed -i '/#hdmi_group=/ c\hdmi_group=${HDMI_GROUP}' $CONFIG
    fi
    if [ -n "${HDMI_MODE}" ]; then
        sed -i '/#hdmi_mode=/ c\hdmi_mode=${HDMI_MODE}' $CONFIG
    fi
    if [ -n "${HDMI_CVT}" ]; then
        echo 'hdmi_cvt=${HDMI_CVT}' >> $CONFIG
    fi
    if [ -n "${CONFIG_HDMI_BOOST}" ]; then
        sed -i '/#config_hdmi_boost=/ c\config_hdmi_boost=${CONFIG_HDMI_BOOST}' $CONFIG
    fi
    if [ -n "${SDTV_MODE}" ]; then
        sed -i '/#sdtv_mode=/ c\sdtv_mode=${SDTV_MODE}' $CONFIG
    fi
    if [ -n "${SDTV_ASPECT}" ]; then
        sed -i '/#sdtv_aspect=/ c\sdtv_aspect=${SDTV_ASPECT}' $CONFIG
    fi
    if [ -n "${DISPLAY_ROTATE}" ]; then
        sed -i '/#display_rotate=/ c\display_rotate=${DISPLAY_ROTATE}' $CONFIG
    fi

    # Video camera support
    if [ "${VIDEO_CAMERA}" = "1" ]; then
        #   It has been observed that Raspberry Pi 4B 4GB may fail to enable the
        # camera if "start_x=1" is at the end of the file. Therefore,
        # "start_x=1" has been set to replace the original occurrence in
        # config.txt, which is at the middle of the file.
        #   The exact underlying cause is unknown. There are similar issues
        # reported in the raspberrypi/firware repo and the conclusion reached
        # was that there could be a file size limitation affecting certain
        # variables. It was commented that this limitation could be 4k but
        # not proved.
        sed -i '/#start_x=/ c\start_x=1' $CONFIG
    fi

    # Offline compositing support
    if [ "${DISPMANX_OFFLINE}" = "1" ]; then
        echo "# Enable offline compositing" >>$CONFIG
        echo "dispmanx_offline=1" >>$CONFIG
    fi

    # SPI bus support
    if [ "1" = "1" ] || [ "0" = "1" ]; then
        echo "# Enable SPI bus" >>$CONFIG
        echo "dtparam=spi=on" >>$CONFIG
    fi

    # I2C support
    if [ "1" = "1" ] || [ "0" = "1" ]; then
        echo "# Enable I2C" >>$CONFIG
        echo "dtparam=i2c1=on" >>$CONFIG
        echo "dtparam=i2c_arm=on" >>$CONFIG
    fi

    # PiTFT22 display support
    if [ "0" = "1" ]; then
        echo "# Enable PITFT22 display" >>$CONFIG
        echo "dtoverlay=pitft22,rotate=270,speed=32000000,txbuflen=32768" >>$CONFIG
    fi
    if [ "0" = "1" ]; then
        echo "# Enable PITFT28r display" >>$CONFIG
        echo "dtoverlay=pitft28-resistive,rotate=90,speed=32000000,txbuflen=32768" >>$CONFIG
    fi
    if [ "0" = "1" ]; then
        echo "# Enable PITFT28c display" >>$CONFIG
        echo "dtoverlay=pitft28-capacitive,rotate=90,speed=32000000,txbuflen=32768" >>$CONFIG
        echo "dtoverlay=pitft28-capacitive,touch-swapxy,touch-invx" >>$CONFIG
    fi
    if [ "0" = "1" ]; then
        echo "# Enable PITFT35r display" >>$CONFIG
        echo "dtoverlay=pitft35-resistive,rotate=90,speed=42000000,fps=20" >>$CONFIG
    fi

    # UART support
    if [ "1" = "1" ] || [ "1" = "0" ]; then
        echo "# Enable UART" >>$CONFIG
        echo "enable_uart=1" >>$CONFIG
    elif [ -n "1" ]; then
        bbfatal "Invalid value for ENABLE_UART [1]. The value for ENABLE_UART can be 0 or 1."
    fi

    # U-Boot requires "enable_uart=1" for various boards to operate correctly
    # cf https://source.denx.de/u-boot/u-boot/-/blob/v2023.04/arch/arm/mach-bcm283x/Kconfig?ref_type=tags#L65
    if [ "${RPI_USE_U_BOOT}" = "1" ] && [ "1" != "1" ]; then
        case "rpi_arm64_config" in
            rpi_0_w_defconfig|rpi_3_32b_config|rpi_4_32b_config|rpi_arm64_config)
                if [ "1" = "0" ]; then
                    bbfatal "Invalid configuration: RPI_USE_U_BOOT requires to enable the UART in config.txt for raspberrypi4-64"
                fi
                echo "# U-Boot requires UART" >>$CONFIG
                echo "enable_uart=1" >>$CONFIG
                ;;
        esac
    fi

    if [ "1"  = "1" ] && [ "raspberrypi4-64" = "raspberrypi-cm5-io-board" ]; then
        # Enable UART on the 40-pin header of the CM5 IO Board
        echo "dtoverlay=uart0" >>$CONFIG
        echo "dtparam=uart0_console" >>$CONFIG
    fi

    # Infrared support
    if [ "${ENABLE_IR}" = "1" ]; then
        echo "# Enable infrared" >>$CONFIG
        echo "dtoverlay=gpio-ir,gpio_pin=18" >>$CONFIG
        echo "dtoverlay=gpio-ir-tx,gpio_pin=17" >>$CONFIG
    fi

    # VC4 Graphics support
    if [ "1" = "1" ]; then
        echo "# Enable VC4 Graphics" >> $CONFIG
        echo "dtoverlay=vc4-kms-v3d" >> $CONFIG
    fi

    # Choose Camera Sensor to be used, default imx219 sensor
    if [ "${RASPBERRYPI_CAMERA_V2}" = "1" ]; then
        echo "# Enable Sony RaspberryPi Camera(imx219)" >> $CONFIG
        echo "dtoverlay=imx219" >> $CONFIG
    fi

    # Choose Camera Sensor to be used, default imx477 sensor
    #if [ "${RASPBERRYPI_HD_CAMERA}" = "1" ]; then
    #    echo "# Enable Sony RaspberryPi Camera(imx477)" >> $CONFIG
    #    echo "dtoverlay=imx477" >> $CONFIG
    #fi

    # Choose Camera Sensor to be used, default imx708 sensor
    if [ "${RASPBERRYPI_CAMERA_V3}" = "1" ]; then
        echo "# Enable Sony RaspberryPi Camera(imx708)" >> $CONFIG
        echo "dtoverlay=imx708" >> $CONFIG
    fi

    # Waveshare "C" 1024x600 7" Rev2.1 IPS capacitive touch (http://www.waveshare.com/7inch-HDMI-LCD-C.htm)
    if [ "${WAVESHARE_1024X600_C_2_1}" = "1" ]; then
        echo "# Waveshare \"C\" 1024x600 7\" Rev2.1 IPS capacitive touch screen" >> $CONFIG
        echo "max_usb_current=1" >> $CONFIG
        echo "hdmi_group=2" >> $CONFIG
        echo "hdmi_mode=87" >> $CONFIG
        echo "hdmi_cvt 1024 600 60 6 0 0 0" >> $CONFIG
        echo "hdmi_drive=1" >> $CONFIG
    fi

    # DWC2 USB peripheral support
    if ([ "${ENABLE_DWC2_PERIPHERAL}" = "1" ] && [ "${ENABLE_DWC2_OTG}" != "1" ]); then
        echo "# Enable USB peripheral mode" >> $CONFIG
        echo "dtoverlay=dwc2,dr_mode=peripheral" >> $CONFIG
    fi

    # DWC2 USB host mode support
    if [ "1" = "1" ]; then
        echo "# Enable USB host mode" >> $CONFIG
        echo "dtoverlay=dwc2,dr_mode=host" >> $CONFIG
    fi

    # DWC2 USB OTG support
    if ([ "${ENABLE_DWC2_OTG}" = "1" ] && [ "${ENABLE_DWC2_PERIPHERAL}" != "1" ]); then
        echo "# Enable USB OTG mode" >> $CONFIG
        echo "dtoverlay=dwc2,dr_mode=otg" >> $CONFIG
    fi

    # AT86RF23X support
    if [ "${ENABLE_AT86RF}" = "1" ]; then
        echo "# Enable AT86RF23X" >>$CONFIG
        echo "dtoverlay=at86rf233,speed=3000000" >>$CONFIG
    fi

    # ENABLE DUAL CAN
    if [ "${ENABLE_DUAL_CAN}" = "1" ]; then
        echo "# Enable DUAL CAN" >>$CONFIG
        echo "dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25" >>$CONFIG
        echo "dtoverlay=mcp2515-can1,oscillator=16000000,interrupt=24" >>$CONFIG
    # ENABLE CAN
    elif [ "${ENABLE_CAN}" = "1" ]; then
        echo "# Enable CAN" >>$CONFIG
        echo "dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25" >>$CONFIG
    fi


    if [ "${ENABLE_GPIO_SHUTDOWN}" = "1" ]; then
        if ([ "1" = "1" ] || [ "0" = "1" ]) && [ -z "" ]; then
            # By default GPIO shutdown uses the same pin as the (master) I2C SCL.
            # If I2C is configured and an alternative pin is not configured for
            # gpio-shutdown, there is a configuration conflict.
            bbfatal "I2C and gpio-shutdown are both enabled and using the same pins!"
        fi
        echo "# Enable gpio-shutdown" >> $CONFIG
        if [ -z "" ]; then
            echo "dtoverlay=gpio-shutdown" >> $CONFIG
        else
            echo "dtoverlay=gpio-shutdown,gpio_pin=" >> $CONFIG
        fi
    fi

    # Append extra config if the user has provided any
    printf "${RPI_EXTRA_CONFIG}\n" >> $CONFIG

    # Handle setup with armstub file
    if [ "0" = "1" ]; then
        echo "\n# ARM stub configuration" >> $CONFIG
        echo "armstub=armstub8-gic.bin" >> $CONFIG
        case "armstub8-gic.bin" in
            *-gic.bin)
                echo  "enable_gic=1" >> $CONFIG
                ;;
        esac
    fi

    # WM8960 support
    if [ "0" = "1" ]; then
        echo "# Enable WM8960" >> $CONFIG
        echo "dtoverlay=wm8960-soundcard" >> $CONFIG
    fi

    # W1-GPIO - One-Wire Interface
    if [ "${ENABLE_W1}" = "1" ]; then
        echo "# Enable One-Wire Interface" >> $CONFIG
        echo "dtoverlay=w1-gpio" >> $CONFIG
    fi

    # Reduce config.txt file size to avoid corruption and
    # to boot successfully Raspberry Pi 5. The issue has
    # been reported to related projects:
    # https://github.com/raspberrypi/firmware/issues/1848
    # https://github.com/Evilpaul/RPi-config/issues/9
    sed -i '/^##/d' $CONFIG
    if grep -q -E '^.{80}.$' /home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/deploy-rpi-config/bootfiles/config.txt; then
        bbwarn "config.txt contains lines longer than 80 characters, this is not supported"
    fi
}

# line: 60, file: /home/sim/yocto/scarthgap/poky/meta/classes-global/logging.bbclass
bbfatal() {
	if [ -p /home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/temp/fifo.241744 ] ; then
		printf "%b\0" "bbfatal $*" > /home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/temp/fifo.241744
	else
		echo "ERROR: $*"
	fi
	exit 1
}

# line: 38, file: /home/sim/yocto/scarthgap/poky/meta/classes-global/logging.bbclass
bbwarn() {
	if [ -p /home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/temp/fifo.241744 ] ; then
		printf "%b\0" "bbwarn $*" > /home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/temp/fifo.241744
	else
		echo "WARNING: $*"
	fi
}

cd '/home/sim/yocto/scarthgap/build/tmp/work/raspberrypi4_64-poky-linux/rpi-config/git/deploy-rpi-config/bootfiles'
do_deploy

# cleanup
ret=$?
trap '' 0
exit $ret
