#!/bin/bash
# 2025-09-28 11:15 created by niujiuru

set -e
set -o pipefail

ver_num="1.0.0.8"
cur_dir="$(pwd)"
pre_dir="$(dirname "$cur_dir")"
pkg_dir="${cur_dir}/rtu_bxs_seyou"
pkg_tar="${cur_dir}/rtu_bxs_seyou_${ver_num}.tar.gz"

rm -rf "${pkg_dir}" "${pkg_tar}"

echo "1, Creating config directory and copying config files..."
mkdir -p "${pkg_dir}/config"
cp -rf "${pre_dir}/config.ini" "${pkg_dir}/config/"
cp -rf "${pre_dir}/mcu_ctrl_board/mcuparams.ini" "${pkg_dir}/config/"

echo "2, Creating script directory and copying scripts..."
mkdir -p "${pkg_dir}/script"
cp -rf "${pre_dir}/scripts/"* "${pkg_dir}/script/"
chmod a+x "${pkg_dir}/script/"*

echo "3, Creating lib directory and copying library files..."
mkdir -p "${pkg_dir}/lib"
cp -rf "${pre_dir}/mvs_takephoto/lib/armv7hf/"* "${pkg_dir}/lib/"

echo "4, Building hkcam_reset..."
make -C "${pre_dir}/mvs_takephoto" clean
make -C "${pre_dir}/mvs_takephoto" target=armv7hf hkcam_reset.out
cp -rf "${pre_dir}/mvs_takephoto/hkcam_reset.out" "${pkg_dir}/"
make -C "${pre_dir}/mvs_takephoto" clean

echo "5, Building rtu_bxs_seyou..."
cd ${pre_dir}
make -C "${pre_dir}" clean
make -C "${pre_dir}" target=armv7hf
cd ${cur_dir}
cp -rf "${pre_dir}/rtu_bxs_seyou.out" "${pkg_dir}/"

echo "6, Packaging into ${pkg_tar}..."
tar -czvf "${pkg_tar}" -C "${cur_dir}" "${pkg_dir##*/}"

# 清理临时目录
rm -rf "${pkg_dir}"

echo "✅ Package build completed successfully: ${pkg_tar}"
