#!/bin/bash -ex

progname="$0"
workdir=$(readlink -f ${PWD})
dirname="$(dirname $(readlink -f "${0}"))"
fit_path=${dirname}/fit

DEFCONFIG_FILE="p3636-0001_defconfig"
DEFCONFIG_FIT_FILE="p3636-0001_fit_defconfig"
DEFCONFIG_NO_CONSOLE_FILE="p3636-0001_no_console_defconfig"

NOT_SUPPORT_FIT_IMAGE="u-boot.bin"
NOT_SUPPORT_FIT_RELEASE_IMAGE="u-boot_release.bin"
SUPPORT_FIT_DEBUG_IMAGE="u-boot-fit_debug.bin"
SUPPORT_FIT_SECURE_IMAGE="u-boot-fit_secure.bin"
SUPPORT_FIT_SECURE_RELEASE_IMAGE="u-boot-fit_secure_release.bin"

if [ $# -eq 1 ]; then
    if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
        echo "usage:"
        echo "  $(basename ${progname})"
        echo "    build ${NOT_SUPPORT_FIT_IMAGE}, ${NOT_SUPPORT_FIT_RELEASE_IMAGE} and ${SUPPORT_FIT_DEBUG_IMAGE}"
        echo ""
        echo "  $(basename ${progname}) <key_path> <fit_name>"
        echo "    build following images."
        echo "      ${NOT_SUPPORT_FIT_IMAGE}"
        echo "      ${NOT_SUPPORT_FIT_RELEASE_IMAGE}"
        echo "      ${SUPPORT_FIT_DEBUG_IMAGE}"
        echo "      ${SUPPORT_FIT_SECURE_IMAGE}"
        echo "      ${SUPPORT_FIT_SECURE_RELEASE_IMAGE}"
        echo ""
        echo "  parameters:"
        echo "      key_path       : directory of <fit_name>_secure.key and <fit_name>_secure.crt."
        echo "      fit_name       : prefix of _secure.its, _fit.key and _fit.crt."
        exit 0
    fi
fi

key_path=${1}
if [ -n "${key_path}" ]; then
    key_path=$(readlink -f ${key_path})
fi

pushd ${dirname}/../Vss-G8-BDK18-Toolchain
. enable_path.sh
popd

rm -f ${NOT_SUPPORT_FIT_IMAGE}
rm -f ${NOT_SUPPORT_FIT_RELEASE_IMAGE}
rm -f ${SUPPORT_FIT_DEBUG_IMAGE}
rm -f ${SUPPORT_FIT_SECURE_IMAGE}
rm -f ${SUPPORT_FIT_SECURE_RELEASE_IMAGE}

pushd ${dirname}/u-boot

release_defconfig="${DEFCONFIG_FILE}_release_defconfig"
fit_defconfig="${DEFCONFIG_FILE}_fit_defconfig"
fit_release_defconfig="${DEFCONFIG_FILE}_fit_release_defconfig"

cat ./configs/${DEFCONFIG_FILE} ./configs/${DEFCONFIG_NO_CONSOLE_FILE} > "./configs/${release_defconfig}"
cat ./configs/${DEFCONFIG_FILE} ./configs/${DEFCONFIG_FIT_FILE} > "./configs/${fit_defconfig}"
cat ./configs/${DEFCONFIG_FILE} ./configs/${DEFCONFIG_FIT_FILE} ./configs/${DEFCONFIG_NO_CONSOLE_FILE} > "./configs/${fit_release_defconfig}"

echo "build ${NOT_SUPPORT_FIT_IMAGE}"
make distclean
make ${DEFCONFIG_FILE}
make -j$(nproc) CROSS_COMPILE=$UBUNTU_U18_TOOLCHAIN/aarch64-linux-gnu/cross/bin/aarch64-linux-gnu-
cp -a u-boot.bin ${dirname}/${NOT_SUPPORT_FIT_IMAGE}

echo "build ${NOT_SUPPORT_FIT_RELEASE_IMAGE}"
make distclean
make ${release_defconfig}
make -j$(nproc) CROSS_COMPILE=$UBUNTU_U18_TOOLCHAIN/aarch64-linux-gnu/cross/bin/aarch64-linux-gnu-
cp -a u-boot.bin ${dirname}/${NOT_SUPPORT_FIT_RELEASE_IMAGE}

echo "build ${SUPPORT_FIT_DEBUG_IMAGE}"
make distclean
make ${fit_defconfig}
make -j$(nproc) CROSS_COMPILE=$UBUNTU_U18_TOOLCHAIN/aarch64-linux-gnu/cross/bin/aarch64-linux-gnu-
cp -a ./tools/mkimage ${dirname}/mkimage
cp -a u-boot.bin ${dirname}/${SUPPORT_FIT_DEBUG_IMAGE}

if [ $# -eq 2 ]; then
    if [ ! -d "${key_path}" ]; then
        echo "Error: ${key_path} not found"
        exit 1
    fi

    fit_name=${2}
    itsfile="${fit_path}/${fit_name}_secure.its"
    if [ ! -f "${itsfile}" ]; then
        echo "Error: ${itsfile} not found"
        exit 1
    fi

    keyfile="${key_path}/${fit_name}_fit.key"
    if [ ! -f "${keyfile}" ]; then
        echo "Error: ${keyfile} not found"
        exit 1
    fi

    crtfile="${key_path}/${fit_name}_fit.crt"
    if [ ! -f "${crtfile}" ]; then
        echo "Error: ${crtfile} not found"
        exit 1
    fi

    touch ${fit_path}/Image
    touch ${fit_path}/initrd

    ./tools/mkimage -k ${key_path} -f ${itsfile} ${fit_path}/image.fit
    ./tools/mkimage -F -k ${key_path} -K dts/dt.dtb -r ${fit_path}/image.fit
    make -j$(nproc) CROSS_COMPILE=$UBUNTU_U18_TOOLCHAIN/aarch64-linux-gnu/cross/bin/aarch64-linux-gnu-
    cp -a u-boot.bin ${dirname}/${SUPPORT_FIT_SECURE_IMAGE}

    echo "build ${SUPPORT_FIT_SECURE_RELEASE_IMAGE}"
    make distclean
    make ${fit_release_defconfig}
    make -j$(nproc) CROSS_COMPILE=$UBUNTU_U18_TOOLCHAIN/aarch64-linux-gnu/cross/bin/aarch64-linux-gnu-

    ./tools/mkimage -k ${key_path} -f ${itsfile} ${fit_path}/image.fit
    ./tools/mkimage -F -k ${key_path} -K dts/dt.dtb -r ${fit_path}/image.fit
    make -j$(nproc) CROSS_COMPILE=$UBUNTU_U18_TOOLCHAIN/aarch64-linux-gnu/cross/bin/aarch64-linux-gnu-
    cp -a u-boot.bin ${dirname}/${SUPPORT_FIT_SECURE_RELEASE_IMAGE}

    rm ${fit_path}/image.fit
    rm ${fit_path}/initrd
    rm ${fit_path}/Image
fi

rm -f "./configs/${release_defconfig}"
rm -f "./configs/${fit_defconfig}"
rm -f "./configs/${fit_release_defconfig}"

popd
