#!/bin/bash

declare -A SUPPORTED_MACHINE=([mcm-imx8m-mini]=1 [ucm-imx8m-mini]=1 [iot-gate-imx8]=1)

get_meta() {
if [[ -d ${dir} ]];then
git -C ${dir} remote update
git -C ${dir} pull
else
git clone ${url} ${dir}
git -C ${dir} checkout ${branch}
fi
}

get_meta_compulab() {
dir="../sources/meta-compulab"
branch="f5f8a2198e2bf037e1999653b9149c0622c9a0ec"
url="https://github.com/compulab-yokneam/meta-compulab"
get_meta ${url} ${branch} ${dir}
}

get_meta_compulab_bsp() {
dir="../sources/meta-compulab-bsp"
branch="01015e56471b1eac23f9024d8a1225e0be54972d"
url="https://github.com/compulab-yokneam/meta-compulab-bsp"
get_meta ${url} ${branch} ${dir}
}

mod_bblayer() {
# Get CompuLab Main meta layer
get_meta_compulab
get_meta_compulab_bsp
# A mandatory bsp layer: kernel, u-boot, imx-boot
cat << eof | tee -a ${BBLAYERS_CONF} > /dev/null
BBLAYERS += " \\
	\${BSPDIR}/sources/meta-compulab \\
	\${BSPDIR}/sources/meta-compulab-bsp/meta-desktop \\
	\${BSPDIR}/sources/meta-compulab-bsp/meta-multimedia \\
	\${BSPDIR}/sources/meta-compulab-bsp/meta-graphics \\
	\${BSPDIR}/sources/meta-compulab-bsp/meta-bsp \\
	\${BSPDIR}/sources/meta-compulab-bsp/meta-utils \\
	\${BSPDIR}/sources/meta-bsp-imx8mm \\
"
eof
}

mod_local() {
# Modify local.conf
if [[ ${MACHINE} == 'iot-gate-imx8' ]];then
CORE_IMAGE_EXTRA_INSTALL+=" dhcp-client memtester cl-uboot cl-deploy"
CORE_IMAGE_EXTRA_INSTALL+=" cl-migrate-env cl-stest u-boot-fw-utils"
CORE_IMAGE_EXTRA_INSTALL+=" modemmanager networkmanager wireless-tools bluez5"
CORE_IMAGE_EXTRA_INSTALL+=" can-utils gpsd gps-utils minicom wvdial tzdata"
CORE_IMAGE_EXTRA_INSTALL+=" eeprom-util"
else
CORE_IMAGE_EXTRA_INSTALL+=" dhcp-client memtester cl-uboot cl-deploy cl-migrate-env cl-launcher cl-stest cl-camera u-boot-fw-utils imx-gpu-viv-demos"
fi
CORE_IMAGE_EXTRA_INSTALL+=" htop iotop tmux iperf3"
IMAGE_FEATURES+=" package-management ssh-server-openssh "
cat << eof | tee -a ${LOCAL_CONF} > /dev/null
CORE_IMAGE_EXTRA_INSTALL += "${CORE_IMAGE_EXTRA_INSTALL}"
IMAGE_FEATURES += "${IMAGE_FEATURES}"
LICENSE_FLAGS_WHITELIST = "commercial"
PREFERRED_RPROVIDER_u-boot-fw-utils = "u-boot-imx-fw-utils"
HOSTTOOLS += " git-lfs"
eof
}

CWD=$(pwd)
PROGNAME=${BASH_SOURCE[0]}
[ $(basename -- $BASH_SOURCE) == $(basename -- $0) ] && EXIT="exit" || EXIT="return"

if [[ $(id -u) -eq 0 ]]; then
    echo "ERROR: do not use the BSP as root. Exiting..."
    ${EXIT}
fi

BBLAYERS_CONF=conf/bblayers.conf
BB_CL_CONF_DONE=conf/bb_cl_conf.done
LOCAL_CONF=conf/local.conf

if ([ ! -f ${BBLAYERS_CONF} ] || [ ! -f ${LOCAL_CONF} ]);then
cat << eom
    Warning: Files ${BBLAYERS_CONF} and ${LOCAL_CONF} not found.
    Make sure that the scritp issued at a correct location.
eom
    ${EXIT} 2
fi

[[ -n ${FORCE} ]] && rm -rf ${BB_CL_CONF_DONE}

if [ -f ${BB_CL_CONF_DONE} ];then
cat << eom
    Note: Already issued.
    Nothing to be done.
eom
    ${EXIT} 0
fi

eval $(awk '(/MACHINE|DISTRO/)&&(gsub(/'"'"'/,"")&&($0=$1"="$NF))' ORS=";" ${LOCAL_CONF})

if [[ ${SUPPORTED_MACHINE[${MACHINE}]} -ne 1 ]];then
cat << eom
    ${MACHINE} is not in supported machine list.
    Error exit.
eom
    ${EXIT} 0
fi

cat << eom
    Prepare: ${MACHINE}:${DISTRO} ...
eom

mod_bblayer
mod_local

cat << eom
    Note: Files ${BBLAYERS_CONF} and ${LOCAL_CONF} updated successfully.
eom

cat << eom > ${BB_CL_CONF_DONE}
$(date)
eom

unset IMAGE_FEATURES CORE_IMAGE_EXTRA_INSTALL
${EXIT} 0
