# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2017-present Team LibreELEC (https://libreelec.tv)

SRCDIR="${BUILD}"/image/system/usr/share/bootloader
DSTDIR="${RELEASE_DIR}"/3rdparty/bootloader

mkdir -p "${DSTDIR}"
if [ -n "${UBOOT_SYSTEM}" ]; then
  if [ -f "${SRCDIR}"/"${UBOOT_FIT_IMAGE}" ]; then
    cp -av "${SRCDIR}"/"${UBOOT_FIT_IMAGE}" "${DSTDIR}" && \
    ln -sfv "${UBOOT_FIT_IMAGE}" "${DSTDIR}"/idbloader.img || exit 1
    if [ "${ROCKCHIP_LEGACY_BOOT}" = "1" ]; then
      touch "${DSTDIR}"/.rockchip_boot_chain_old
    else
      touch "${DSTDIR}"/.rockchip_boot_chain_new
    fi
  else
    echo "release: bootloader image ${SRCDIR}/${UBOOT_FIT_IMAGE} does not exist."
    exit 1
  fi
fi

cp -av "${SRCDIR}"/"${DEVICE,,}"*.dtb "${DSTDIR}"
