#!/bin/bash

# Generate install package for Linux Platform

set -e

while getopts "e:v:m:" opt; do
    case "$opt" in
        e) edition="$OPTARG" ;;  # -e enterprise/community
        v) version="$OPTARG" ;;  # -v version
        m) model_dir="$OPTARG" ;;  # -m model_dir
        *) echo "Usage: $0 -e edition -v version"; exit 1 ;;
    esac
done

if [ -z "$edition" ] || [ -z "$version" ]; then
    echo "Usage: $0 -e edition -v version"
    exit 1
fi
if [ "$edition" == "enterprise" ]; then
    productName="tdengine-tdgpt-enterprise"
fi

if [ "$edition" == "community" ]; then
    productName="tdengine-tdgpt-oss"
fi

echo start to build release package, edition: ${edition}, version: ${version}

curr_dir=$(pwd)
compile_dir=$1

script_dir="$(dirname $(readlink -f $0))"
top_dir="$(readlink -f ${script_dir}/..)"

echo -e ${top_dir}

serverName="taosanoded"
configFile="taosanode.ini"
tarName="package.tar.gz"

# create compressed install file.
build_dir="${compile_dir}/build"
release_dir="${top_dir}/release"

#package_name='linux'
install_dir="${release_dir}/${productName}-${version}"

cfg_dir="${top_dir}/cfg"
install_files="${script_dir}/install.sh"

# make directories.
mkdir -p ${install_dir}
mkdir -p ${install_dir}/cfg && cp ${cfg_dir}/${configFile} ${install_dir}/cfg/${configFile}
mkdir -p ${install_dir}/resource &&  cp ${top_dir}/resource/*  ${install_dir}/resource/

if [ -f "${cfg_dir}/${serverName}.service" ]; then
  cp ${cfg_dir}/${serverName}.service ${install_dir}/cfg || :
fi

# python files
lib_install_dir="${install_dir}/lib"
mkdir -p ${install_dir}/bin && mkdir -p ${lib_install_dir}

# copy enterprise tools from outside
if [ "$edition" == "enterprise" ]; then
  cp -r ${top_dir}/../../../enterprise/src/kit/tools/tdgpt/taosanalytics/misc/*   ${top_dir}/taosanalytics/misc/ || :
fi

# remove cache files generated by Python interpreter
TARGET_PATTERN="__pycache__"
find "${top_dir}/taosanalytics/" -type d -name "$TARGET_PATTERN" -exec rm -rf {} +

# script to control start/stop/uninstall process
cp -r ${top_dir}/taosanalytics/ ${lib_install_dir}/ && chmod a+x ${lib_install_dir}/ || :
cp -r ${top_dir}/script/st*.sh ${install_dir}/bin/ && chmod a+x ${install_dir}/bin/* || :
cp -r ${top_dir}/script/uninstall.sh ${install_dir}/bin/ && chmod a+x ${install_dir}/bin/* || :
cp -r ${top_dir}/requirements_ess.txt ${install_dir}/ || :

# copy model files
model_dir=${model_dir:-""}
model_install_dir="${install_dir}/model"

if [ -d "${model_dir}" ]; then
  mkdir -p ${model_install_dir}
  td_model_name="tdtsfm"
  echo "copy ${td_model_name} model files"
  cp -r ${model_dir}/${td_model_name}.tar.gz ${model_install_dir} || :
  echo "copy ${td_model_name} model files done"
  xhs_model_name="timemoe"
  echo "copy ${xhs_model_name} model files "
  cp -r ${model_dir}/${xhs_model_name}.tar.gz ${model_install_dir}|| :
  echo "copy ${xhs_model_name} model files done"
fi

# tar lib and model files
cd ${install_dir} && tar -zcv -f ${tarName} ./lib/* || :

# shellcheck disable=SC2115
[ -d "${lib_install_dir}" ] && rm -rf ${lib_install_dir} || :

exitcode=$?
if [ "$exitcode" != "0" ]; then
  echo "tar ${tarName} error !!!"
  exit $exitcode
fi

cd ${curr_dir}
cp ${install_files} ${install_dir}

chmod a+x ${install_dir}/install.sh

# Copy release note
# cp ${script_dir}/release_note ${install_dir}

# exit 1
cd ${release_dir}

platform=$(uname | tr '[:upper:]' '[:lower:]')
if uname -m | grep -q "x86_64"; then
    arch=x64
elif uname -m | grep -q "arm64\|aarch64"; then
    arch=arm64
fi
pkg_name=${install_dir}-${platform}-${arch}
echo -e "pkg_name is: ${pkg_name}"

if [ "$osType" != "Darwin" ]; then
    tar -zcv -f "$(basename ${pkg_name}).tar.gz" "$(basename ${install_dir})" --remove-files || :
else
    tar -zcv -f "$(basename ${pkg_name}).tar.gz" "$(basename ${install_dir})" || :
    rm -rf "${install_dir}" ||:
fi

exitcode=$?
if [ "$exitcode" != "0" ]; then
  echo "tar ${pkg_name}.tar.gz error !!!"
  exit $exitcode
fi

if [ "$edition" == "enterprise" ]; then
  rm -f ${top_dir}/taosanalytics/misc/analytics* || :
  echo -e "clean up"
fi

cd ${curr_dir}
