#!/bin/bash
set -e

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

## 初始化
root_path=$(cd `dirname $0`; cd ..;pwd)
echo $root_path
cd $root_path

rm -rf $root_path/output
rm -rf node_modules
#rm -rf package-lock.json


## 安装node
export PATH=$NODEJS_BIN_LATEST:$PATH

node --version
npm --version

# 配置node内存
export NODE_OPTIONS=--max_old_space_size=4096

# 配置npm
npm config set registry http://registry.npm.baidu-int.com
npm config set proxy http://proxy.npm.baidu-int.com:8269
PROXY=$(npm config get proxy)
export http_proxy=$PROXY
export https_proxy=$PROXY
export SWAN_HTTP_PROXY=$PROXY
echo $export SWAN_HTTP_PROXY=$PROXY

# 安装依赖
npm install --registry=http://registry.npm.baidu-int.com

if [ $? -eq 0 ];then
    echo 'npm install completely'
else
    echo 'npm install fail'
    exit 1
fi

# 编译小程序
NODE_ENV=production npm run build
# 编译H5
NODE_ENV=production npm run deploy:h5

# 打包到output
static_dir=$root_path/output/webroot/med-h5
template_dir=$root_path/output/template/med-h5
swan_dir=$root_path/output/med-swan

mkdir -p $static_dir
mkdir -p $template_dir
mkdir -p $swan_dir

cp package.json $swan_dir/package.json

cd $root_path/dist-h5/dist
mv index.html $template_dir
mv MP_verify_L2QgtQuzLzDe4U41.txt $template_dir
mv MP_verify_nUa0eQE9VUGWwpK1.txt $template_dir
mv swJZzyGG5D.txt $template_dir
mv static $static_dir
cd $root_path
mv dist-swan $swan_dir

function build_seo() {
    NODE_ENV=production npm run deploy:seo
    static_dir=$root_path/output/webroot/med-s
    template_dir=$root_path/output/template/med-s

    mkdir -p $static_dir
    mkdir -p $template_dir

    cd $root_path/dist-h5/dist
    mv index.html $template_dir
    mv MP_verify_L2QgtQuzLzDe4U41.txt $template_dir
    mv MP_verify_nUa0eQE9VUGWwpK1.txt $template_dir
    mv swJZzyGG5D.txt $template_dir
    mv static $static_dir
    cd $root_path
}

build_seo

# 压缩.tar
cd $root_path/output
tar -zcf static.tar.gz webroot
tar -zcf template.tar.gz template
tar -zcf swan.tar.gz med-swan

rm -rf webroot
rm -rf template
rm -rf med-swan

cd $root_path
echo 'build completely!'







