#!/bin/bash

SAVE_FLAG=0
if [ $# -eq 1 ]; then
	if [ "$1" == "save" ]; then
		SAVE_FLAG=1
	elif [ "$1" != "menuconfig" ]; then
		echo -e "\033[31m[ERROR]\033[0m ${BASH_SOURCE}:${LINENO}: Input parameter 'save' to save output file or 'menuconfig' to set configuration"
		exit 1
	fi
fi

if [ -n "$(git status --porcelain)" ]; then
	if [ "$1" = "save" ]; then
		echo -e "\033[31m[ERROR]\031[0m ${BASH_SOURCE}:${LINENO}: PLEASE COMMIT YOUR CHANGE FIRST!!!"
		exit 1
	fi
	if [ "$1" != "menuconfig" ]; then
		echo -e "\033[33m[WARNING]\033[0m ${BASH_SOURCE}:${LINENO}: NEVER COMMIT YOUR CHANGE"
		sleep 2
	fi
	GIT_RELEASE_CODE="(beta)"
else
	GIT_TIME=$(git log -1 --format="%at" | xargs -I{} date -d @{} +%Y%m%d_%H%M)
	GIT_HASH=$(git -C . log -1 --format="%h")
	GIT_RELEASE_CODE=${GIT_TIME}-${GIT_HASH}
fi
RELEASE_DIR=release
RELEASE_NAME=${FW_NAME}-${CHIP_NAME}-${GIT_RELEASE_CODE}-v${FW_VERSION_MAIN}.${FW_VERSION_MINOR}.${FW_VERSION_BUILD}
RELEASE_PRO_FILE=${RELEASE_DIR}/Release-pro-${RELEASE_NAME}.bin
RELEASE_OTA_FILE=${RELEASE_DIR}/Release-ota-${RELEASE_NAME}.bin
SDK_CONFIG_FILE=sdkconfig_defaults/sdkconfig.release_${FW_NAME}_${CHIP_NAME}${ADDITIONAL}.defaults


if [ ! -f ${RELEASE_OTA_FILE} ] || [ ! -f ${RELEASE_PRO_FILE} ] || [ "$1" != "save" ]; then

	# 检查配置文件是否存在
	if [ ! -f ${SDK_CONFIG_FILE} ]; then
		echo -e "\033[31m[ERROR]\033[0m ${BASH_SOURCE}:${LINENO}: No such sdkconfig file '${SDK_CONFIG_FILE}'"
		exit 1
	fi

	# 删除输出目录下旧的临时输出文件
	mkdir -p ${RELEASE_DIR} && rm -f `find ${RELEASE_DIR} -maxdepth 1 -type f -name "*.bin"`

	# 生成的头文件用于在编译阶段检查文件名与版本号的一致性
	echo -e \
		"/* Automatically generated file; DO NOT EDIT. */\r\n\r\n"\
		"#pragma once\r\n\r\n"\
		"#define FW_VERSION_MAIN  ${FW_VERSION_MAIN} /* --------------------------------================================ 请检查版本号！！ ================================-------------------------------- */\r\n"\
		"#define FW_VERSION_MINOR ${FW_VERSION_MINOR} /* --------------------------------================================ 请检查版本号！！ ================================-------------------------------- */\r\n"\
		"#define FW_VERSION_BUILD ${FW_VERSION_BUILD} /* --------------------------------================================ 请检查版本号！！ ================================-------------------------------- */\r\n"\
		"#define FW_RELEASE_CODE  \"${GIT_RELEASE_CODE}\""\
		> app/config/version_build.h

	# 执行完整编译
	if [ "$1" == "menuconfig" ]; then
		idf.py -D SDKCONFIG_DEFAULTS="${SDK_CONFIG_FILE}" set-target ${CHIP_NAME} menuconfig || exit 1
		exit 0
	else
		idf.py -D SDKCONFIG_DEFAULTS="${SDK_CONFIG_FILE}" set-target ${CHIP_NAME} build size || exit 1
	fi

	# 复制固件文件到固定位置
	cp build/project.bin ${RELEASE_OTA_FILE} || exit 1

	# 合并为完整量产包并输出到固定位置
	esptool.py \
		--chip ${CHIP_NAME} merge_bin \
		-o ${RELEASE_PRO_FILE} \
		0x0     build/bootloader/bootloader.bin \
		0x8000  build/partition_table/partition-table.bin \
		0x65000 build/ota_data_initial.bin \
		0x70000 build/project.bin \
	|| exit 1

fi

# 显示执行结果
echo -e "\n\x1b[33m[COMPLETE]\x1b[0m"
echo -e "Latest copy folder:        \x1b[34m$(readlink -e ${RELEASE_DIR}/)/\x1b[0m"
echo -e "Release ota packet:        \x1b[34m${RELEASE_OTA_FILE}\x1b[0m"
echo -e "Release production packet: \x1b[34m${RELEASE_PRO_FILE}\x1b[0m"

RELEASE_DIR=${RELEASE_DIR}/${FW_NAME}
RELEASE_OTA_FILE_SAVE=${RELEASE_DIR}/$(basename ${RELEASE_OTA_FILE})
RELEASE_PRO_FILE_SAVE=${RELEASE_DIR}/$(basename ${RELEASE_PRO_FILE})

if [ "${GIT_RELEASE_CODE}" != "(beta)" ]; then
	if [ "${SAVE_FLAG}" == "1" ]; then

		# 将固定位置的 升级包和量产包 保存到对应的固件类型目录中
		mkdir -p ${RELEASE_DIR} || exit 1
		if [ ! -f ${RELEASE_OTA_FILE_SAVE} ] || [ ! -f ${RELEASE_PRO_FILE_SAVE} ]; then
			cp ${RELEASE_OTA_FILE} ${RELEASE_OTA_FILE_SAVE} || exit 1
			cp ${RELEASE_PRO_FILE} ${RELEASE_PRO_FILE_SAVE} || exit 1
		fi
		echo -e "Release ota packet:        \x1b[34m${RELEASE_OTA_FILE_SAVE}\x1b[0m"
		echo -e "Release production packet: \x1b[34m${RELEASE_PRO_FILE_SAVE}\x1b[0m"

	else

		if [ ! -f ${RELEASE_OTA_FILE_SAVE} ] || [ ! -f ${RELEASE_PRO_FILE_SAVE} ]; then
			echo -e "To save these release files, run this command:\r\n${0} save"
		fi

	fi
fi
