#!/bin/bash

[ -f "${LNX_APPCFG}" ] && source "${LNX_APPCFG}"
source "${FTOPDIR}/opensource/sources.sh"
source "${FTOPDIR}/lnxall_core/sources.sh"
source "${FTOPDIR}/tagman/sources.sh"

PRODUCT="${product}"

target_dummy_config() {
	return 0
}

generate_version_issue() {
	# generate /lib/dyiot/issue
	local major="$(($(date +%Y) - 2018))"
	local minor="$(date +%V%u)"
	local revision="$(date +%H%M)"
	ISSUE_DATE="v2-${major}.${minor}.${revision}"
	echo  "${ISSUE_DATE}" > './lib/dyiot/issue'
	return $?
}

# generate build_info.txt
generate_build_info() {
	local bfile="${FTOPDIR}/lnxall_core/fwupgrade/build_info.txt"
	echo "issue=$1" > ${bfile}
	echo "product=$2" >> ${bfile}
	echo "build_date=$(date '+%Y-%m-%d')" >> ${bfile}
	# 判断 文件存不存在
	if [ -f "${FTOPDIR}/target/product" ]; then
		echo "add ProductInfo from ${FTOPDIR}/target/product"
		echo "support=[`cat ${FTOPDIR}/target/product`]" >> ${bfile}
	else
		echo "Error, failed to generate build_info.txt" 1>&2
	fi

	versionf=`ls ${FTOPDIR}/target/install/ipkg-dirs/*-proto_forward/opt/lnxall_app/version.log`
	if [ -f "${versionf}" ]; then
		echo "firmware=`cat ${versionf}`" >> ${bfile}
	else
		echo "Error, failed to generate version.log" 1>&2
		echo "firmware=unknow" >> ${bfile}
	fi
}

target_elf_build() {
	lnxbf_ipkg_finalize
	[ $? -ne 0 ] && return 1

	generate_build_info "$(date '+%Y-%m-%d')" emu
	lnxbf_elf_generate "LNXALL_APP_ARMHF_fwupgrade.elf"
	return $?
}

target_elf_clean() {
	cd "${FTOPDIR}/lnxall_core/fwupgrade" && \
		rm -rf *.sh *.bin *.out *.txt ./target

	cd "${FSTAGING_DIR}" && rm -rf *
	return 0
}

register_source 'target_iMX6ULL_glibc/gen_fw' \
	target_dummy_config target_elf_build target_elf_clean
