#!/bin/bash
#
# 部署脚本
#

set -e

#
APP_NAME=flashdeliver
START_TIME=$(date +%s)
DATE=$(date +%m/%d/%y)
TIME=$(date +%k:%M:%S)
TARGET_VERSION=$(date +%Y%m%d%H%M)

# 目录配置
DIST_NAME="dist"
BUILD_NAME=build
WORK_DIR=/d/work-space/vue-wechat-addons-flashdeliver
REMOTE_DIR=/data/flashdeliver

# SSH配置
REMOTE_USER=root
REMOTE_IP=175.178.185.146
PRIVATE_SSH_KEY=/d/work-space/ssh-key/id_rsa_my_tencent_146

# workspace
cd $WORK_DIR

# build
echo -e "\033[34m[INFO]\033[0m Building $APP_NAME ..."
npm run $BUILD_NAME

# upload
echo -e "\033[34m[INFO]\033[0m Uploading $APP_NAME to $REMOTE_IP ..."
tar -cf "$DIST_NAME.zip" $DIST_NAME
scp -i $PRIVATE_SSH_KEY "$DIST_NAME.zip" "$REMOTE_USER@$REMOTE_IP:$REMOTE_DIR"
rm "$DIST_NAME.zip"

# publish
echo -e "\033[34m[INFO]\033[0m Publishing $APP_NAME to $REMOTE_IP ..."
ssh -i $PRIVATE_SSH_KEY "$REMOTE_USER@$REMOTE_IP" /bin/bash <<EOF
cd $REMOTE_DIR
tar -xf "$DIST_NAME.zip" && cp -r $DIST_NAME/* ./
mkdir -p "archive"
mv $DIST_NAME $TARGET_VERSION && mv $TARGET_VERSION "archive"
ls -l index.html
# rm "$DIST_NAME.zip"
exit
EOF

END_TIME=$(date +%s)
EXECUTION_TIME=$((END_TIME - START_TIME))
echo "脚本执行时间：$EXECUTION_TIME 秒".
echo -e "\033[32m[DONE]\033[0m Deployed complete."