#!/bin/bash

# 检查是否提供了所有必要参数
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
  echo "Error: All three parameters are required."
  exit 1
fi

# 域名
rootUrl=$1
# 租户名称
tenantName=$2
# 前端路径
packUrl=$3


# 执行构建命令
cd "$packUrl" || { echo "Error: Failed to change directory to $packUrl"; exit 1; }


# 检查 $packUrl/dist 路径是否存在存在就不需要打包
if [ ! -d "$packUrl/dist" ]; then


# 执行构建命令
npm install

npm run build -- --tenant="$tenantName"

fi


# 删除目标目录下的所有文件和子目录
rm -rf "$rootUrl"

# 执行复制命令
cp -r "$packUrl/dist/" "$rootUrl"

sudo nginx -s reload
# 打印成功信息
echo "Build and copy completed for server: $rootUrl and tenant: $tenantName"
