#!/usr/bin/env bash
# Copyright (c) 2000-2020 Synology Inc. All rights reserved.

[ -z "$__INCLUDE_PLATFORM__" ] || return
readonly __INCLUDE_PLATFORM__=defined


# shellcheck source=/dev/null
source "SynoBuildConf/_env"

platform_target()
{
    case $PLATFORM_FAMILY in
        SYNOPLAT_F_X86_64)
            echo "bzImage"
            ;;
        SYNOPLAT_F_ARMV8)
            echo "Image.lzma"
            ;;
    esac
}

# displayed image name should be matched with the result of
# platform_customize_image()
platform_image_name()
{
    [ -f ".config" ] || return

    if [ "$PLATFORM_FAMILY" = "SYNOPLAT_F_X86_64" ]; then
        make -s image_name
    elif [ "$PLATFORM_FAMILY" = "SYNOPLAT_F_ARMV8" ]; then
        make -s image_name
    else
        echo "ENOENT"
    fi
}

# shellcheck disable=SC2154
platform_customize_rtd1619b_mango_image()
{
    # shellcheck disable=SC2043
    for model in mango; do
        DTBIMG="${KernelDir}/arch/arm64/boot/dts/realtek/rtd-1619b-synology-${model}.dtb"
        mkdir -p "${DebDevBuild}/image/files/sds-sii/dtbs/synology_rtd1619bmango_${model}/"
        cp -vf "${DTBIMG}" "${DebDevBuild}/image/files/sds-sii/dtbs/synology_rtd1619bmango_${model}/model-vendor.dtb"
    done
}

# shellcheck disable=SC2154
platform_customize_rtd1619b_image()
{
    # shellcheck disable=SC2043
    for model in ds223j ds423 ds223 ds124; do
        DTBIMG="${KernelDir}/arch/arm64/boot/dts/realtek/rtd-1619b-synology-${model}.dtb"
        mkdir -p "${DebDevBuild}/image/files/sds-sii/dtbs/synology_rtd1619b_${model}/"
        cp -vf "${DTBIMG}" "${DebDevBuild}/image/files/sds-sii/dtbs/synology_rtd1619b_${model}/model-vendor.dtb"
    done
}

# some platforms such as REALTEK_RTD1296, may need customized kernel image.
# please leave your customization here.
platform_customize_image()
{
    if [ "$PLATFORM_FAMILY" = "SYNOPLAT_F_X86_64" ]; then
        : # do nothing
    elif [ "$BUILD_TARGET" = "REALTEK_RTD1619B" ]; then
        platform_customize_rtd1619b_image
    elif [ "$BUILD_TARGET" = "REALTEK_RTD1619B_MANGO" ]; then
        platform_customize_rtd1619b_mango_image
    else
        : # customized by platform later
    fi
}

